Skip to content

Commit 8ed0c14

Browse files
committed
redis caching completed
1 parent a5031d5 commit 8ed0c14

File tree

5 files changed

+24
-32
lines changed

5 files changed

+24
-32
lines changed

.env.development

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ JWT_SECRET="superSKR"
1919
JWT_TOKEN_EXPIRES_IN="10m"
2020

2121
# REDIS related
22-
REDIS_CONNECTION_URL="redis://52.66.253.128:6379"
22+
REDIS_CONNECTION_URL="redis://52.66.253.128:6379"
23+
REDIS_CACHE_EXPIRY_TIME=2*60

serverless.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
"login": {
3333
"handler": "src/services/loginService.loginHandler",
3434
"events": [{ "httpApi": "POST /login" }]
35-
},
36-
"checkRedis": {
37-
"handler": "src/services/redisService.checkRedisHandler",
38-
"events": [{ "httpApi": "POST /check" }]
3935
}
4036
}
4137
}

src/dbRelated/userDbOps.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ const getAllUsers = async () => {
2323
// redis operations
2424
let usersInCache = [];
2525
try {
26-
await connectRedis();
26+
connectRedis();
2727
usersInCache = await getValueFromRedis("allUsers");
28-
await closeConnectionToRedis();
28+
usersInCache = JSON.parse(usersInCache);
29+
if (usersInCache) {
30+
return sendResponse(SUCCESS_CODE, { usersInCache });
31+
}
2932
} catch (error) {
3033
return sendResponse(ERROR_CODE, {
3134
message: "Unable to get records from Redis",
3235
error: error.toString(),
3336
});
37+
} finally {
38+
closeConnectionToRedis();
3439
}
40+
3541
const client = await createConnectionToDB();
3642
try {
3743
// select the db, Collections are selected based on needs
@@ -44,7 +50,10 @@ const getAllUsers = async () => {
4450
.toArray();
4551

4652
const users = data.length > 0 ? [...data] : [];
47-
const res = { users, usersInCache };
53+
54+
// save data to Redis cache
55+
await setValueToRedis("allUsers", users);
56+
const res = { users };
4857

4958
return sendResponse(SUCCESS_CODE, res);
5059
} catch (error) {
@@ -56,6 +65,7 @@ const getAllUsers = async () => {
5665
} finally {
5766
// close the connection to MongoDB Atlas
5867
closeConnectionToDB(client);
68+
closeConnectionToRedis();
5969
}
6070
};
6171

src/helpers/redisHelpers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ const connectRedis = async () => {
66

77
// get value based on the key provided
88
const getValueFromRedis = async (key) => {
9+
// convert key to String
10+
key = typeof key !== "string" ? JSON.stringify(key) : key;
911
const value = await redisClient.get(key);
1012
return value;
1113
};
1214

1315
// set the value to key provided
1416
const setValueToRedis = async (key, value) => {
15-
await redisClient.set(key, value);
17+
// convert key, value to String
18+
key = typeof key !== "string" ? JSON.stringify(key) : key;
19+
value = typeof value !== "string" ? JSON.stringify(value) : value;
20+
21+
await redisClient.set(key, value, {
22+
EX: process.env.REDIS_CACHE_EXPIRY_TIME,
23+
});
1624
};
1725

1826
// close the redis connection

src/services/redisService.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)