mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-05 03:51:26 +01:00
Update to lru-cache@^7 (#10815)
* chore(deps): bump lru-cache from 6.0.0 to 7.13.1 in /install Bumps [lru-cache](https://github.com/isaacs/node-lru-cache) from 6.0.0 to 7.13.1. - [Release notes](https://github.com/isaacs/node-lru-cache/releases) - [Changelog](https://github.com/isaacs/node-lru-cache/blob/main/CHANGELOG.md) - [Commits](https://github.com/isaacs/node-lru-cache/compare/v6.0.0...v7.13.1) --- updated-dependencies: - dependency-name: lru-cache dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * fix(lru-cache): remove unneeded `length` params for cache creation, as `maxSize` was not used in those init calls, also renamed some methods to match new method names in lru-cache [breaking] Added deprecation notices for old params * fix: replace three direct calls to lru-cache with call to cacheCreate, moved cache creation call in uploads to run on first init as config is not populated at lib init * test: move configs init above cache reset calls in databasemock * move some more code above cache clear * refactor: remove unused * test: lru * test: more debug * test: on more test * use await helpers.uploadFile * fix: tests remove logs * fix: acp cache page * fix: add in one more guard again cache instantiation with `length` prop but no `maxSize` prop * fix(deps): bump markdown Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Barış Soner Uşaklı <barisusakli@gmail.com>
This commit is contained in:
@@ -4,30 +4,72 @@ module.exports = function (opts) {
|
||||
const LRU = require('lru-cache');
|
||||
const pubsub = require('./pubsub');
|
||||
|
||||
const cache = new LRU(opts);
|
||||
// lru-cache@7 deprecations
|
||||
const winston = require('winston');
|
||||
const chalk = require('chalk');
|
||||
|
||||
// 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 lru-cache@7.0.0.`);
|
||||
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 lru-cache@7.0.0. Please change this to ${chalk.yellow(newProp)} instead.`);
|
||||
opts[newProp] = opts[oldProp];
|
||||
delete opts[oldProp];
|
||||
}
|
||||
});
|
||||
|
||||
const lruCache = new LRU(opts);
|
||||
|
||||
const cache = {};
|
||||
cache.name = opts.name;
|
||||
cache.hits = 0;
|
||||
cache.misses = 0;
|
||||
cache.enabled = opts.hasOwnProperty('enabled') ? opts.enabled : true;
|
||||
const cacheSet = lruCache.set;
|
||||
|
||||
const cacheSet = cache.set;
|
||||
const cacheGet = cache.get;
|
||||
const cacheDel = cache.del;
|
||||
const cacheReset = cache.reset;
|
||||
// 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,
|
||||
});
|
||||
});
|
||||
|
||||
cache.set = function (key, value, maxAge) {
|
||||
cache.set = function (key, value, ttl) {
|
||||
if (!cache.enabled) {
|
||||
return;
|
||||
}
|
||||
cacheSet.apply(cache, [key, value, maxAge]);
|
||||
const opts = {};
|
||||
if (ttl) {
|
||||
opts.ttl = ttl;
|
||||
}
|
||||
cacheSet.apply(lruCache, [key, value, opts]);
|
||||
};
|
||||
|
||||
cache.get = function (key) {
|
||||
if (!cache.enabled) {
|
||||
return undefined;
|
||||
}
|
||||
const data = cacheGet.apply(cache, [key]);
|
||||
const data = lruCache.get(key);
|
||||
if (data === undefined) {
|
||||
cache.misses += 1;
|
||||
} else {
|
||||
@@ -41,16 +83,18 @@ module.exports = function (opts) {
|
||||
keys = [keys];
|
||||
}
|
||||
pubsub.publish(`${cache.name}:cache:del`, keys);
|
||||
keys.forEach(key => cacheDel.apply(cache, [key]));
|
||||
keys.forEach(key => lruCache.delete(key));
|
||||
};
|
||||
cache.delete = cache.del;
|
||||
|
||||
cache.reset = function () {
|
||||
pubsub.publish(`${cache.name}:cache:reset`);
|
||||
localReset();
|
||||
};
|
||||
cache.clear = cache.reset;
|
||||
|
||||
function localReset() {
|
||||
cacheReset.apply(cache);
|
||||
lruCache.clear();
|
||||
cache.hits = 0;
|
||||
cache.misses = 0;
|
||||
}
|
||||
@@ -61,7 +105,7 @@ module.exports = function (opts) {
|
||||
|
||||
pubsub.on(`${cache.name}:cache:del`, (keys) => {
|
||||
if (Array.isArray(keys)) {
|
||||
keys.forEach(key => cacheDel.apply(cache, [key]));
|
||||
keys.forEach(key => lruCache.delete(key));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -87,5 +131,13 @@ module.exports = function (opts) {
|
||||
return unCachedKeys;
|
||||
};
|
||||
|
||||
cache.dump = function () {
|
||||
return lruCache.dump();
|
||||
};
|
||||
|
||||
cache.peek = function (key) {
|
||||
return lruCache.peek(key);
|
||||
};
|
||||
|
||||
return cache;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user