UNPKG

@croct/cache

Version:

An abstraction layer for caching.

26 lines (25 loc) 697 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutoSaveCache = void 0; /** * A cache provider that automatically caches loaded values. */ class AutoSaveCache { constructor(cacheProvider) { this.cacheProvider = cacheProvider; } get(key, loader) { return this.cacheProvider.get(key, async () => { const value = await loader(key); await this.cacheProvider.set(key, value); return value; }); } set(key, value) { return this.cacheProvider.set(key, value); } delete(key) { return this.cacheProvider.delete(key); } } exports.AutoSaveCache = AutoSaveCache;