Skip to content

Commit

Permalink
disable cache for now
Browse files Browse the repository at this point in the history
  • Loading branch information
boutell committed May 3, 2022
1 parent 0ecd681 commit 2c8b07a
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const renderer = require(appPath).default;
const pageSize = 20;
const versionFile = `${__dirname}/../version.txt`;
const version = fs.existsSync(versionFile) ? fs.readFileSync(versionFile, 'utf8') : '0';
const cache = {};

// Disabled for now because it causes confusion when we update the data
// const cache = {};

module.exports = async function({ plants, nurseries }) {
const port = process.env.PORT || 3000;
Expand All @@ -20,10 +22,6 @@ module.exports = async function({ plants, nurseries }) {
app.use(express.static(publicDir));
app.use('/images', express.static(`${__dirname}/../images`));
app.get('/api/v1/plants', async (req, res) => {
const data = getCache(req);
if (data) {
return res.send(data);
}
try {
const fetchResults = req.query.results !== '0';
const fetchTotal = req.query.total !== '0';
Expand Down Expand Up @@ -323,7 +321,7 @@ module.exports = async function({ plants, nurseries }) {
response.counts[filter.name] = Object.fromEntries(Object.entries(counts).filter((value, count) => !filter.ignore.includes(value)));
}
}
setCache(req, response);
// setCache(req, response);
return res.send(response);
} catch (e) {
console.error('error:', e);
Expand Down Expand Up @@ -381,35 +379,35 @@ module.exports = async function({ plants, nurseries }) {
return app;
};

function hashKey(req) {
return JSON.stringify(req.query);
}
// function hashKey(req) {
// return JSON.stringify(req.query);
// }

function getCache(req) {
const key = hashKey(req);
if (cache[key]) {
cache[key].last = Date.now();
return cache[key].data;
}
}
// function getCache(req) {
// const key = hashKey(req);
// if (cache[key]) {
// cache[key].last = Date.now();
// return cache[key].data;
// }
// }

function setCache(req, data) {
cache[hashKey(req)] = {
last: Date.now(),
data
};
if (Object.keys(cache).length > 100) {
const oldest = Object.entries(cache, (a, [key, value]) => {
if ((!a) || (value.last < a.last)) {
return [key, value];
}
}, null);
if (oldest) {
// console.log('Deleting:', oldest[0]);
delete cache[oldest[0]];
}
}
// Object.keys(cache).map(key => {
// console.log(`${key} ${cache[key].last}`);
// });
}
// function setCache(req, data) {
// cache[hashKey(req)] = {
// last: Date.now(),
// data
// };
// if (Object.keys(cache).length > 100) {
// const oldest = Object.entries(cache, (a, [key, value]) => {
// if ((!a) || (value.last < a.last)) {
// return [key, value];
// }
// }, null);
// if (oldest) {
// // console.log('Deleting:', oldest[0]);
// delete cache[oldest[0]];
// }
// }
// // Object.keys(cache).map(key => {
// // console.log(`${key} ${cache[key].last}`);
// // });
// }

0 comments on commit 2c8b07a

Please sign in to comment.