UNPKG

@croct/cache

Version:

An abstraction layer for caching.

31 lines (30 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimestampedCacheEntry = void 0; const time_1 = require("@croct/time"); var TimestampedCacheEntry; (function (TimestampedCacheEntry) { function toJSON(entry) { return JSON.stringify({ value: entry.value, timestamp: entry.timestamp.toEpochMillis(), }); } TimestampedCacheEntry.toJSON = toJSON; function fromJSON(json) { const jsonEntry = JSON.parse(json); if (jsonEntry === null || typeof jsonEntry !== 'object' || Array.isArray(jsonEntry) || jsonEntry.value === undefined || typeof jsonEntry.timestamp !== 'number' || !Number.isSafeInteger(jsonEntry.timestamp)) { throw new Error('Invalid JSON representation of TimestampedCacheEntry.'); } return { value: jsonEntry.value, timestamp: time_1.Instant.ofEpochMilli(jsonEntry.timestamp), }; } TimestampedCacheEntry.fromJSON = fromJSON; })(TimestampedCacheEntry || (exports.TimestampedCacheEntry = TimestampedCacheEntry = {}));