Skip to content

Commit 80ab72b

Browse files
committed
added getter and setter functions for cache
1 parent fea80fe commit 80ab72b

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

routes/api/API.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ const {
1111
getCodeBaseFilesRating,
1212
} = require('../../src/engine')
1313
const {authenticateAPI} = require('../../src/auth')
14-
15-
let {cache} = require('../../src/cache')
14+
const {
15+
checkRandomCodesCacheExpiry,
16+
getRandomCodesFromCache,
17+
setRandomCodesInCache,
18+
checkSearchUserCacheExpiry,
19+
getSearchUserFromCache,
20+
setSearchUserInCache
21+
} = require('../../src/cache')
1622

1723
/**
1824
* @endpoint /api/v1/randomCodes
@@ -33,12 +39,8 @@ let {cache} = require('../../src/cache')
3339
**/
3440
router.get('/randomCodes', async (req, res) => {
3541
// return the cache data if present
36-
if (
37-
cache.has('randomCodes') &&
38-
cache.get('randomCodes').time >
39-
Date.now() - process.env.CACHE_STORAGE_SECONDS * 1000
40-
) {
41-
return res.status(200).json(cache.get('randomCodes').data)
42+
if (checkRandomCodesCacheExpiry()) {
43+
return res.status(200).json(getRandomCodesFromCache())
4244
}
4345

4446
const count = await getCodeBaseFilesCount()
@@ -72,7 +74,7 @@ router.get('/randomCodes', async (req, res) => {
7274
)
7375

7476
// setting the cache data with time
75-
cache.set('randomCodes', {
77+
setRandomCodesInCache({
7678
time: Date.now(),
7779
data: {
7880
status: 200,
@@ -94,7 +96,7 @@ router.get('/randomCodes', async (req, res) => {
9496
},
9597
})
9698

97-
res.status(200).json(cache.get('randomCodes').data)
99+
res.status(200).json(getRandomCodesFromCache())
98100
} else {
99101
res.status(404).json({
100102
status: 404,
@@ -195,14 +197,8 @@ router.get('/searchUser', authenticateAPI, async (req, res) => {
195197
})
196198
} else {
197199
// send the cache data according to username and sendContent
198-
if (
199-
cache.has('searchUser') &&
200-
cache.get('searchUser').time >
201-
Date.now() - process.env.CACHE_STORAGE_SECONDS * 1000 &&
202-
cache.get('searchUser').username === username &&
203-
cache.get('searchUser').sendContent === sendContent
204-
) {
205-
return res.status(200).json(cache.get('searchUser').data)
200+
if (checkSearchUserCacheExpiry(username, sendContent)) {
201+
return res.status(200).json(getSearchUserFromCache())
206202
}
207203

208204
let userCodeBaseFiles = await getCodeBaseFileForUser(username).catch(
@@ -231,7 +227,7 @@ router.get('/searchUser', authenticateAPI, async (req, res) => {
231227
})
232228
Promise.all(userCodeBaseFiles).then((data) => {
233229
// setting the cache
234-
cache.set('searchUser', {
230+
setSearchUserInCache({
235231
data: {
236232
status: 200,
237233
userCodeBaseFiles: data,
@@ -240,10 +236,10 @@ router.get('/searchUser', authenticateAPI, async (req, res) => {
240236
username,
241237
sendContent,
242238
})
243-
res.status(200).json(cache.get('searchUser').data)
239+
res.status(200).json(getSearchUserFromCache())
244240
})
245241
} else {
246-
cache.set('searchUser', {
242+
setSearchUserInCache({
247243
data: {
248244
status: 200,
249245
userCodeBaseFiles,
@@ -252,7 +248,7 @@ router.get('/searchUser', authenticateAPI, async (req, res) => {
252248
username,
253249
sendContent,
254250
})
255-
res.status(200).json(cache.get('searchUser').data)
251+
res.status(200).json(getSearchUserFromCache())
256252
}
257253
}
258254
})

src/cache.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,41 @@ cache.set('searchUser', {
99
username: '',
1010
})
1111

12-
module.exports = {cache}
12+
function checkRandomCodesCacheExpiry(){
13+
return cache.has('randomCodes') &&
14+
cache.get('randomCodes').time >
15+
Date.now() - process.env.CACHE_STORAGE_SECONDS * 1000
16+
}
17+
18+
function getRandomCodesFromCache(){
19+
return cache.get('randomCodes').data;
20+
}
21+
22+
function setRandomCodesInCache(object){
23+
cache.set('randomCodes', object);
24+
}
25+
26+
function checkSearchUserCacheExpiry(username, sendContent){
27+
return cache.has('searchUser') &&
28+
cache.get('searchUser').time >
29+
Date.now() - process.env.CACHE_STORAGE_SECONDS * 1000 &&
30+
cache.get('searchUser').username === username &&
31+
cache.get('searchUser').sendContent === sendContent
32+
}
33+
34+
function getSearchUserFromCache(){
35+
return cache.get('searchUser').data;
36+
}
37+
38+
function setSearchUserInCache(object){
39+
cache.set('searchUser', object);
40+
}
41+
42+
module.exports = {
43+
checkRandomCodesCacheExpiry,
44+
getRandomCodesFromCache,
45+
setRandomCodesInCache,
46+
checkSearchUserCacheExpiry,
47+
getSearchUserFromCache,
48+
setSearchUserInCache
49+
}

0 commit comments

Comments
 (0)