import { CacheDelegate } from "./interfaces"; export default class Cache { public hits = 0; public misses = 0; private store = new Map(); constructor(private delegate: CacheDelegate) {} public get(key: K): V { const cacheKey = this.delegate.cacheKey(key); let value = this.store.get(cacheKey); if (value === undefined) { this.misses++; value = this.delegate.create(key); this.store.set(cacheKey, value); } else { this.hits++; } return value; } public clear() { this.store.clear(); } }