-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create a function to update data in redis cache
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
1 parent
94b730d
commit 6afb6e8
Showing
3 changed files
with
39 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
) |