This commit is contained in:
2018-07-12 21:32:20 +02:00
commit 44ca069df8
12 changed files with 596 additions and 0 deletions

1
ts/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './smartcache.classes.smartcache';

View File

@ -0,0 +1,67 @@
import * as plugins from './smartcache.plugins';
import { SmartError } from '@pushrocks/smarterror';
export interface ICacheMap {
[key: string]: {
timer: plugins.smarttime.Timer;
cachedDeferred: plugins.smartpromise.Deferred<any>;
cachedObject: any;
};
}
export class CacheManager {
private _cacheMap: ICacheMap = {};
cacheExists(identifierArg): boolean {
if (this._cacheMap[identifierArg]) {
return true;
}
return false;
}
stillValid(identifierArg: string): boolean {
if (this.cacheExists(identifierArg) && this._cacheMap[identifierArg].timer.startedAt.isYoungerThanMilliSeconds(
this._cacheMap[identifierArg].timer.timeInMilliseconds
)) {
return true;
}
return false;
}
// announce the caching of something
announceCache(identifierArg: string, validForArg: number) {
this._cacheMap[identifierArg] = {
timer: new plugins.smarttime.Timer(validForArg),
cachedDeferred: new plugins.smartpromise.Deferred(),
cachedObject: null
};
this._cacheMap[identifierArg].timer.start();
this._cacheMap[identifierArg].timer.completed.then(() => {
this.deleteCache(identifierArg)
});
}
/**
* waits for the cache to be ready
*/
async waitForCacheReady (identifierArg: string) {
await this._cacheMap[identifierArg].cachedDeferred.promise
return true;
}
setCache(identifierArg: string, cachedObjectArg: any, validForArg = 1000) {
if(!this.cacheExists(identifierArg)) {
console.log(new SmartError(`Cache for ${identifierArg} has not been announced or timed out!`));
}
this._cacheMap[identifierArg].cachedObject = cachedObjectArg;
this._cacheMap[identifierArg].cachedDeferred.resolve();
}
getCache(identifierArg: string) {
return this._cacheMap[identifierArg];
}
async deleteCache(identifierArg: string) {
delete this._cacheMap[identifierArg];
}
}

View File

@ -0,0 +1,21 @@
import * as plugins from './smartcache.plugins';
import { CacheManager } from './smartcache.classes.cachemanager';
export class SmartCache {
private _cacheManager = new CacheManager();
async cacheReturn(asyncCachedFuncArg: () => Promise<any>, cacheDuration: number = 5000) {
let callStack: string = new plugins.smarterror.SmartError('').cleanFullStack;
let callHash = plugins.nodehash.sha256FromStringSync(callStack);
console.log(callHash);
if(this._cacheManager.cacheExists(callHash) && await this._cacheManager.waitForCacheReady(callHash) && this._cacheManager.stillValid(callHash)) {
return this._cacheManager.getCache(callHash).cachedObject;
} else {
this._cacheManager.announceCache(callHash, cacheDuration);
let newCachedObject = await asyncCachedFuncArg()
this._cacheManager.setCache(callHash, newCachedObject, cacheDuration);
return newCachedObject;
}
}
}

7
ts/smartcache.plugins.ts Normal file
View File

@ -0,0 +1,7 @@
import * as smartdelay from '@pushrocks/smartdelay';
import * as smarterror from '@pushrocks/smarterror';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smarttime from '@pushrocks/smarttime';
import * as nodehash from 'nodehash';
export { smarterror, smartdelay, smartpromise, smarttime, nodehash };