diff --git a/examples/cache-handler-redis/cache-handler-redis-custom.js b/examples/cache-handler-redis/cache-handler-redis-custom.js index 996a2b093e23b1..98cda68bdaed66 100644 --- a/examples/cache-handler-redis/cache-handler-redis-custom.js +++ b/examples/cache-handler-redis/cache-handler-redis-custom.js @@ -3,6 +3,7 @@ const { replaceJsonWithBase64, } = require('@neshca/json-replacer-reviver') const { IncrementalCache } = require('@neshca/cache-handler') +const createLruCache = require('@neshca/cache-handler/local-lru').default const { createClient } = require('redis') const REVALIDATED_TAGS_KEY = 'sharedRevalidatedTags' @@ -21,64 +22,68 @@ IncrementalCache.onCreation(async () => { await client.connect() - return { - useFileSystem: !useTtl, - cache: { - async get(key) { - try { - const result = (await client.get(key)) ?? null - - if (!result) { - return null - } - - // use reviveFromBase64Representation to restore binary data from Base64 - return JSON.parse(result, reviveFromBase64Representation) - } catch (error) { - console.error('cache.get', error) + const redisCache = { + async get(key) { + try { + const result = (await client.get(key)) ?? null + if (!result) { return null } - }, - async set(key, value, ttl) { - try { - await client.set( - key, - // use replaceJsonWithBase64 to store binary data in Base64 and save space - JSON.stringify(value, replaceJsonWithBase64), - useTtl && typeof ttl === 'number' ? { EX: ttl } : undefined - ) - } catch (error) { - console.error('cache.set', error) - } - }, - async getRevalidatedTags() { - try { - const sharedRevalidatedTags = await client.hGetAll( - REVALIDATED_TAGS_KEY - ) - const entries = Object.entries(sharedRevalidatedTags) + // use reviveFromBase64Representation to restore binary data from Base64 + return JSON.parse(result, reviveFromBase64Representation) + } catch (error) { + console.error('cache.get', error) - const revalidatedTags = Object.fromEntries( - entries.map(([tag, revalidatedAt]) => [tag, Number(revalidatedAt)]) - ) + return null + } + }, + async set(key, value, ttl) { + try { + await client.set( + key, + // use replaceJsonWithBase64 to store binary data in Base64 and save space + JSON.stringify(value, replaceJsonWithBase64), + useTtl && typeof ttl === 'number' ? { EX: ttl } : undefined + ) + } catch (error) { + console.error('cache.set', error) + } + }, + async getRevalidatedTags() { + try { + const sharedRevalidatedTags = await client.hGetAll(REVALIDATED_TAGS_KEY) - return revalidatedTags - } catch (error) { - console.error('cache.getRevalidatedTags', error) - } - }, - async revalidateTag(tag, revalidatedAt) { - try { - await client.hSet(REVALIDATED_TAGS_KEY, { - [tag]: revalidatedAt, - }) - } catch (error) { - console.error('cache.revalidateTag', error) - } - }, + const entries = Object.entries(sharedRevalidatedTags) + + const revalidatedTags = Object.fromEntries( + entries.map(([tag, revalidatedAt]) => [tag, Number(revalidatedAt)]) + ) + + return revalidatedTags + } catch (error) { + console.error('cache.getRevalidatedTags', error) + } }, + async revalidateTag(tag, revalidatedAt) { + try { + await client.hSet(REVALIDATED_TAGS_KEY, { + [tag]: revalidatedAt, + }) + } catch (error) { + console.error('cache.revalidateTag', error) + } + }, + } + + const localCache = createLruCache({ + useTtl, + }) + + return { + cache: [redisCache, localCache], + useFileSystem: !useTtl, } }) diff --git a/examples/cache-handler-redis/cache-handler-redis.js b/examples/cache-handler-redis/cache-handler-redis.js index eccdc5f111679a..3adbbc162cfc10 100644 --- a/examples/cache-handler-redis/cache-handler-redis.js +++ b/examples/cache-handler-redis/cache-handler-redis.js @@ -13,7 +13,7 @@ client.on('error', (error) => { IncrementalCache.onCreation(async () => { // read more about TTL limitations https://caching-tools.github.io/next-shared-cache/configuration/ttl - const useTtl = false + const useTtl = true await client.connect()