UNPKG

mastercache

Version:

Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers

234 lines (229 loc) 6.64 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/drivers/database/database.ts var database_exports = {}; __export(database_exports, { DatabaseDriver: () => DatabaseDriver }); module.exports = __toCommonJS(database_exports); // ../../node_modules/.pnpm/@lukeed+ms@2.0.2/node_modules/@lukeed/ms/dist/index.mjs var RGX = /^(-?(?:\d+)?\.?\d+) *(m(?:illiseconds?|s(?:ecs?)?))?(s(?:ec(?:onds?|s)?)?)?(m(?:in(?:utes?|s)?)?)?(h(?:ours?|rs?)?)?(d(?:ays?)?)?(w(?:eeks?|ks?)?)?(y(?:ears?|rs?)?)?$/; var SEC = 1e3; var MIN = SEC * 60; var HOUR = MIN * 60; var DAY = HOUR * 24; var YEAR = DAY * 365.25; function parse(val) { var num, arr = val.toLowerCase().match(RGX); if (arr != null && (num = parseFloat(arr[1]))) { if (arr[3] != null) return num * SEC; if (arr[4] != null) return num * MIN; if (arr[5] != null) return num * HOUR; if (arr[6] != null) return num * DAY; if (arr[7] != null) return num * DAY * 7; if (arr[8] != null) return num * YEAR; return num; } } // src/helpers.ts function resolveTtl(ttl, defaultTtl = 3e4) { if (typeof ttl === "number") return ttl; if (ttl === null) { return void 0; } if (ttl === void 0) { if (typeof defaultTtl === "number") return defaultTtl; if (typeof defaultTtl === "string") return parse(defaultTtl); return void 0; } return parse(ttl); } // src/drivers/base-driver.ts var BaseDriver = class { constructor(config) { this.config = config; this.prefix = this.#sanitizePrefix(config.prefix); } /** * Current cache prefix */ prefix; /** * Sanitizes the cache prefix by removing any trailing colons */ #sanitizePrefix(prefix) { if (!prefix) return ""; return prefix.replace(/:+$/, ""); } /** * Creates a namespace prefix by concatenating the cache prefix with the given namespace * If the cache prefix is not defined, the namespace is returned as is */ createNamespacePrefix(namespace) { const sanitizedPrefix = this.#sanitizePrefix(this.prefix); return sanitizedPrefix ? `${sanitizedPrefix}:${namespace}` : namespace; } /** * Returns the cache key with the prefix added to it, if a prefix is defined */ getItemKey(key) { return this.prefix ? `${this.prefix}:${key}` : key; } }; // src/drivers/database/database.ts var DatabaseDriver = class extends BaseDriver { type = "l2"; /** * The underlying adapter */ #adapter; /** * A promise that resolves when the table is created */ #initialized; /** * Pruning interval */ #pruneInterval; constructor(adapter, config, isNamespace = false) { super(config); this.#adapter = adapter; if (isNamespace) { this.#initialized = Promise.resolve(); return; } this.#adapter.setTableName(config.tableName || "mastercache"); if (config.autoCreateTable !== false) { this.#initialized = this.#adapter.createTableIfNotExists(); } else { this.#initialized = Promise.resolve(); } if (config.pruneInterval === false) return; this.#startPruneInterval(resolveTtl(config.pruneInterval)); } /** * Start the interval that will prune expired entries * Maybe rework this using a node Worker ? */ #startPruneInterval(interval) { this.#pruneInterval = setInterval(async () => { await this.#initialized; await this.#adapter.pruneExpiredEntries().catch((err) => console.error("[mastercache] failed to prune expired entries", err)); }, interval); } /** * Check if the given timestamp is expired */ #isExpired(expiration) { return expiration !== null && expiration < Date.now(); } /** * Returns a new instance of the driver namespaced */ namespace(namespace) { const store = new this.constructor( this.#adapter, { ...this.config, prefix: this.createNamespacePrefix(namespace) }, true ); return store; } /** * Get a value from the cache */ async get(key) { await this.#initialized; const result = await this.#adapter.get(this.getItemKey(key)); if (!result) return; if (this.#isExpired(result.expiresAt)) { await this.#adapter.delete(key); return; } return result.value; } /** * Get the value of a key and delete it * * Returns the value if the key exists, undefined otherwise */ async pull(key) { const value = await this.get(key); if (value) await this.delete(key); return value; } /** * Set a value in the cache * Returns true if the value was set, false otherwise */ async set(key, value, ttl) { await this.#initialized; await this.#adapter.set({ key: this.getItemKey(key), value, expiresAt: ttl ? new Date(Date.now() + ttl) : null }); return true; } /** * Check if a key exists in the cache */ async has(key) { await this.#initialized; const result = await this.get(key); if (!result) return false; return true; } /** * Remove all items from the cache */ async clear() { await this.#initialized; await this.#adapter.clear(this.prefix); } /** * Delete a key from the cache * Returns true if the key was deleted, false otherwise */ async delete(key) { await this.#initialized; return this.#adapter.delete(this.getItemKey(key)); } /** * Delete multiple keys from the cache */ async deleteMany(keys) { await this.#initialized; keys = keys.map((key) => this.getItemKey(key)); const result = await this.#adapter.deleteMany(keys); return result > 0; } /** * Disconnect from the database */ async disconnect() { if (this.#pruneInterval) { clearInterval(this.#pruneInterval); } await this.#adapter.disconnect(); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DatabaseDriver }); //# sourceMappingURL=database.cjs.map