You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.4 KiB
JavaScript
144 lines
3.4 KiB
JavaScript
4 years ago
|
'use strict';
|
||
|
|
||
|
module.exports = function (opts) {
|
||
|
const LRU = require('lru-cache');
|
||
|
const pubsub = require('./pubsub');
|
||
|
|
||
3 years ago
|
// lru-cache@7 deprecations
|
||
|
const winston = require('winston');
|
||
|
const chalk = require('chalk');
|
||
4 years ago
|
|
||
3 years ago
|
// sometimes we kept passing in `length` with no corresponding `maxSize`.
|
||
|
// This is now enforced in v7; drop superfluous property
|
||
|
if (opts.hasOwnProperty('length') && !opts.hasOwnProperty('maxSize')) {
|
||
|
winston.warn(`[cache/init(${opts.name})] ${chalk.white.bgRed.bold('DEPRECATION')} ${chalk.yellow('length')} was passed in without a corresponding ${chalk.yellow('maxSize')}. Both are now required as of [email protected].`);
|
||
|
delete opts.length;
|
||
|
}
|
||
|
|
||
|
const deprecations = new Map([
|
||
|
['stale', 'allowStale'],
|
||
|
['maxAge', 'ttl'],
|
||
|
['length', 'sizeCalculation'],
|
||
|
]);
|
||
|
deprecations.forEach((newProp, oldProp) => {
|
||
|
if (opts.hasOwnProperty(oldProp) && !opts.hasOwnProperty(newProp)) {
|
||
|
winston.warn(`[cache/init (${opts.name})] ${chalk.white.bgRed.bold('DEPRECATION')} The option ${chalk.yellow(oldProp)} has been deprecated as of [email protected]. Please change this to ${chalk.yellow(newProp)} instead.`);
|
||
|
opts[newProp] = opts[oldProp];
|
||
|
delete opts[oldProp];
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const lruCache = new LRU(opts);
|
||
|
|
||
|
const cache = {};
|
||
4 years ago
|
cache.name = opts.name;
|
||
|
cache.hits = 0;
|
||
|
cache.misses = 0;
|
||
|
cache.enabled = opts.hasOwnProperty('enabled') ? opts.enabled : true;
|
||
3 years ago
|
const cacheSet = lruCache.set;
|
||
4 years ago
|
|
||
3 years ago
|
// backwards compatibility
|
||
|
const propertyMap = new Map([
|
||
|
['length', 'calculatedSize'],
|
||
|
['max', 'max'],
|
||
|
['maxSize', 'maxSize'],
|
||
|
['itemCount', 'size'],
|
||
|
]);
|
||
|
propertyMap.forEach((lruProp, cacheProp) => {
|
||
|
Object.defineProperty(cache, cacheProp, {
|
||
|
get: function () {
|
||
|
return lruCache[lruProp];
|
||
|
},
|
||
|
configurable: true,
|
||
|
enumerable: true,
|
||
|
});
|
||
|
});
|
||
4 years ago
|
|
||
3 years ago
|
cache.set = function (key, value, ttl) {
|
||
4 years ago
|
if (!cache.enabled) {
|
||
|
return;
|
||
|
}
|
||
3 years ago
|
const opts = {};
|
||
|
if (ttl) {
|
||
|
opts.ttl = ttl;
|
||
|
}
|
||
|
cacheSet.apply(lruCache, [key, value, opts]);
|
||
4 years ago
|
};
|
||
|
|
||
|
cache.get = function (key) {
|
||
|
if (!cache.enabled) {
|
||
|
return undefined;
|
||
|
}
|
||
3 years ago
|
const data = lruCache.get(key);
|
||
4 years ago
|
if (data === undefined) {
|
||
|
cache.misses += 1;
|
||
|
} else {
|
||
|
cache.hits += 1;
|
||
|
}
|
||
|
return data;
|
||
|
};
|
||
|
|
||
|
cache.del = function (keys) {
|
||
|
if (!Array.isArray(keys)) {
|
||
|
keys = [keys];
|
||
|
}
|
||
4 years ago
|
pubsub.publish(`${cache.name}:cache:del`, keys);
|
||
3 years ago
|
keys.forEach(key => lruCache.delete(key));
|
||
4 years ago
|
};
|
||
3 years ago
|
cache.delete = cache.del;
|
||
4 years ago
|
|
||
|
cache.reset = function () {
|
||
4 years ago
|
pubsub.publish(`${cache.name}:cache:reset`);
|
||
4 years ago
|
localReset();
|
||
|
};
|
||
3 years ago
|
cache.clear = cache.reset;
|
||
4 years ago
|
|
||
|
function localReset() {
|
||
3 years ago
|
lruCache.clear();
|
||
4 years ago
|
cache.hits = 0;
|
||
|
cache.misses = 0;
|
||
|
}
|
||
|
|
||
4 years ago
|
pubsub.on(`${cache.name}:cache:reset`, () => {
|
||
4 years ago
|
localReset();
|
||
|
});
|
||
|
|
||
4 years ago
|
pubsub.on(`${cache.name}:cache:del`, (keys) => {
|
||
4 years ago
|
if (Array.isArray(keys)) {
|
||
3 years ago
|
keys.forEach(key => lruCache.delete(key));
|
||
4 years ago
|
}
|
||
|
});
|
||
|
|
||
|
cache.getUnCachedKeys = function (keys, cachedData) {
|
||
|
if (!cache.enabled) {
|
||
|
return keys;
|
||
|
}
|
||
|
let data;
|
||
|
let isCached;
|
||
4 years ago
|
const unCachedKeys = keys.filter((key) => {
|
||
4 years ago
|
data = cache.get(key);
|
||
|
isCached = data !== undefined;
|
||
|
if (isCached) {
|
||
|
cachedData[key] = data;
|
||
|
}
|
||
|
return !isCached;
|
||
|
});
|
||
|
|
||
4 years ago
|
const hits = keys.length - unCachedKeys.length;
|
||
|
const misses = keys.length - hits;
|
||
4 years ago
|
cache.hits += hits;
|
||
|
cache.misses += misses;
|
||
|
return unCachedKeys;
|
||
|
};
|
||
|
|
||
3 years ago
|
cache.dump = function () {
|
||
|
return lruCache.dump();
|
||
|
};
|
||
|
|
||
|
cache.peek = function (key) {
|
||
|
return lruCache.peek(key);
|
||
|
};
|
||
|
|
||
4 years ago
|
return cache;
|
||
|
};
|