2018-12-17 23:57:44 +00:00
|
|
|
const LRUCache = require('lru-cache');
|
2018-07-06 17:53:16 +00:00
|
|
|
|
2018-12-17 23:57:44 +00:00
|
|
|
const maxMegabytes = 40; // Cap the cache at 40 MB
|
|
|
|
const maxLength = maxMegabytes * 1024 * 1024;
|
2018-07-06 17:53:16 +00:00
|
|
|
|
2018-12-17 23:57:44 +00:00
|
|
|
const maxSeconds = 60;
|
|
|
|
const maxAge = maxSeconds * 1000;
|
2018-07-06 17:53:16 +00:00
|
|
|
|
2018-12-17 23:57:44 +00:00
|
|
|
const cache = new LRUCache({
|
|
|
|
max: maxLength,
|
|
|
|
maxAge: maxAge,
|
|
|
|
length: Buffer.byteLength
|
|
|
|
});
|
|
|
|
|
|
|
|
function get(key) {
|
|
|
|
return cache.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setex(key, ttlSeconds, value) {
|
|
|
|
return cache.set(key, value, ttlSeconds * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { get, setex };
|