2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 01:44:04 +00:00
books/fyo/utils/cacheManager.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.2 KiB
TypeScript
Raw Normal View History

export default class CacheManager {
_keyValueCache: Map<string, unknown | undefined>;
_hashCache: Map<string, Map<string, unknown> | undefined>;
constructor() {
this._keyValueCache = new Map();
this._hashCache = new Map();
}
// Regular Cache Ops
getValue(key: string) {
return this._keyValueCache.get(key);
}
setValue(key: string, value: unknown) {
this._keyValueCache.set(key, value);
}
clearValue(key: string) {
this._keyValueCache.delete(key);
}
// Hash Cache Ops
hget(hashName: string, key: string) {
const hc = this._hashCache.get(hashName);
if (hc === undefined) {
return hc;
}
return hc.get(key);
}
hset(hashName: string, key: string, value: unknown) {
const hc = this._hashCache.get(hashName);
if (hc === undefined) {
this._hashCache.set(hashName, new Map());
}
this._hashCache.get(hashName)!.set(key, value);
}
2022-03-23 06:16:13 +00:00
hclear(hashName: string, key?: string) {
if (key) {
this._hashCache.get(hashName)?.delete(key);
} else {
this._hashCache.get(hashName)?.clear();
}
}
hexists(hashName: string) {
return this._hashCache.get(hashName) !== undefined;
}
}