From 839aeda0ba3e5d99ac54c0c1e8504dc768beacf1 Mon Sep 17 00:00:00 2001 From: itsayushpandey Date: Wed, 24 Apr 2024 23:01:25 -0600 Subject: [PATCH] Added redis cache for closestCrimeAPI --- Dockerfile | 2 +- fetchCrimeData.js | 91 +++++++++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1367cdc..5434605 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,4 +16,4 @@ RUN npm install EXPOSE 3000 # Command to run backend server (adjust as per your backend setup) -CMD ["node", "fetchCrimeData.js"] +CMD ["node","--env-file=.env", "fetchCrimeData.js"] diff --git a/fetchCrimeData.js b/fetchCrimeData.js index 06b14b8..11ace71 100644 --- a/fetchCrimeData.js +++ b/fetchCrimeData.js @@ -135,50 +135,67 @@ app.post("/closestCrimeData", async (req, res) => { const closestCrimeDataList = []; - const crimeDataCursor = db.collection("boulderCrimeData").find(); + const cacheKey = objectHash.sha1([req.latitude, req.longitude]); + console.log("Cached key" + cacheKey); + cachedValue = await readData(cacheKey); + console.log("Cached value found: " + cachedValue); + if (cachedValue) { + // No need to call DB as we have values in cache and can skip it. + const responseStatus = new ResponseStatus(200, "OK", "Success"); + res.json(new Response(closestCrimeDataList, responseStatus)); + } else { + const crimeDataCursor = db.collection("boulderCrimeData").find(); - const allCrimeData = await crimeDataCursor.toArray(); + const allCrimeData = await crimeDataCursor.toArray(); - const routeProcessingPromises = routeList.map(async (route) => { - const closestCrimeData = []; + const routeProcessingPromises = routeList.map(async (route) => { + const closestCrimeData = []; - for (const [targetLat, targetLong] of route) { - let closestCrime = null; - let minDistance = Infinity; + for (const [targetLat, targetLong] of route) { + let closestCrime = null; + let minDistance = Infinity; - for (const crime of allCrimeData) { - const distance = calculateDistance( - targetLat, - targetLong, - crime.latitude, - crime.longitude, - ); - if (distance < minDistance) { - minDistance = distance; - closestCrime = crime; + for (const crime of allCrimeData) { + const distance = calculateDistance( + targetLat, + targetLong, + crime.latitude, + crime.longitude, + ); + if (distance < minDistance) { + minDistance = distance; + closestCrime = crime; + } } - } - if (closestCrime) { - closestCrimeData.push({ - latitude: targetLat, - longitude: targetLong, - crimeData: closestCrime.crimeData, - }); - } else { - closestCrimeData.push({ - message: "No crime data found near this location.", - }); + if (closestCrime) { + closestCrimeData.push({ + latitude: targetLat, + longitude: targetLong, + crimeData: closestCrime.crimeData, + }); + } else { + closestCrimeData.push({ + message: "No crime data found near this location.", + }); + } } - } - closestCrimeDataList.push(closestCrimeData); - }); + closestCrimeDataList.push(closestCrimeData); + }); - await Promise.all(routeProcessingPromises); - await client.close(); + await Promise.all(routeProcessingPromises); + await client.close(); - const responseStatus = new ResponseStatus(200, "OK", "Success"); - res.json(new Response(closestCrimeDataList, responseStatus)); + console.log( + "Saving in cache" + + cacheKey + + ", value= " + + crimeDataResponse.data.crimes, + ); + writeData(cacheKey, crimeDataResponse.data.crimes); + const responseStatus = new ResponseStatus(200, "OK", "Success"); + res.json(new Response(closestCrimeDataList, responseStatus)); + } } catch (err) { console.error(err); ResponseUtils.setResponseError(res, 500, "Internal Server Error"); @@ -243,10 +260,6 @@ app.post("/closestCrimeData1", async (req, res) => { } }); -// app.listen(port, () => { -// console.log(`Server is running on http://localhost:${port}`); -// }); - async function initializeExpressServer() { //initialize an Express application app.use(express.json());