mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-30 11:28:19 +03:00
Add ttl cache
This commit is contained in:
@@ -76,6 +76,12 @@ const cache = config.redis.clientCacheSize ? new LRUCache<RedisCommandArgument,
|
|||||||
ttl: 1000 * 60 * 30,
|
ttl: 1000 * 60 * 30,
|
||||||
ttlResolution: 1000 * 60 * 15
|
ttlResolution: 1000 * 60 * 15
|
||||||
}) : null;
|
}) : null;
|
||||||
|
// Used to cache ttl data
|
||||||
|
const ttlCache = config.redis.clientCacheSize ? new LRUCache<RedisCommandArgument, number>({
|
||||||
|
max: config.redis.clientCacheSize / 10 / 4, // 4 byte integer per element
|
||||||
|
ttl: 1000 * 60 * 30,
|
||||||
|
ttlResolution: 1000 * 60 * 15
|
||||||
|
}) : null;
|
||||||
|
|
||||||
// For redis
|
// For redis
|
||||||
let cacheConnectionClientId = "";
|
let cacheConnectionClientId = "";
|
||||||
@@ -194,14 +200,17 @@ if (config.redis?.enabled) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const ttl = client.ttl.bind(client);
|
const ttl = client.ttl.bind(client);
|
||||||
exportClient.ttl = (key) => {
|
exportClient.ttl = async (key) => {
|
||||||
if (cache && cacheClient && cache.has(key)) {
|
if (cache && cacheClient && ttlCache.has(key)) {
|
||||||
// Trigger usage of cache
|
// Trigger usage of cache
|
||||||
cache.get(key);
|
cache.get(key);
|
||||||
|
|
||||||
return Promise.resolve(config.redis?.expiryTime - Math.floor((cache.ttl - cache.info(key).ttl) / 1000));
|
return ttlCache.get(key) + config.redis?.expiryTime - Math.floor(Date.now() / 1000);
|
||||||
} else {
|
} else {
|
||||||
return ttl(createKeyName(key));
|
const result = await ttl(createKeyName(key));
|
||||||
|
if (ttlCache) ttlCache.set(key, Math.floor(Date.now() / 1000) - (config.redis?.expiryTime - result));
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -401,6 +410,8 @@ async function setupCacheClientListener(cacheClient: RedisClientType,
|
|||||||
lastInvalidation = Date.now();
|
lastInvalidation = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ttlCache.get(key);
|
||||||
|
|
||||||
// To tell it to not save the result of this currently running request
|
// To tell it to not save the result of this currently running request
|
||||||
if (key && activeRequestPromises[key] !== undefined) {
|
if (key && activeRequestPromises[key] !== undefined) {
|
||||||
resetKeys.add(key);
|
resetKeys.add(key);
|
||||||
|
|||||||
Reference in New Issue
Block a user