UNPKG

bentocache

Version:

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

32 lines (31 loc) 765 B
// src/cache/locks.ts import { is } from "@julr/utils/is"; import { Mutex, withTimeout } from "async-mutex"; var Locks = class { /** * A map that will hold active locks for each key */ #locks = /* @__PURE__ */ new Map(); /** * For a given key, get or create a new lock * * @param key Key to get or create a lock for * @param timeout Time to wait to acquire the lock */ getOrCreateForKey(key, timeout) { let lock = this.#locks.get(key); if (!lock) { lock = new Mutex(); this.#locks.set(key, lock); } return is.number(timeout) ? withTimeout(lock, timeout) : lock; } release(key, releaser) { releaser(); this.#locks.delete(key); } }; export { Locks }; //# sourceMappingURL=chunk-TLJ7G3ZX.js.map