@river-build/web3
Version:
Dapps for our Space and Registry contracts
41 lines • 1.54 kB
JavaScript
import TTLCache from '@isaacs/ttlcache';
export class EntitlementCache {
negativeCache;
positiveCache;
constructor(options) {
const positiveCacheTTLSeconds = options?.positiveCacheTTLSeconds ?? 15 * 60;
const negativeCacheTTLSeconds = options?.negativeCacheTTLSeconds ?? 2;
const positiveCacheSize = options?.positiveCacheSize ?? 10000;
const negativeCacheSize = options?.negativeCacheSize ?? 10000;
this.negativeCache = new TTLCache({
ttl: negativeCacheTTLSeconds * 1000,
max: negativeCacheSize,
});
this.positiveCache = new TTLCache({
ttl: positiveCacheTTLSeconds * 1000,
max: positiveCacheSize,
});
}
async executeUsingCache(keyable, onCacheMiss) {
const key = keyable.toKey();
const negativeCacheResult = this.negativeCache.get(key);
if (negativeCacheResult !== undefined) {
negativeCacheResult.cacheHit = true;
return negativeCacheResult;
}
const positiveCacheResult = this.positiveCache.get(key);
if (positiveCacheResult !== undefined) {
positiveCacheResult.cacheHit = true;
return positiveCacheResult;
}
const result = await onCacheMiss(keyable);
if (result.isPositive) {
this.positiveCache.set(key, result);
}
else {
this.negativeCache.set(key, result);
}
return result;
}
}
//# sourceMappingURL=EntitlementCache.js.map