fix(core): update
This commit is contained in:
@@ -4,57 +4,99 @@ import { CacheMemoryManager } from './levelcache.classes.cache.memorymanager';
|
||||
import { CacheS3Manager } from './levelcache.classes.cache.s3manager';
|
||||
import { CacheEntry } from './levelcache.classes.cacheentry';
|
||||
import { CacheRouter } from './levelcache.classes.cacherouter';
|
||||
import { AbstractCache } from './levelcache.abstract.classes.cache';
|
||||
|
||||
export interface ILevelCacheConstructorOptions {
|
||||
maxMemoryStorageInMB: number;
|
||||
maxDiskStorageInMB: number;
|
||||
/**
|
||||
* a unique id when having more than one cache
|
||||
*/
|
||||
cacheId: string;
|
||||
maxMemoryStorageInMB?: number;
|
||||
maxDiskStorageInMB?: number;
|
||||
maxS3StorageInMB?: number;
|
||||
smartbucketConfig?: plugins.smartbucket.ISmartBucketConfig;
|
||||
diskStoragePath?: string;
|
||||
s3Config?: plugins.smartbucket.ISmartBucketConfig;
|
||||
s3BucketName?: string;
|
||||
forceLevel?: 'memory' | 'disk' | 's3';
|
||||
expirationInMs?: number;
|
||||
immutableCache?: boolean;
|
||||
persistentCache?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* a leveled cache for storing things for a short time
|
||||
*/
|
||||
export class LevelCache {
|
||||
export class LevelCache extends AbstractCache {
|
||||
private readyDeferred = plugins.smartpromise.defer<void>();
|
||||
|
||||
public ready = this.readyDeferred.promise;
|
||||
|
||||
public status: 'active' = 'active'; // artifact of AbstractCache
|
||||
|
||||
public cacheRouter = new CacheRouter(this);
|
||||
public cacheDiskManager = new CacheDiskManager();
|
||||
public cacheMemoryManager = new CacheMemoryManager();
|
||||
public cacheS3Manager = new CacheS3Manager();
|
||||
public cacheDiskManager: CacheDiskManager;
|
||||
public cacheMemoryManager: CacheMemoryManager;
|
||||
public cacheS3Manager: CacheS3Manager;
|
||||
|
||||
public options: ILevelCacheConstructorOptions;
|
||||
|
||||
constructor(optionsArg: ILevelCacheConstructorOptions) {
|
||||
super();
|
||||
this.options = optionsArg;
|
||||
this.init();
|
||||
}
|
||||
|
||||
private processKey(keyArg: string) {
|
||||
if (!keyArg) {
|
||||
return plugins.smartunique.shortId();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
// Blobs
|
||||
/**
|
||||
* store a Blob
|
||||
*/
|
||||
public async storeBlobByKey(keyArg: string, blob: Buffer) {
|
||||
keyArg = this.processKey(keyArg);
|
||||
return keyArg;
|
||||
// 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve a blob
|
||||
*/
|
||||
public async retrieveBlob(keyArg: string): CacheEntry {}
|
||||
|
||||
// Cache Entries
|
||||
// retrieve things
|
||||
/**
|
||||
* retrieve cache entry
|
||||
*/
|
||||
public async retrieveCacheEntryByKey(): CacheEntry {}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async checkKeyPresence(keyArg: string): Promise<boolean> {
|
||||
return plugins.smartpromise.getFirstTrueOrFalse([
|
||||
this.cacheMemoryManager.checkKeyPresence(keyArg),
|
||||
this.cacheDiskManager.checkKeyPresence(keyArg),
|
||||
this.cacheS3Manager.checkKeyPresence(keyArg)
|
||||
]);
|
||||
}
|
||||
|
||||
// cache maintenance
|
||||
/**
|
||||
* cleans the cache
|
||||
*/
|
||||
public clean() {}
|
||||
public async clean(): Promise<void> {
|
||||
await Promise.all([
|
||||
this.cacheDiskManager.clean(),
|
||||
this.cacheDiskManager.clean(),
|
||||
this.cacheS3Manager.clean(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user