-
Notifications
You must be signed in to change notification settings - Fork 26
/
cacheDecorator.js
33 lines (32 loc) · 1.35 KB
/
cacheDecorator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @param {FastifyInstance} fastify
* @param {string} segmentName
*/
module.exports = function (fastify, segmentName) {
fastify.decorate("selectiveFlush", async function (prefix) {
if (fastify.cache.keys) {
// this is a blocking operation in redis.
// should eventually be replaced with having a separate redis instance and flushing it all
const keys = await fastify.cache.keys(`${prefix}*`);
fastify.log.info(`flushing keys ${prefix}* ${JSON.stringify(keys)}`);
// cache exposes a method that deletes a single key asynchronously.
// using that would be terrible for node performance ad medium scale.
// instead, use del(...keys) from redis directly
if (keys.length) await fastify.redis.del(...keys.map((k) => `${segmentName}:${k}`));
} else if (fastify.cache.client._cache?._keymap) {
const _keys = fastify.cache.client._cache._keymap.keys();
const _keysToDelete = [];
for (const _key of _keys) {
if (_key.startsWith(`${segmentName}:${prefix}`)) _keysToDelete.push(_key);
}
_keysToDelete.forEach((_key) => fastify.cache.delete(_keysToDelete));
}
});
fastify.decorate("flush", async function () {
if (fastify.redis) {
await fastify.redis.del();
} else if (fastify.cache.client._cache?._keymap) {
fastify.cache.client._cache._keymap.clear();
}
});
};