@jbagatta/johnny-cache
Version:
A robust distributed dictionary for coordinating and caching expensive operations in a distributed environment
38 lines (37 loc) • 1.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DistributedDictionaryFactory = void 0;
const node_cache_1 = __importDefault(require("node-cache"));
const johnny_cache_1 = require("../core/johnny-cache");
const ioredis_1 = __importDefault(require("ioredis"));
const johnny_locke_1 = require("@jbagatta/johnny-locke");
const redis_l1_cache_manager_1 = require("../core/l1-cache/redis-l1-cache-manager");
const nats_l1_cache_manager_1 = require("../core/l1-cache/nats-l1-cache-manager");
class DistributedDictionaryFactory {
static async create(client, cacheOptions, l1Cache) {
const config = {
namespace: cacheOptions.name,
defaultLockDurationMs: cacheOptions.expiry?.timeMs ?? 30000,
objectExpiryMs: cacheOptions.expiry?.timeMs
};
const lock = (client instanceof ioredis_1.default)
? await johnny_locke_1.RedisDistributedLock.create(client, config)
: await johnny_locke_1.JetstreamDistributedLock.create(client, config);
let cacheManager;
if (cacheOptions.l1CacheOptions?.enabled === true) {
const l1 = l1Cache ?? new node_cache_1.default({
checkperiod: cacheOptions.l1CacheOptions?.purgeIntervalSeconds ?? 10,
errorOnMissing: false,
deleteOnExpire: true
});
cacheManager = (client instanceof ioredis_1.default)
? new redis_l1_cache_manager_1.RedisL1CacheManager(client, config.namespace, l1)
: new nats_l1_cache_manager_1.NatsL1CacheManager(client, config.namespace, l1);
}
return new johnny_cache_1.JohnnyCache(lock, cacheOptions, cacheManager);
}
}
exports.DistributedDictionaryFactory = DistributedDictionaryFactory;