mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
fix: race condition that results in 2x localReset call the same process
This commit is contained in:
12
src/cache/lru.js
vendored
12
src/cache/lru.js
vendored
@@ -2,6 +2,8 @@
|
||||
|
||||
module.exports = function (opts) {
|
||||
const { LRUCache } = require('lru-cache');
|
||||
const os = require('os');
|
||||
|
||||
const pubsub = require('../pubsub');
|
||||
|
||||
// lru-cache@7 deprecations
|
||||
@@ -91,7 +93,9 @@ module.exports = function (opts) {
|
||||
cache.delete = cache.del;
|
||||
|
||||
cache.reset = function () {
|
||||
pubsub.publish(`${cache.name}:lruCache:reset`);
|
||||
pubsub.publish(`${cache.name}:lruCache:reset`, {
|
||||
id: `${os.hostname()}:${process.pid}`,
|
||||
});
|
||||
localReset();
|
||||
};
|
||||
cache.clear = cache.reset;
|
||||
@@ -102,8 +106,10 @@ module.exports = function (opts) {
|
||||
cache.misses = 0;
|
||||
}
|
||||
|
||||
pubsub.on(`${cache.name}:lruCache:reset`, () => {
|
||||
localReset();
|
||||
pubsub.on(`${cache.name}:lruCache:reset`, ({ id }) => {
|
||||
if (id !== `${os.hostname()}:${process.pid}`) {
|
||||
localReset();
|
||||
}
|
||||
});
|
||||
|
||||
pubsub.on(`${cache.name}:lruCache:del`, (keys) => {
|
||||
|
||||
12
src/cache/ttl.js
vendored
12
src/cache/ttl.js
vendored
@@ -2,6 +2,8 @@
|
||||
|
||||
module.exports = function (opts) {
|
||||
const TTLCache = require('@isaacs/ttlcache');
|
||||
const os = require('os');
|
||||
|
||||
const pubsub = require('../pubsub');
|
||||
|
||||
const ttlCache = new TTLCache(opts);
|
||||
@@ -72,7 +74,9 @@ module.exports = function (opts) {
|
||||
cache.delete = cache.del;
|
||||
|
||||
cache.reset = function () {
|
||||
pubsub.publish(`${cache.name}:ttlCache:reset`);
|
||||
pubsub.publish(`${cache.name}:ttlCache:reset`, {
|
||||
id: `${os.hostname()}:${process.pid}`,
|
||||
});
|
||||
localReset();
|
||||
};
|
||||
cache.clear = cache.reset;
|
||||
@@ -83,8 +87,10 @@ module.exports = function (opts) {
|
||||
cache.misses = 0;
|
||||
}
|
||||
|
||||
pubsub.on(`${cache.name}:ttlCache:reset`, () => {
|
||||
localReset();
|
||||
pubsub.on(`${cache.name}:ttlCache:reset`, ({ id }) => {
|
||||
if (id !== `${os.hostname()}:${process.pid}`) {
|
||||
localReset();
|
||||
}
|
||||
});
|
||||
|
||||
pubsub.on(`${cache.name}:ttlCache:del`, (keys) => {
|
||||
|
||||
Reference in New Issue
Block a user