-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
cache-handler-redis
example dependencies (#59458)
This pull request updates the dependencies in the cache-handler-redis example. It updates the versions of the `@neshca/cache-handler`, which introduces new features. --------- Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
- Loading branch information
1 parent
21dbfec
commit 8694ed6
Showing
4 changed files
with
113 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
const { IncrementalCache } = require('@neshca/cache-handler') | ||
const { createHandler } = require('@neshca/cache-handler/redis-stack') // @neshca/cache-handler/redis-strings also available | ||
const createRedisCache = require('@neshca/cache-handler/redis-stack').default | ||
const createLruCache = require('@neshca/cache-handler/local-lru').default | ||
const { createClient } = require('redis') | ||
|
||
function createRedisClient(url) { | ||
const client = createClient({ | ||
url, | ||
}) | ||
|
||
client.on('error', (error) => { | ||
console.error('Redis error:', error.message) | ||
}) | ||
const client = createClient({ | ||
url: process.env.REDIS_URL ?? 'redis://localhost:6379', | ||
}) | ||
|
||
return client | ||
} | ||
client.on('error', (error) => { | ||
console.error('Redis error:', error.message) | ||
}) | ||
|
||
async function connect(client) { | ||
try { | ||
await client.connect() | ||
} catch (error) { | ||
console.error('Redis connection error:', error.message) | ||
} | ||
} | ||
IncrementalCache.onCreation(async () => { | ||
// read more about TTL limitations https://caching-tools.github.io/next-shared-cache/configuration/ttl | ||
const useTtl = true | ||
|
||
const client = createRedisClient( | ||
process.env.REDIS_URL ?? 'redis://localhost:6379' | ||
) | ||
await client.connect() | ||
|
||
connect(client).then(() => { | ||
console.log('Redis connected') | ||
}) | ||
|
||
IncrementalCache.onCreation( | ||
createHandler({ | ||
const redisCache = await createRedisCache({ | ||
client, | ||
useTtl, | ||
}) | ||
|
||
const localCache = createLruCache({ | ||
useTtl, | ||
}) | ||
) | ||
|
||
return { | ||
cache: [redisCache, localCache], | ||
useFileSystem: !useTtl, | ||
} | ||
}) | ||
|
||
module.exports = IncrementalCache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters