levelcache/ts/levelcache.classes.levelcache.ts

122 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

2022-03-22 21:45:12 +00:00
import * as plugins from './levelcache.plugins.js';
import { CacheDiskManager } from './levelcache.classes.cache.diskmanager.js';
import { CacheMemoryManager } from './levelcache.classes.cache.memorymanager.js';
import { CacheS3Manager } from './levelcache.classes.cache.s3manager.js';
import { CacheEntry } from './levelcache.classes.cacheentry.js';
import { CacheRouter } from './levelcache.classes.cacherouter.js';
import { AbstractCache } from './levelcache.abstract.classes.cache.js';
2020-02-15 22:38:28 +00:00
export interface ILevelCacheConstructorOptions {
2021-04-23 18:40:57 +00:00
/**
* a unique id when having more than one cache
*/
cacheId: string;
maxMemoryStorageInMB?: number;
maxDiskStorageInMB?: number;
2020-02-15 22:38:28 +00:00
maxS3StorageInMB?: number;
2021-04-23 18:40:57 +00:00
diskStoragePath?: string;
2024-02-14 00:32:19 +00:00
s3Config?: plugins.tsclass.storage.IS3Descriptor;
2021-04-23 18:40:57 +00:00
s3BucketName?: string;
forceLevel?: 'memory' | 'disk' | 's3';
expirationInMs?: number;
immutableCache?: boolean;
persistentCache?: boolean;
2020-02-15 22:38:28 +00:00
}
2020-02-05 17:11:30 +00:00
/**
* a leveled cache for storing things for a short time
*/
2021-04-23 18:40:57 +00:00
export class LevelCache extends AbstractCache {
private readyDeferred = plugins.smartpromise.defer<void>();
public ready = this.readyDeferred.promise;
public status: 'active' = 'active'; // artifact of AbstractCache
2020-02-15 22:38:28 +00:00
public cacheRouter = new CacheRouter(this);
2021-04-23 18:40:57 +00:00
public cacheDiskManager: CacheDiskManager;
public cacheMemoryManager: CacheMemoryManager;
public cacheS3Manager: CacheS3Manager;
2020-02-05 17:11:30 +00:00
2020-02-15 22:38:28 +00:00
public options: ILevelCacheConstructorOptions;
constructor(optionsArg: ILevelCacheConstructorOptions) {
2021-04-23 18:40:57 +00:00
super();
2022-03-22 21:45:12 +00:00
this.options = {
maxMemoryStorageInMB: 0.5,
maxDiskStorageInMB: 10,
maxS3StorageInMB: 50,
2023-07-20 22:44:39 +00:00
...optionsArg,
2022-03-22 21:45:12 +00:00
};
2021-04-23 18:40:57 +00:00
this.init();
2020-02-15 22:38:28 +00:00
}
2021-04-23 18:40:57 +00:00
public async init() {
this.cacheMemoryManager = new CacheMemoryManager(this);
2022-03-22 21:45:12 +00:00
this.cacheDiskManager = new CacheDiskManager(this);
2021-04-23 18:40:57 +00:00
this.cacheS3Manager = new CacheS3Manager(this);
await Promise.all([
this.cacheDiskManager.ready,
this.cacheMemoryManager.ready,
this.cacheS3Manager.ready,
]);
this.readyDeferred.resolve();
2020-02-14 18:10:10 +00:00
}
2021-04-23 18:40:57 +00:00
// store things
public async storeCacheEntryByKey(keyArg: string, cacheEntryArg: CacheEntry): Promise<void> {
cacheEntryArg.key = keyArg;
const targetCache = await this.cacheRouter.getCacheForStoreAction(keyArg, cacheEntryArg);
cacheEntryArg.createdAt = Date.now();
2021-04-23 18:40:57 +00:00
await targetCache.storeCacheEntryByKey(keyArg, cacheEntryArg);
2020-02-14 18:10:10 +00:00
}
2020-02-05 17:11:30 +00:00
2021-04-23 18:40:57 +00:00
// retrieve things
2020-02-14 17:28:13 +00:00
/**
* retrieve cache entry
*/
2021-04-23 18:40:57 +00:00
public async retrieveCacheEntryByKey(keyArg: string): Promise<CacheEntry> {
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;
}
2021-04-23 18:40:57 +00:00
return cacheEntry;
} else {
return null;
}
}
2020-02-05 17:11:30 +00:00
2021-04-23 18:40:57 +00:00
public async checkKeyPresence(keyArg: string): Promise<boolean> {
return plugins.smartpromise.getFirstTrueOrFalse([
this.cacheMemoryManager.checkKeyPresence(keyArg),
this.cacheDiskManager.checkKeyPresence(keyArg),
2021-04-23 18:41:30 +00:00
this.cacheS3Manager.checkKeyPresence(keyArg),
2021-04-23 18:40:57 +00:00
]);
}
public async deleteCacheEntryByKey(keyArg) {
await Promise.all([
this.cacheMemoryManager.deleteCacheEntryByKey(keyArg),
this.cacheDiskManager.deleteCacheEntryByKey(keyArg),
this.cacheS3Manager.deleteCacheEntryByKey(keyArg),
]);
}
2021-04-23 18:40:57 +00:00
// cache maintenance
public async cleanOutdated() {}
2020-02-14 17:28:13 +00:00
/**
* cleans the complete cache
2020-02-14 17:28:13 +00:00
*/
public async cleanAll(): Promise<void> {
2021-04-23 18:40:57 +00:00
await Promise.all([
this.cacheDiskManager.cleanAll(),
this.cacheDiskManager.cleanAll(),
this.cacheS3Manager.cleanAll(),
2021-04-23 18:40:57 +00:00
]);
}
2020-02-05 17:11:30 +00:00
}