mastercache
Version:
Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers
281 lines (277 loc) • 7.59 kB
JavaScript
// ../../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();
}
};
// src/drivers/database/adapters/orchid.ts
var OrchidAdapter = class {
#connection;
#tableName;
constructor(config) {
this.#connection = config.connection;
}
getTable() {
return this.#connection(this.#tableName, (t) => ({
key: t.varchar().primaryKey(),
value: t.varchar(),
expires_at: t.timestampNoTZ().encode((value) => value).parse((v) => v ? new Date(v).valueOf() : v).nullable()
}));
}
setTableName(tableName) {
this.#tableName = tableName;
}
async get(key) {
const result = await this.getTable().findByOptional({ key }).select("value", "expires_at");
if (!result) return;
return { value: result.value, expiresAt: result.expires_at };
}
async delete(key) {
const count = await this.getTable().where({ key }).delete();
return count > 0;
}
async deleteMany(keys) {
return await this.getTable().whereIn("key", keys).delete();
}
async disconnect() {
await this.#connection.close();
}
async createTableIfNotExists() {
await this.#connection.adapter.pool.query(`
CREATE TABLE IF NOT EXISTS "public"."${this.#tableName}" (
"key" varchar NOT NULL,
"value" text NOT NULL,
"expires_at" timestamp,
PRIMARY KEY ("key")
);
`);
}
async pruneExpiredEntries() {
await this.getTable().where({ expires_at: { lt: /* @__PURE__ */ new Date() } }).delete();
}
async clear(prefix) {
await this.getTable().where({ key: { startsWith: prefix } }).delete();
}
async set(row) {
await this.getTable().findBy({ key: row.key }).upsert({
create: {
key: row.key,
value: row.value,
expires_at: row.expiresAt
},
update: {
value: row.value,
expires_at: row.expiresAt
}
});
}
};
function orchidDriver(options) {
return {
options,
factory: (config) => {
const adapter = new OrchidAdapter(config);
return new DatabaseDriver(adapter, config);
}
};
}
export {
OrchidAdapter,
orchidDriver
};
//# sourceMappingURL=orchid.js.map