-
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.
examples: Update
cache-handler-redis
(#58562)
- Loading branch information
1 parent
ba3ef55
commit 10c794a
Showing
6 changed files
with
120 additions
and
177 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
103 changes: 103 additions & 0 deletions
103
examples/cache-handler-redis/cache-handler-redis-custom.js
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const { | ||
reviveFromBase64Representation, | ||
replaceJsonWithBase64, | ||
} = require('@neshca/json-replacer-reviver') | ||
const { IncrementalCache } = require('@neshca/cache-handler') | ||
const { createClient } = require('redis') | ||
|
||
/** @type {import('@neshca/cache-handler').TagsManifest} */ | ||
const localTagsManifest = { | ||
version: 1, | ||
items: {}, | ||
} | ||
|
||
const TAGS_MANIFEST_KEY = 'sharedTagsManifest' | ||
|
||
function createRedisClient(url) { | ||
const client = createClient({ | ||
url, | ||
}) | ||
|
||
client.on('error', (error) => { | ||
console.error('Redis error:', error.message) | ||
}) | ||
|
||
return client | ||
} | ||
|
||
async function connect(client) { | ||
try { | ||
await client.connect() | ||
} catch (error) { | ||
console.error('Redis connection error:', error.message) | ||
} | ||
} | ||
|
||
IncrementalCache.onCreation(() => { | ||
const client = createRedisClient( | ||
process.env.REDIS_URL ?? 'redis://localhost:6379' | ||
) | ||
|
||
connect(client).then(() => { | ||
console.log('Redis connected') | ||
}) | ||
|
||
return { | ||
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) { | ||
return null | ||
} | ||
}, | ||
async set(key, value) { | ||
try { | ||
// use replaceJsonWithBase64 to store binary data in Base64 and save space | ||
await client.set(key, JSON.stringify(value, replaceJsonWithBase64)) | ||
} catch (error) { | ||
// ignore because value will be written to disk | ||
} | ||
}, | ||
async getTagsManifest() { | ||
try { | ||
const remoteTagsManifest = await client.hGetAll(TAGS_MANIFEST_KEY) | ||
|
||
if (!remoteTagsManifest) { | ||
return localTagsManifest | ||
} | ||
|
||
Object.entries(remoteTagsManifest).reduce( | ||
(acc, [tag, revalidatedAt]) => { | ||
acc[tag] = { revalidatedAt: parseInt(revalidatedAt ?? '0', 10) } | ||
return acc | ||
}, | ||
localTagsManifest.items | ||
) | ||
|
||
return localTagsManifest | ||
} catch (error) { | ||
return localTagsManifest | ||
} | ||
}, | ||
async revalidateTag(tag, revalidatedAt) { | ||
try { | ||
await client.hSet(TAGS_MANIFEST_KEY, { | ||
[tag]: revalidatedAt, | ||
}) | ||
} catch (error) { | ||
localTagsManifest.items[tag] = { revalidatedAt } | ||
} | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
module.exports = IncrementalCache |
This file was deleted.
Oops, something went wrong.
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