Skip to content

Commit

Permalink
cleanup cache update
Browse files Browse the repository at this point in the history
  • Loading branch information
philippdormann committed Sep 27, 2022
1 parent bf75ab9 commit b1dc484
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions api/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const cheerio = require('cheerio');
const axios = require('axios').default;
const institutions = require('../institutions.json');
// =========
process.env.CACHE_TIME_MINUTES = parseInt(process.env.CACHE_TIME_MINUTES || 1);
// =========
let redisclient = undefined;
if (process.env.CACHE === 'redis') {
if (process.env.CACHE_REDIS_URL) {
Expand All @@ -15,14 +17,25 @@ if (process.env.CACHE === 'redis') {
}
const mensaplanCache = [];
// =========
function getCacheItem(key, expiry) {
function updateCacheItem(key, data) {
if (process.env.CACHE === 'redis') {
redisclient.set(key, data, 'EX', process.env.CACHE_TIME_MINUTES * 60);
} else {
mensaplanCache.push({
ts: Date.now(),
data,
key
});
}
}
function getCacheItem(key) {
return new Promise(function (resolve, reject) {
if (process.env.CACHE === 'redis') {
redisclient
.get(key)
.then((result) => {
if (result) {
resolve({ content: result });
resolve({ data: result });
} else {
resolve(undefined);
}
Expand All @@ -31,13 +44,15 @@ function getCacheItem(key, expiry) {
resolve(undefined);
});
} else {
const expiry =
Date.now() - process.env.CACHE_TIME_MINUTES * 60 * 1000;
const cacheItem = mensaplanCache.find(
(i) => i.ts > expiry && i.key === key
);
if (cacheItem) {
resolve(cacheItem);
} else {
reject(expiry);
resolve(undefined);
}
}
});
Expand All @@ -59,38 +74,22 @@ function getMensaplanHTML({ p, e }) {
}
);
} else {
const cacheTime = parseInt(
process.env.CACHE_TIME_MINUTES || 1
);
getCacheItem(
`${p}${e}${found.provider}`,
Date.now() - cacheTime * 60 * 1000
)
getCacheItem(`${p}${e}${found.provider}`)
.then((cache) => {
if (cache) {
// serve from cache
resolve(cache.content);
resolve(cache.data);
} else {
// load fresh data
fetchHTML({
p,
e,
provider: found.provider
}).then((data) => {
if (process.env.CACHE === 'redis') {
redisclient.set(
`${p}${e}${found.provider}`,
data,
'EX',
cacheTime * 60
);
} else {
mensaplanCache.push({
ts: Date.now(),
content: data,
key: `${p}${e}${found.provider}`
});
}
updateCacheItem(
`${p}${e}${found.provider}`,
data
);
resolve(data);
});
}
Expand Down

1 comment on commit b1dc484

@vercel
Copy link

@vercel vercel bot commented on b1dc484 Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mensa-api – ./

mensa-api-git-main-philippdormann.vercel.app
mensa-api-philippdormann.vercel.app
mensa.vercel.app

Please sign in to comment.