Skip to content

Commit

Permalink
Enable TTL by default in cache-handler-redis example
Browse files Browse the repository at this point in the history
  • Loading branch information
better-salmon committed Dec 12, 2023
1 parent 4744d58 commit da6f80c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
107 changes: 56 additions & 51 deletions examples/cache-handler-redis/cache-handler-redis-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
}
})

Expand Down
2 changes: 1 addition & 1 deletion examples/cache-handler-redis/cache-handler-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit da6f80c

Please sign in to comment.