@@ -11,8 +11,14 @@ const {
1111 getCodeBaseFilesRating,
1212} = require ( '../../src/engine' )
1313const { 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 **/
3440router . 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} )
0 commit comments