UNPKG

@pushchain/core

Version:
65 lines 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cache = exports.CacheKeys = void 0; class CacheKeys { static ueaAddressOnchain(chain, address, pushNetwork, vm) { return `uea_address_onchain:${chain}:${address}:${pushNetwork}:${vm}`; } static deploymentStatus(address) { return `deployment_status:${address}`; } } exports.CacheKeys = CacheKeys; class Cache { constructor(maxSize = 100) { this.cache = new Map(); this.maxSize = maxSize; } isExpired(entry) { if (!entry.ttl) return false; return Date.now() > entry.createdAt + entry.ttl; } get(key) { if (!this.cache.has(key)) return null; const entry = this.cache.get(key); if (entry != undefined) { if (this.isExpired(entry)) { this.cache.delete(key); return null; } this.cache.delete(key); this.cache.set(key, entry); return entry.value; } return null; } set(key, value, ttl) { if (this.cache.has(key)) { this.cache.delete(key); } else if (this.cache.size >= this.maxSize) { const oldestKey = this.cache.keys().next().value; if (oldestKey != undefined) { this.cache.delete(oldestKey); } } this.cache.set(key, { value, createdAt: Date.now(), ttl, }); } clear(key) { this.cache.delete(key); } clearAll() { this.cache.clear(); } size() { return this.cache.size; } } exports.Cache = Cache; //# sourceMappingURL=cache.js.map