UNPKG

cachified-redis-json-adapter

Version:
43 lines (42 loc) 2.14 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { totalTtl } from '@epic-web/cachified'; export function redisJsonCacheAdapter(redisJsonCache) { return { name: redisJsonCache.name || 'RedisJson', delete(key) { return redisJsonCache.json.del(key); }, set(key, value) { var _a; return __awaiter(this, void 0, void 0, function* () { const ttl = totalTtl(value === null || value === void 0 ? void 0 : value.metadata); const createdTime = (_a = value === null || value === void 0 ? void 0 : value.metadata) === null || _a === void 0 ? void 0 : _a.createdTime; // the $ is the root path of the json object // so we are ovewriting the entire object at the specified "key" const setOperation = yield redisJsonCache.json.set(key, '$', value); // We want the user to have if (ttl > 0 && ttl < Infinity && typeof createdTime === 'number') { yield redisJsonCache.expireAt(key, Math.ceil((ttl + createdTime) / 1000)); } return setOperation; }); }, get(key) { return __awaiter(this, void 0, void 0, function* () { const value = yield redisJsonCache.json.get(key); if (value == null) { return null; } return value; }); }, }; }