* feat: #8824, cache refactor ability to disable caches ability to download contents of cache refactor cache modules to remove duplicated code * fix: remove duplicate hit/miss tracking check cacheEnabled in getUncachedKeysv1.18.x
parent
bcbc085497
commit
f1f9b225b0
@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"post-cache": "Post Cache",
|
"post-cache": "Post Cache",
|
||||||
"posts-in-cache": "Posts in Cache",
|
|
||||||
"average-post-size": "Average Post Size",
|
|
||||||
"length-to-max": "Length / Max",
|
|
||||||
"percent-full": "%1% Full",
|
"percent-full": "%1% Full",
|
||||||
"post-cache-size": "Post Cache Size",
|
"post-cache-size": "Post Cache Size",
|
||||||
"items-in-cache": "Items in Cache",
|
"items-in-cache": "Items in Cache",
|
||||||
"control-panel": "Control Panel",
|
"control-panel": "Control Panel",
|
||||||
"update-settings": "Update Cache Settings"
|
"update-settings": "Update Cache Settings",
|
||||||
|
"clear": "Clear",
|
||||||
|
"download": "Download",
|
||||||
|
"enabled": "Enabled"
|
||||||
}
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = function (opts) {
|
||||||
|
const LRU = require('lru-cache');
|
||||||
|
const pubsub = require('./pubsub');
|
||||||
|
|
||||||
|
const cache = new LRU(opts);
|
||||||
|
|
||||||
|
cache.name = opts.name;
|
||||||
|
cache.hits = 0;
|
||||||
|
cache.misses = 0;
|
||||||
|
cache.enabled = opts.hasOwnProperty('enabled') ? opts.enabled : true;
|
||||||
|
|
||||||
|
const cacheSet = cache.set;
|
||||||
|
const cacheGet = cache.get;
|
||||||
|
const cacheDel = cache.del;
|
||||||
|
const cacheReset = cache.reset;
|
||||||
|
|
||||||
|
cache.set = function (key, value) {
|
||||||
|
if (!cache.enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cacheSet.apply(cache, [key, value]);
|
||||||
|
};
|
||||||
|
|
||||||
|
cache.get = function (key) {
|
||||||
|
if (!cache.enabled) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const data = cacheGet.apply(cache, [key]);
|
||||||
|
if (data === undefined) {
|
||||||
|
cache.misses += 1;
|
||||||
|
} else {
|
||||||
|
cache.hits += 1;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
cache.del = function (keys) {
|
||||||
|
if (!Array.isArray(keys)) {
|
||||||
|
keys = [keys];
|
||||||
|
}
|
||||||
|
pubsub.publish(cache.name + ':cache:del', keys);
|
||||||
|
keys.forEach(key => cacheDel.apply(cache, [key]));
|
||||||
|
};
|
||||||
|
|
||||||
|
cache.reset = function () {
|
||||||
|
pubsub.publish(cache.name + ':cache:reset');
|
||||||
|
localReset();
|
||||||
|
};
|
||||||
|
|
||||||
|
function localReset() {
|
||||||
|
cacheReset.apply(cache);
|
||||||
|
cache.hits = 0;
|
||||||
|
cache.misses = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pubsub.on(cache.name + ':cache:reset', function () {
|
||||||
|
localReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
pubsub.on(cache.name + ':cache:del', function (keys) {
|
||||||
|
if (Array.isArray(keys)) {
|
||||||
|
keys.forEach(key => cacheDel.apply(cache, [key]));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
cache.getUnCachedKeys = function (keys, cachedData) {
|
||||||
|
if (!cache.enabled) {
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
let data;
|
||||||
|
let isCached;
|
||||||
|
const unCachedKeys = keys.filter(function (key) {
|
||||||
|
data = cache.get(key);
|
||||||
|
isCached = data !== undefined;
|
||||||
|
if (isCached) {
|
||||||
|
cachedData[key] = data;
|
||||||
|
}
|
||||||
|
return !isCached;
|
||||||
|
});
|
||||||
|
|
||||||
|
var hits = keys.length - unCachedKeys.length;
|
||||||
|
var misses = keys.length - hits;
|
||||||
|
cache.hits += hits;
|
||||||
|
cache.misses += misses;
|
||||||
|
return unCachedKeys;
|
||||||
|
};
|
||||||
|
|
||||||
|
return cache;
|
||||||
|
};
|
@ -1,56 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports.create = function (name) {
|
module.exports.create = function (name) {
|
||||||
var LRU = require('lru-cache');
|
const cacheCreate = require('../cacheCreate');
|
||||||
var pubsub = require('../pubsub');
|
return cacheCreate({
|
||||||
|
name: name + '-object',
|
||||||
var cache = new LRU({
|
|
||||||
max: 40000,
|
max: 40000,
|
||||||
length: function () { return 1; },
|
length: function () { return 1; },
|
||||||
maxAge: 0,
|
maxAge: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
cache.misses = 0;
|
|
||||||
cache.hits = 0;
|
|
||||||
|
|
||||||
pubsub.on(name + ':hash:cache:del', function (keys) {
|
|
||||||
keys.forEach(key => cache.del(key));
|
|
||||||
});
|
|
||||||
|
|
||||||
pubsub.on(name + ':hash:cache:reset', function () {
|
|
||||||
cache.reset();
|
|
||||||
});
|
|
||||||
|
|
||||||
cache.delObjectCache = function (keys) {
|
|
||||||
if (!Array.isArray(keys)) {
|
|
||||||
keys = [keys];
|
|
||||||
}
|
|
||||||
pubsub.publish(name + ':hash:cache:del', keys);
|
|
||||||
keys.forEach(key => cache.del(key));
|
|
||||||
};
|
|
||||||
|
|
||||||
cache.resetObjectCache = function () {
|
|
||||||
pubsub.publish(name + ':hash:cache:reset');
|
|
||||||
cache.reset();
|
|
||||||
};
|
|
||||||
|
|
||||||
cache.getUnCachedKeys = function (keys, cachedData) {
|
|
||||||
let data;
|
|
||||||
let isCached;
|
|
||||||
const unCachedKeys = keys.filter(function (key) {
|
|
||||||
data = cache.get(key);
|
|
||||||
isCached = data !== undefined;
|
|
||||||
if (isCached) {
|
|
||||||
cachedData[key] = data;
|
|
||||||
}
|
|
||||||
return !isCached;
|
|
||||||
});
|
|
||||||
|
|
||||||
var hits = keys.length - unCachedKeys.length;
|
|
||||||
var misses = keys.length - hits;
|
|
||||||
cache.hits += hits;
|
|
||||||
cache.misses += misses;
|
|
||||||
return unCachedKeys;
|
|
||||||
};
|
|
||||||
return cache;
|
|
||||||
};
|
};
|
||||||
|
@ -1,48 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var LRU = require('lru-cache');
|
const cacheCreate = require('../cacheCreate');
|
||||||
var pubsub = require('../pubsub');
|
|
||||||
|
|
||||||
var cache = new LRU({
|
module.exports = function (Groups) {
|
||||||
|
Groups.cache = cacheCreate({
|
||||||
|
name: 'group',
|
||||||
max: 40000,
|
max: 40000,
|
||||||
maxAge: 0,
|
maxAge: 0,
|
||||||
});
|
});
|
||||||
cache.hits = 0;
|
|
||||||
cache.misses = 0;
|
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
|
||||||
Groups.cache = cache;
|
|
||||||
|
|
||||||
pubsub.on('group:cache:reset', function () {
|
|
||||||
localReset();
|
|
||||||
});
|
|
||||||
|
|
||||||
pubsub.on('group:cache:del', function (data) {
|
|
||||||
if (data && data.groupNames) {
|
|
||||||
data.groupNames.forEach(function (groupName) {
|
|
||||||
cache.del(data.uid + ':' + groupName);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Groups.resetCache = function () {
|
|
||||||
pubsub.publish('group:cache:reset');
|
|
||||||
localReset();
|
|
||||||
};
|
|
||||||
|
|
||||||
function localReset() {
|
|
||||||
cache.reset();
|
|
||||||
cache.hits = 0;
|
|
||||||
cache.misses = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Groups.clearCache = function (uid, groupNames) {
|
Groups.clearCache = function (uid, groupNames) {
|
||||||
if (!Array.isArray(groupNames)) {
|
if (!Array.isArray(groupNames)) {
|
||||||
groupNames = [groupNames];
|
groupNames = [groupNames];
|
||||||
}
|
}
|
||||||
pubsub.publish('group:cache:del', { uid: uid, groupNames: groupNames });
|
const keys = groupNames.map(name => uid + ':' + name);
|
||||||
groupNames.forEach(function (groupName) {
|
Groups.cache.del(keys);
|
||||||
cache.del(uid + ':' + groupName);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue