2022-03-22 21:45:12 +00:00
|
|
|
import * as plugins from './levelcache.plugins.js';
|
|
|
|
import { LevelCache } from './levelcache.classes.levelcache.js';
|
|
|
|
import { AbstractCache } from './levelcache.abstract.classes.cache.js';
|
|
|
|
import { CacheEntry } from './levelcache.classes.cacheentry.js';
|
2020-02-14 18:10:10 +00:00
|
|
|
|
|
|
|
export class CacheRouter {
|
2020-02-15 22:38:28 +00:00
|
|
|
public levelCacheRef: LevelCache;
|
2021-04-23 18:40:57 +00:00
|
|
|
public cacheKeyMap = new plugins.lik.FastMap<AbstractCache>();
|
2020-02-15 22:38:28 +00:00
|
|
|
|
|
|
|
constructor(levelCacheRef: LevelCache) {
|
|
|
|
this.levelCacheRef = levelCacheRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the relevant cache to perform a store action on
|
|
|
|
*/
|
2022-03-22 21:45:12 +00:00
|
|
|
async getCacheForStoreAction (keyArg: string, cacheEntry: CacheEntry): Promise<AbstractCache> {
|
2021-04-23 18:40:57 +00:00
|
|
|
let returnCache: AbstractCache;
|
2022-03-22 21:45:12 +00:00
|
|
|
const mbToBytesMultiplier = 1000 * 1000;
|
|
|
|
const maxMemoryBytes = this.levelCacheRef.options.maxMemoryStorageInMB * mbToBytesMultiplier;
|
|
|
|
const maxDiskBytes = this.levelCacheRef.options.maxDiskStorageInMB * mbToBytesMultiplier;
|
|
|
|
const maxS3Bytes = this.levelCacheRef.options.maxS3StorageInMB * mbToBytesMultiplier;
|
|
|
|
|
2021-04-23 18:40:57 +00:00
|
|
|
switch (true) {
|
2022-03-22 21:45:12 +00:00
|
|
|
case cacheEntry.contents.byteLength <= maxMemoryBytes &&
|
|
|
|
this.levelCacheRef.cacheMemoryManager.status === 'active':
|
2021-04-23 18:40:57 +00:00
|
|
|
returnCache = this.levelCacheRef.cacheMemoryManager;
|
|
|
|
break;
|
|
|
|
case this.levelCacheRef.cacheDiskManager.status === 'active' &&
|
2022-03-22 21:45:12 +00:00
|
|
|
cacheEntry.contents.byteLength > maxMemoryBytes &&
|
|
|
|
cacheEntry.contents.byteLength <= maxDiskBytes &&
|
|
|
|
this.levelCacheRef.cacheDiskManager.status === 'active':
|
2021-04-23 18:40:57 +00:00
|
|
|
returnCache = this.levelCacheRef.cacheDiskManager;
|
|
|
|
break;
|
2022-03-22 21:45:12 +00:00
|
|
|
case cacheEntry.contents.byteLength > maxDiskBytes &&
|
|
|
|
cacheEntry.contents.byteLength < maxS3Bytes &&
|
2021-04-23 18:40:57 +00:00
|
|
|
this.levelCacheRef.cacheS3Manager.status === 'active':
|
|
|
|
returnCache = this.levelCacheRef.cacheS3Manager;
|
|
|
|
break;
|
|
|
|
default:
|
2022-03-22 21:45:12 +00:00
|
|
|
returnCache = null;
|
2021-04-23 18:40:57 +00:00
|
|
|
}
|
|
|
|
this.cacheKeyMap.addToMap(keyArg, returnCache);
|
|
|
|
return returnCache;
|
|
|
|
}
|
2020-02-15 22:38:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the relevant cache to perform a retrieval action on
|
|
|
|
*/
|
2022-03-22 21:45:12 +00:00
|
|
|
async getCacheForRetrieveAction (keyArg: string): Promise<AbstractCache> {
|
2021-04-23 18:40:57 +00:00
|
|
|
const done = plugins.smartpromise.defer<AbstractCache>();
|
|
|
|
const returnCache = this.cacheKeyMap.getByKey(keyArg);
|
|
|
|
if (!returnCache && this.levelCacheRef.options.persistentCache) {
|
|
|
|
const checkCache = (cacheArg: AbstractCache) => {
|
|
|
|
const resultPromise = cacheArg.checkKeyPresence(keyArg);
|
2021-04-23 18:41:30 +00:00
|
|
|
resultPromise.then((hasKeyArg) => {
|
2021-04-23 18:40:57 +00:00
|
|
|
if (hasKeyArg) {
|
|
|
|
done.resolve(cacheArg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return resultPromise;
|
|
|
|
};
|
|
|
|
Promise.all([
|
|
|
|
checkCache(this.levelCacheRef.cacheMemoryManager),
|
|
|
|
checkCache(this.levelCacheRef.cacheDiskManager),
|
2021-04-23 18:41:30 +00:00
|
|
|
checkCache(this.levelCacheRef.cacheMemoryManager),
|
2021-04-23 18:40:57 +00:00
|
|
|
]).then(() => {
|
|
|
|
done.resolve(returnCache);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done.resolve(returnCache);
|
|
|
|
}
|
|
|
|
return done.promise;
|
|
|
|
}
|
2020-02-15 22:38:28 +00:00
|
|
|
}
|