levelcache/ts/levelcache.classes.levelcache.ts

102 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-02-05 17:11:30 +00:00
import * as plugins from './levelcache.plugins';
2020-02-14 17:28:13 +00:00
import { CacheDiskManager } from './levelcache.classes.cache.diskmanager';
import { CacheMemoryManager } from './levelcache.classes.cache.memorymanager';
import { CacheS3Manager } from './levelcache.classes.cache.s3manager';
2020-02-14 18:10:10 +00:00
import { CacheEntry } from './levelcache.classes.cacheentry';
2020-02-15 22:38:28 +00:00
import { CacheRouter } from './levelcache.classes.cacherouter';
2021-04-23 18:40:57 +00:00
import { AbstractCache } from './levelcache.abstract.classes.cache';
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;
s3Config?: plugins.smartbucket.ISmartBucketConfig;
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();
2020-02-15 22:38:28 +00:00
this.options = optionsArg;
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.cacheDiskManager = new CacheDiskManager(this);
this.cacheMemoryManager = new CacheMemoryManager(this);
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);
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);
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
]);
}
// cache maintenance
2020-02-14 17:28:13 +00:00
/**
* cleans the cache
*/
2021-04-23 18:40:57 +00:00
public async clean(): Promise<void> {
await Promise.all([
this.cacheDiskManager.clean(),
this.cacheDiskManager.clean(),
this.cacheS3Manager.clean(),
]);
}
2020-02-05 17:11:30 +00:00
}