Skip to content

Commit

Permalink
fix(server): Error in setCache and hashCache functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AdwaithAthman committed Jan 3, 2024
1 parent 5a10f9a commit 6e7024e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions server/src/frameworks/database/redis/redisRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@ export const redisRepository = () => {
if (cachedData.length > 0)
return cachedData.map((data) => JSON.parse(data));
const freshData = await cb();
const serializedData = freshData.map((data: any) => JSON.stringify(data));
redisClient.sAdd(key, serializedData);
redisClient.expire(key, DEFAULT_EXPIRATION);
if (freshData.length > 0) {
const serializedData = freshData.map((data: any) => JSON.stringify(data));
redisClient.sAdd(key, serializedData);
redisClient.expire(key, DEFAULT_EXPIRATION);
}
return freshData;
};


const hashCache = async (key: string, cb: Function) => {
const cachedData = await redisClient.hGetAll(key);
if (Object.keys(cachedData).length > 0) {
console.log("from cache")
Object.keys(cachedData).forEach((field) => {
cachedData[field] = JSON.parse(cachedData[field]);
});
return cachedData;
}
const freshData = await cb();
console.log('no cache')
for (const field of Object.keys(freshData)) {
redisClient.hSet(key, field, JSON.stringify(freshData[field]));
if (freshData[field] !== undefined) {
redisClient.hSet(key, field, JSON.stringify(freshData[field]));
}
}
redisClient.expire(key, DEFAULT_EXPIRATION);
return freshData;
Expand Down

0 comments on commit 6e7024e

Please sign in to comment.