Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made a in memory cache module and added it to /tasks endpoint #713

Merged
merged 4 commits into from
Sep 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed variable names changes to be more descriptive
  • Loading branch information
yash committed Sep 21, 2022
commit ab3d6685cf0dd6e7a3c514dbefdf2369fb788f5c
26 changes: 13 additions & 13 deletions utils/cache.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const CACHEEXPIRYTIME = 2; // Default cache expriy time in Minutes
const CACHESIZE = 10; // Default maximum size for cache pool (in Megabytes)
const minutesToMilliseconds = (minutes) => minutes * 60000; // Converts given minutes into milliseconds
const CACHE_EXPIRY_TIME_MIN = 2;
const CACHE_SIZE_MB = 10;
const minutesToMilliseconds = (minutes) => minutes * 60000;

/**
* Cache pool to get and store API responses
* @param {Object} [opt] options for cache pool
* @param {number} opt.maximumSize Maximum size of the cache pool in Megabytes (MB)
*/
const cachePool = (opt = { maximumSize: CACHESIZE }) => {
const cachePool = (opt = { maximumSize: CACHE_SIZE_MB }) => {
const cacheStore = new Map();
let hits = 0;

Expand All @@ -17,22 +17,22 @@ const cachePool = (opt = { maximumSize: CACHESIZE }) => {
* @returns {null | object}
*/
const get = (key) => {
const apiData = cacheStore.get(key);
const cachedData = cacheStore.get(key);

if (!apiData) {
if (!cachedData) {
return null;
}

const isApiDataExpired = new Date().getTime() > apiData.expiry;
const isCacheDataExpired = new Date().getTime() > cachedData.expiry;

// If data is expired remove it from store, time to get a fresh copy.
if (isApiDataExpired) {
if (isCacheDataExpired) {
cacheStore.delete(key);
yesyash marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

hits += 1;
return apiData.response;
return cachedData.response;
};

/**
Expand Down Expand Up @@ -66,13 +66,13 @@ const pool = cachePool();
* @param {number} data.expiry Cache expiry time of api in minutes.
* @returns {function} middleware function to help cache api response.
*/
const cache = (data = { priority: 2, expiry: CACHEEXPIRYTIME }) => {
const cache = (data = { priority: 2, expiry: CACHE_EXPIRY_TIME_MIN }) => {
return async (req, res, next) => {
yesyash marked this conversation as resolved.
Show resolved Hide resolved
const key = "__cache__" + req.method + req.originalUrl;
const dataInCache = pool.get(key);
const cacheData = pool.get(key);

if (dataInCache) {
res.send(dataInCache);
if (cacheData) {
res.send(cacheData);
} else {
/**
* As we do not have data in our cache we call the next middleware,
Expand Down