magicbell
Version:
MagicBell API wrapper
52 lines • 1.61 kB
JavaScript
import { debug } from './log.js';
function sortObj(obj) {
const entries = Object.entries(obj || {});
if (entries.length === 0)
return undefined;
return Object.fromEntries(entries.sort());
}
export class Cache {
#records = new Map();
#ttl;
constructor(options) {
this.#ttl = options?.ttl ?? 1000;
}
getRequestKey(args) {
const { method, url, data, headers } = args;
return JSON.stringify({
method,
url: url.toString(),
data: sortObj(data),
headers: sortObj(headers),
});
}
#flush() {
const currentTimestamp = Date.now();
for (const [key, { timestamp }] of this.#records.entries()) {
if (currentTimestamp - timestamp >= this.#ttl) {
const age = currentTimestamp - timestamp;
debug('cache FLUSH', { key, age, ttl: this.#ttl });
this.#records.delete(key);
}
}
}
get(key) {
this.#flush();
const record = this.#records.get(key);
if (record) {
const age = Date.now() - record.timestamp;
debug('cache HIT', { key, age, ttl: this.#ttl });
return record.promise;
}
debug('cache MISS', { key, ttl: this.#ttl });
return null;
}
set(key, promise) {
if (this.#ttl <= 0)
return;
const timestamp = Date.now();
debug('cache SET', { key, timestamp, ttl: this.#ttl });
this.#records.set(key, { promise, timestamp });
}
}
//# sourceMappingURL=cache.js.map