Skip to content

Commit a7cbcb5

Browse files
committed
feat: implement project caching by ID and refactor data deletion
- Add setProjectById and getProjectById to redisCaching service - Use project ID caching in getSingleProject controller - Refactor deleteData to use updateOne (Note: verify logic for vs ) - Remove console.time logs
1 parent 513fbf0 commit a7cbcb5

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

backend/controllers/data.controller.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,11 @@ module.exports.deleteSingleDoc = async (req, res) => {
178178
await Model.deleteOne({ _id: id });
179179

180180
if (!project.resources.db.isExternal) {
181-
project.databaseUsed = Math.max(0, (project.databaseUsed || 0) - docSize);
182-
await project.save();
181+
let databaseUsed = Math.max(0, (project.databaseUsed || 0) - docSize);
182+
await Project.updateOne(
183+
{ _id: project._id },
184+
{ $inc: { databaseUsed } }
185+
);
183186
}
184187

185188
res.json({ message: "Document deleted", id });

backend/controllers/project.controller.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { URL } = require('url');
1111
const { getConnection } = require("../utils/connection.manager");
1212
const { getCompiledModel } = require("../utils/injectModel")
1313
const { storageRegistry } = require("../utils/registry");
14-
const { deleteProjectByApiKeyCache } = require("../services/redisCaching");
14+
const { deleteProjectByApiKeyCache, setProjectById, getProjectById } = require("../services/redisCaching");
1515

1616

1717

@@ -68,10 +68,17 @@ module.exports.getAllProject = async (req, res) => {
6868

6969
module.exports.getSingleProject = async (req, res) => {
7070
try {
71-
const project = await Project.findOne({ _id: req.params.projectId, owner: req.user._id }).select('-apiKey -jwtSecret');
72-
if (!project) return res.status(404).json({ error: "Project not found." });
71+
let project;
72+
project = await getProjectById(req.params.projectId);
73+
let projectObj;
74+
if (!project) {
75+
project = await Project.findOne({ _id: req.params.projectId, owner: req.user._id }).select('-apiKey -jwtSecret');
76+
if (!project) return res.status(404).json({ error: "Project not found." });
77+
projectObj = project.toObject();
78+
await setProjectById(req.params.projectId, project);
79+
}
7380

74-
const projectObj = project.toObject();
81+
projectObj = project;
7582
delete projectObj.apiKey;
7683
delete projectObj.jwtSecret;
7784
res.json(projectObj);
@@ -85,6 +92,10 @@ module.exports.regenerateApiKey = async (req, res) => {
8592
const newApiKey = generateApiKey();
8693
const hashed = hashApiKey(newApiKey);
8794

95+
const oldApiProj = await Project.findOne({ _id: req.params.projectId, owner: req.user._id }).select('apiKey');
96+
if (!oldApiProj) return res.status(404).json({ error: "Project not found." });
97+
await deleteProjectByApiKeyCache(oldApiProj.apiKey);
98+
8899

89100
const project = await Project.findOneAndUpdate(
90101
{ _id: req.params.projectId, owner: req.user._id },

backend/services/redisCaching.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ const redis = require("../config/redis");
22

33
async function setProjectByApiKeyCache(api, project) {
44
try {
5-
console.time("json");
65
const data = JSON.stringify(project);
7-
console.timeEnd("json");
86
await redis.set(
97
`project:apikey:${api}`,
108
data,
@@ -20,9 +18,8 @@ async function getProjectByApiKeyCache(api) {
2018
try {
2119
const data = await redis.get(`project:apikey:${api}`);
2220
if (!data) return null;
23-
console.time("json parse")
21+
2422
const parsedData = JSON.parse(data)
25-
console.timeEnd("json parse")
2623
return parsedData;
2724
} catch (err) {
2825
console.log(err);
@@ -38,8 +35,39 @@ async function deleteProjectByApiKeyCache(api) {
3835
}
3936
}
4037

38+
39+
async function setProjectById(id, project) {
40+
try {
41+
const data = JSON.stringify(project);
42+
await redis.set(
43+
`project:id:${id}`,
44+
data,
45+
'EX',
46+
60 * 60 * 2
47+
);
48+
} catch (err) {
49+
console.log(err);
50+
}
51+
}
52+
53+
54+
async function getProjectById(id) {
55+
try {
56+
const data = await redis.get(`project:id:${id}`);
57+
if (!data) return null;
58+
const parsedData = JSON.parse(data)
59+
return parsedData;
60+
} catch (err) {
61+
console.log(err);
62+
return null;
63+
}
64+
}
65+
66+
4167
module.exports = {
4268
setProjectByApiKeyCache,
4369
getProjectByApiKeyCache,
44-
deleteProjectByApiKeyCache
70+
deleteProjectByApiKeyCache,
71+
setProjectById,
72+
getProjectById
4573
};

0 commit comments

Comments
 (0)