mastercache
Version:
Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers
167 lines (165 loc) • 4.33 kB
JavaScript
// 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
});
}
};
// src/cache/facades/local-cache.ts
var LocalCache = class {
#driver;
#logger;
constructor(driver, logger) {
this.#driver = driver;
this.#logger = logger.child({ context: "mastercache.localCache" });
}
/**
* Get an item from the local cache
*/
get(key, options) {
this.#logger.trace({ key, opId: options.id }, "try getting local cache item");
const value = this.#driver.get(key);
if (value === void 0) {
this.#logger.trace({ key, opId: options.id }, "local cache item not found");
return;
}
return CacheEntry.fromDriver(key, value);
}
/**
* Set a new item in the local cache
*/
set(key, value, options) {
if (!options.isGracePeriodEnabled && options.physicalTtl && options.physicalTtl <= 0) {
return this.delete(key, options);
}
this.#logger.trace({ key, value, opId: options.id }, "saving local cache item");
this.#driver.set(key, value, options.physicalTtl);
}
/**
* Delete an item from the local cache
*/
delete(key, options) {
this.#logger.trace({ key, opId: options?.id }, "deleting local cache item");
return this.#driver.delete(key);
}
/**
* Make an item logically expire in the local cache
*
* That means that the item will be expired but kept in the cache
* in order to be able to return it to the user if the remote cache
* is down and the grace period is enabled
*/
logicallyExpire(key) {
this.#logger.trace({ key }, "logically expiring local cache item");
const value = this.#driver.get(key);
if (value === void 0) return;
const newEntry = CacheEntry.fromDriver(key, value).expire().serialize();
return this.#driver.set(key, newEntry, this.#driver.getRemainingTtl(key));
}
/**
* Delete many item from the local cache
*/
deleteMany(keys, options) {
this.#logger.trace({ keys, options, opId: options.id }, "deleting local cache items");
this.#driver.deleteMany(keys);
}
/**
* Create a new namespace for the local cache
*/
namespace(namespace) {
return this.#driver.namespace(namespace);
}
/**
* Check if an item exists in the local cache
*/
has(key) {
return this.#driver.has(key);
}
/**
* Clear the local cache
*/
clear() {
return this.#driver.clear();
}
/**
* Disconnect from the local cache
*/
disconnect() {
return this.#driver.disconnect();
}
};
export {
LocalCache
};
//# sourceMappingURL=local-cache.js.map