Skip to content

Commit a5031d5

Browse files
committed
redis caching added
1 parent 2ff7cb5 commit a5031d5

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

.env.development

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Application related
2+
APP_NAME="simple-aws-lamba-mongodb"
13
# AWS related
24
REGION="ap-south-1"
35

@@ -17,4 +19,4 @@ JWT_SECRET="superSKR"
1719
JWT_TOKEN_EXPIRES_IN="10m"
1820

1921
# REDIS related
20-
REDIS_CONNECTION_URL="redis://35.154.200.227:6379"
22+
REDIS_CONNECTION_URL="redis://52.66.253.128:6379"

src/dbRelated/userDbOps.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,31 @@ import {
77
convertToObjectId,
88
} from "../helpers/dbHelpers.js";
99

10-
// top level await to connect the MongoDB Atlas
11-
// const client = await dbClient.connect();
10+
// redis dependencies
11+
import {
12+
setValueToRedis,
13+
getValueFromRedis,
14+
connectRedis,
15+
closeConnectionToRedis,
16+
} from "../helpers/redisHelpers.js";
1217

1318
// all the db related process variables
14-
const {
15-
DB_NAME,
16-
COLLECTION_USER_STICKER,
17-
SUCCESS_CODE,
18-
ERROR_CODE,
19-
} = process.env;
19+
const { DB_NAME, COLLECTION_USER_STICKER, SUCCESS_CODE, ERROR_CODE } =
20+
process.env;
2021

2122
const getAllUsers = async () => {
23+
// redis operations
24+
let usersInCache = [];
25+
try {
26+
await connectRedis();
27+
usersInCache = await getValueFromRedis("allUsers");
28+
await closeConnectionToRedis();
29+
} catch (error) {
30+
return sendResponse(ERROR_CODE, {
31+
message: "Unable to get records from Redis",
32+
error: error.toString(),
33+
});
34+
}
2235
const client = await createConnectionToDB();
2336
try {
2437
// select the db, Collections are selected based on needs
@@ -31,7 +44,7 @@ const getAllUsers = async () => {
3144
.toArray();
3245

3346
const users = data.length > 0 ? [...data] : [];
34-
const res = { users };
47+
const res = { users, usersInCache };
3548

3649
return sendResponse(SUCCESS_CODE, res);
3750
} catch (error) {

src/helpers/jwtHelpers.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ const { sign, verify } = jwt;
88

99
// create token from the payload
1010
const createToken = (payload) => {
11-
const token = sign(payload, process.env.JWT_SECRET, {
12-
expiresIn: process.env.JWT_TOKEN_EXPIRES_IN,
13-
});
11+
const token = sign(
12+
{ ...payload, appName: process.env.APP_NAME },
13+
process.env.JWT_SECRET,
14+
{
15+
expiresIn: process.env.JWT_TOKEN_EXPIRES_IN,
16+
}
17+
);
1418

1519
return token;
1620
};
@@ -21,7 +25,7 @@ const verifyToken = (event) => {
2125
const decoded = verify(token, process.env.JWT_SECRET);
2226

2327
// if the token has the correct data it is passed
24-
if (decoded.username === "anijit123") {
28+
if (decoded.appName === process.env.APP_NAME) {
2529
return true;
2630
}
2731
return false;

src/helpers/redisHelpers.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ const connectRedis = async () => {
55
};
66

77
// get value based on the key provided
8-
const getValue = async (key) => {
8+
const getValueFromRedis = async (key) => {
99
const value = await redisClient.get(key);
1010
return value;
1111
};
1212

1313
// set the value to key provided
14-
const setValue = async (key, value) => {
14+
const setValueToRedis = async (key, value) => {
1515
await redisClient.set(key, value);
16-
return;
1716
};
1817

19-
export { connectRedis, getValue, setValue };
18+
// close the redis connection
19+
const closeConnectionToRedis = async () => {
20+
await redisClient.quit();
21+
};
22+
23+
export {
24+
connectRedis,
25+
getValueFromRedis,
26+
setValueToRedis,
27+
closeConnectionToRedis,
28+
};

src/services/redisService.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
// local dependencies
2-
import { setValue, getValue, connectRedis } from "../helpers/redisHelpers.js";
2+
import {
3+
setValueToRedis,
4+
getValueFromRedis,
5+
connectRedis,
6+
closeConnectionToRedis,
7+
} from "../helpers/redisHelpers.js";
38
import { sendResponse } from "../helpers/sendResponse.js";
49

510
const checkRedisHandler = async (event) => {
611
try {
7-
const res = { message: "API endpoint reached" };
12+
await connectRedis();
13+
const cachedNameValue = await getValueFromRedis("name");
14+
await setValueToRedis("name", "JOe");
15+
await closeConnectionToRedis();
16+
const res = { message: "API endpoint reached", name: cachedNameValue };
817
return sendResponse(process.env.SUCCESS_CODE, res);
918
} catch (error) {
1019
return sendResponse(process.env.ERROR_CODE, { error: error.toString() });

0 commit comments

Comments
 (0)