UNPKG

mastercache

Version:

Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers

83 lines (82 loc) 1.92 kB
// src/serializers/json.ts var JsonSerializer = class { serialize(value) { return JSON.stringify(value); } deserialize(value) { return JSON.parse(value); } }; // src/cache/cache-entry/cache-entry.ts var CacheEntry = class _CacheEntry { /** * The key of the cache item. */ #key; /** * The value of the item. */ #value; /** * The logical expiration is the time in miliseconds when the item * will be considered expired. But, if grace period is enabled, * the item will still be available for a while. */ #logicalExpiration; #earlyExpiration; static #serializer = new JsonSerializer(); constructor(key, item) { this.#key = key; this.#value = item.value; this.#logicalExpiration = item.logicalExpiration; this.#earlyExpiration = item.earlyExpiration; } getValue() { return this.#value; } getKey() { return this.#key; } getLogicalExpiration() { return this.#logicalExpiration; } getEarlyExpiration() { return this.#earlyExpiration; } isLogicallyExpired() { return Date.now() >= this.#logicalExpiration; } isEarlyExpired() { if (!this.#earlyExpiration) { return false; } if (this.isLogicallyExpired()) { return false; } return Date.now() >= this.#earlyExpiration; } static fromDriver(key, item) { return new _CacheEntry(key, this.#serializer.deserialize(item)); } applyFallbackDuration(duration) { this.#logicalExpiration += duration; this.#earlyExpiration = 0; return this; } expire() { this.#logicalExpiration = Date.now() - 100; this.#earlyExpiration = 0; return this; } serialize() { return _CacheEntry.#serializer.serialize({ value: this.#value, logicalExpiration: this.#logicalExpiration, earlyExpiration: this.#earlyExpiration }); } }; export { CacheEntry }; //# sourceMappingURL=cache-entry.js.map