@croct/cache
Version:
An abstraction layer for caching.
26 lines (25 loc) • 628 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryCache = void 0;
/**
* In-memory cache implementation backed by a native `Map`.
*/
class InMemoryCache {
constructor() {
this.cache = new Map();
}
get(key, loader) {
return this.cache.has(key)
? Promise.resolve(this.cache.get(key))
: loader(key);
}
set(key, value) {
this.cache.set(key, value);
return Promise.resolve();
}
delete(key) {
this.cache.delete(key);
return Promise.resolve();
}
}
exports.InMemoryCache = InMemoryCache;