UNPKG

@naturalcycles/db-lib

Version:

Lowest Common Denominator API to supported Databases

31 lines (30 loc) 1.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommonKeyValueDaoMemoCache = void 0; const js_lib_1 = require("@naturalcycles/js-lib"); /** * AsyncMemoCache implementation, backed by CommonKeyValueDao. * * Does NOT support persisting Errors, skips them instead. * * Also, does not support .clear(), as it's more dangerous than useful to actually * clear the whole table/cache. */ class CommonKeyValueDaoMemoCache { constructor(cfg) { this.cfg = cfg; } async get(k) { return (await this.cfg.dao.getById(k)) || js_lib_1.MISS; } async set(k, v) { const opt = this.cfg.ttl ? { expireAt: (js_lib_1.localTime.nowUnix() + this.cfg.ttl) } : undefined; await this.cfg.dao.save(k, v, opt); } async clear() { throw new Error('CommonKeyValueDaoMemoCache.clear is not supported, because cache is expected to be persistent'); } } exports.CommonKeyValueDaoMemoCache = CommonKeyValueDaoMemoCache;