mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
Use cache for ttl if possible
Also fixes etag when compression enabled
This commit is contained in:
parent
7c77bf566e
commit
0602fdd651
3 changed files with 11 additions and 1 deletions
|
@ -38,7 +38,7 @@ function getLastModified(hashType: hashType, hashKey: hashKey, service: Service)
|
||||||
|
|
||||||
export async function getEtag(hashType: hashType, hashKey: hashKey, service: Service): Promise<ETag> {
|
export async function getEtag(hashType: hashType, hashKey: hashKey, service: Service): Promise<ETag> {
|
||||||
const lastModified = await getLastModified(hashType, hashKey, service);
|
const lastModified = await getLastModified(hashType, hashKey, service);
|
||||||
return `${hashType};${hashKey};${service};${lastModified.getTime()}` as ETag;
|
return `"${hashType};${hashKey};${service};${lastModified.getTime()}"` as ETag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* example usage
|
/* example usage
|
||||||
|
|
|
@ -143,6 +143,7 @@ async function getKeyLastModified(key: string): Promise<Date> {
|
||||||
if (!config.redis?.enabled) return Promise.reject("ETag - Redis not enabled");
|
if (!config.redis?.enabled) return Promise.reject("ETag - Redis not enabled");
|
||||||
return await redis.ttl(key)
|
return await redis.ttl(key)
|
||||||
.then(ttl => {
|
.then(ttl => {
|
||||||
|
if (ttl <= 0) return new Date();
|
||||||
const sinceLive = config.redis?.expiryTime - ttl;
|
const sinceLive = config.redis?.expiryTime - ttl;
|
||||||
const now = Math.floor(Date.now() / 1000);
|
const now = Math.floor(Date.now() / 1000);
|
||||||
return new Date((now-sinceLive) * 1000);
|
return new Date((now-sinceLive) * 1000);
|
||||||
|
|
|
@ -193,6 +193,15 @@ if (config.redis?.enabled) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const ttl = client.ttl.bind(client);
|
||||||
|
exportClient.ttl = (key) => {
|
||||||
|
if (cache && cacheClient && cache.has(key)) {
|
||||||
|
return Promise.resolve(config.redis?.expiryTime - Math.floor((cache.ttl - cache.info(key).ttl) / 1000));
|
||||||
|
} else {
|
||||||
|
return ttl(createKeyName(key));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const get = client.get.bind(client);
|
const get = client.get.bind(client);
|
||||||
const getRead = readClient?.get?.bind(readClient);
|
const getRead = readClient?.get?.bind(readClient);
|
||||||
exportClient.get = (key) => new Promise((resolve, reject) => {
|
exportClient.get = (key) => new Promise((resolve, reject) => {
|
||||||
|
|
Loading…
Reference in a new issue