mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
49 lines
872 B
TypeScript
49 lines
872 B
TypeScript
import NodeCache from 'node-cache';
|
|
|
|
export default class Cache {
|
|
cache: NodeCache;
|
|
|
|
constructor(config?: object) {
|
|
this.cache = new NodeCache({
|
|
useClones: false,
|
|
...config,
|
|
});
|
|
}
|
|
|
|
get(key: string, storeFunction: () => Promise<any>) {
|
|
const value = this.cache.get(key);
|
|
|
|
if (value) {
|
|
return Promise.resolve(value);
|
|
}
|
|
return storeFunction().then((result) => {
|
|
this.cache.set(key, result);
|
|
return result;
|
|
});
|
|
}
|
|
|
|
set(key: string, results: any) {
|
|
this.cache.set(key, results);
|
|
}
|
|
|
|
del(keys: string) {
|
|
this.cache.del(keys);
|
|
}
|
|
|
|
delStartWith(startStr = '') {
|
|
if (!startStr) {
|
|
return;
|
|
}
|
|
|
|
const keys = this.cache.keys();
|
|
for (const key of keys) {
|
|
if (key.indexOf(startStr) === 0) {
|
|
this.del(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
flush() {
|
|
this.cache.flushAll();
|
|
}
|
|
} |