From e05deef6e28c7bbddda154d3d23f5325e85f3571 Mon Sep 17 00:00:00 2001 From: Meier Lukas Date: Sat, 18 May 2024 15:28:05 +0200 Subject: [PATCH] feat: add redis cache channel --- packages/redis/src/lib/channel.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/redis/src/lib/channel.ts b/packages/redis/src/lib/channel.ts index a80a8fb19..bb41f10cc 100644 --- a/packages/redis/src/lib/channel.ts +++ b/packages/redis/src/lib/channel.ts @@ -53,6 +53,34 @@ export const createSubPubChannel = (name: string) => { }; }; +const cacheClient = createRedisConnection(); + +/** + * Creates a new cache channel. + * @param name name of the channel + * @returns cache channel object + */ +export const createCacheChannel = (name: string) => { + const cacheChannelName = `cache:${name}`; + return { + /** + * Get the data from the cache channel. + * @returns data or undefined if not found + */ + getAsync: async () => { + const data = await cacheClient.get(cacheChannelName); + return data ? superjson.parse(data) : undefined; + }, + /** + * Set the data in the cache channel. + * @param data data to be stored in the cache channel + */ + setAsync: async (data: TData) => { + await cacheClient.set(cacheChannelName, superjson.stringify(data)); + }, + }; +}; + const queueClient = createRedisConnection(); type WithId = TItem & { _id: string };