Skip to content

Commit

Permalink
feat: create a function to update data in redis cache
Browse files Browse the repository at this point in the history
Create `updateRedisCache` function to provide an easy way to update data
in redis cache.

Make `updateSetting` function takes an instant of `ClientSession` class
as option.

Use MongoDB session through `startSession` and Implement a logic in
`settings_updateSetting_post` to ensure that the database and redis cache updated
successfully in case the setting that is targeted to update stored in redis cache.
  • Loading branch information
AbdulrhmanGoni committed Apr 7, 2024
1 parent 94b730d commit 6afb6e8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/cache/updateRedisCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import redisClient from "../configuration/redisClient.js";

export default async function updateRedisCache(key, updateFunction, options) {
const currentData = await redisClient.get(key);
const updatedData = updateFunction(JSON.parse(currentData));
return await redisClient.set(key, JSON.stringify(updatedData), options)
.then(() => true)
.catch(() => false)
}
4 changes: 2 additions & 2 deletions src/controllers/settings-controllers/updateSetting.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import SettingsModel from "../../models/Settings.js";

export default async function updateSetting(setting, newValue) {
export default async function updateSetting(setting, newValue, session) {
try {
const res = await SettingsModel.updateOne({}, { [setting]: newValue });
const res = await SettingsModel.updateOne({}, { [setting]: newValue }, { session });
return !!res.modifiedCount
} catch (error) {
console.log(error)
Expand Down
30 changes: 28 additions & 2 deletions src/routes/settings_routes/settings_updateSetting_post.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { startSession } from "mongoose";
import { storeVariablesNames } from "../../CONSTANT/storeVariables.js";
import SettingsController from "../../controllers/settings-controllers/SettingsController.js";
import asyncRouteHandler from "../../utilities/asyncRouteHandler.js";
import updateRedisCache from "../../cache/updateRedisCache.js";

export default asyncRouteHandler(
async function settings_updateSetting_post(req, res) {
const { setting, newValue } = req.body;
const respond = await SettingsController.updateSetting(setting, newValue);
res.status(respond === undefined ? 400 : 200).json(respond);
const isStoreVariable = storeVariablesNames.includes(setting);
if (isStoreVariable) {
const session = await startSession();
session.startTransaction();
const respond = await SettingsController.updateSetting(setting, newValue, session);
if (respond) {
const updateResult = await updateRedisCache("store-variables", (currentData) => {
if (currentData) {
currentData[setting] = newValue
}
return currentData
})
if (updateResult) {
await session.commitTransaction();
res.status(200).json(respond);
return;
}
}

await session.abortTransaction();
res.status(400).json(false);
} else {
const respond = await SettingsController.updateSetting(setting, newValue);
res.status(respond === undefined ? 400 : 200).json(respond);
}
}
)

0 comments on commit 6afb6e8

Please sign in to comment.