fix(caching): properly respect ttl for all cache levels

This commit is contained in:
2021-05-10 14:26:32 +00:00
parent f7b6df5ff7
commit 6b57e8b1f3
7 changed files with 88 additions and 20 deletions

View File

@@ -62,6 +62,7 @@ export class LevelCache extends AbstractCache {
public async storeCacheEntryByKey(keyArg: string, cacheEntryArg: CacheEntry): Promise<void> {
cacheEntryArg.key = keyArg;
const targetCache = await this.cacheRouter.getCacheForStoreAction(keyArg, cacheEntryArg);
cacheEntryArg.createdAt = Date.now();
await targetCache.storeCacheEntryByKey(keyArg, cacheEntryArg);
}
@@ -73,6 +74,10 @@ export class LevelCache extends AbstractCache {
const targetCache = await this.cacheRouter.getCacheForRetrieveAction(keyArg);
if (targetCache) {
const cacheEntry = await targetCache.retrieveCacheEntryByKey(keyArg);
if (cacheEntry.createdAt + cacheEntry.ttl < Date.now()) {
await this.deleteCacheEntryByKey(keyArg).catch();
return null;
}
return cacheEntry;
} else {
return null;
@@ -87,15 +92,25 @@ export class LevelCache extends AbstractCache {
]);
}
// cache maintenance
/**
* cleans the cache
*/
public async clean(): Promise<void> {
public async deleteCacheEntryByKey(keyArg) {
await Promise.all([
this.cacheDiskManager.clean(),
this.cacheDiskManager.clean(),
this.cacheS3Manager.clean(),
this.cacheMemoryManager.deleteCacheEntryByKey(keyArg),
this.cacheDiskManager.deleteCacheEntryByKey(keyArg),
this.cacheS3Manager.deleteCacheEntryByKey(keyArg),
]);
}
// cache maintenance
public async cleanOutdated() {}
/**
* cleans the complete cache
*/
public async cleanAll(): Promise<void> {
await Promise.all([
this.cacheDiskManager.cleanAll(),
this.cacheDiskManager.cleanAll(),
this.cacheS3Manager.cleanAll(),
]);
}
}