@open-mercato/cache-provider-memory 0.11.1-develop.8758.1.714940bb5c → 0.11.1-develop.8759.1.ac7b7cb1db
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -0
- package/dist/index.js.map +2 -2
- package/package.json +2 -2
- package/src/index.ts +6 -1
package/dist/index.js
CHANGED
|
@@ -96,6 +96,7 @@ function createMemoryStrategy(options) {
|
|
|
96
96
|
touchKey(key, entry);
|
|
97
97
|
return entry.value;
|
|
98
98
|
};
|
|
99
|
+
const peek = async (key) => store.get(key)?.value ?? null;
|
|
99
100
|
const set = async (key, value, options2) => {
|
|
100
101
|
const oldEntry = store.get(key);
|
|
101
102
|
if (oldEntry) {
|
|
@@ -180,6 +181,7 @@ function createMemoryStrategy(options) {
|
|
|
180
181
|
};
|
|
181
182
|
return {
|
|
182
183
|
get,
|
|
184
|
+
peek,
|
|
183
185
|
set,
|
|
184
186
|
has,
|
|
185
187
|
delete: deleteKey,
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import type { CacheStrategy, CacheEntry, CacheGetOptions, CacheSetOptions, CacheValue } from '@open-mercato/cache-provider-kit'\nimport { matchCacheKeyPattern } from '@open-mercato/cache-provider-kit/patterns'\n\nconst EXPIRED_SWEEP_WRITE_INTERVAL = 256\n\n/**\n * Upper bound on entries scanned by a single amortized sweep pass. Keeps the\n * sweep O(budget) instead of O(store size) so a large, hot store never pays a\n * full-store scan latency spike on the `set()` that crosses the sweep interval.\n * The scan walks from the LRU head, where expired-and-cold entries accumulate;\n * anything beyond the budget is reclaimed on a later sweep, on access, by LRU\n * eviction once the cap is exceeded, or by an explicit `cleanup()`.\n */\nconst EXPIRED_SWEEP_MAX_SCAN = 1_000\n\n/**\n * Default upper bound on the number of entries a memory cache retains.\n * Bounds memory for long-lived (process-wide) instances; LRU eviction drops\n * the least-recently-used entries once the cap is exceeded. Override via the\n * `maxEntries` option (or `CACHE_MEMORY_MAX_ENTRIES`); a non-positive value\n * disables the cap (unbounded \u2014 only safe for short-lived instances).\n */\nexport const DEFAULT_MEMORY_MAX_ENTRIES = 50_000\n\nfunction normalizeMaxEntries(raw?: number): number {\n if (raw === undefined) return DEFAULT_MEMORY_MAX_ENTRIES\n if (!Number.isFinite(raw) || raw <= 0) return Number.POSITIVE_INFINITY\n return Math.floor(raw)\n}\n\n/**\n * In-memory cache strategy with tag support.\n * Fast but data is lost when process restarts.\n *\n * Bounded by an LRU cap (`maxEntries`, default {@link DEFAULT_MEMORY_MAX_ENTRIES},\n * env-tunable via `CACHE_MEMORY_MAX_ENTRIES` resolved in the cache service) so a\n * process-shared instance (OM_BOOTSTRAP_CACHE, long-lived workers, memory-backed\n * CRUD list cache) cannot grow without limit on user-controllable key\n * cardinality. Recency is refreshed on read (Map re-insertion), the oldest\n * entries are evicted on write, and expired entries are reclaimed by an\n * amortized sweep every N writes \u2014 no per-instance timer, so the per-request\n * default stays leak-free (a per-instance setInterval would pin every request's\n * Maps for the process lifetime).\n */\nexport function createMemoryStrategy(options?: { defaultTtl?: number; maxEntries?: number }): CacheStrategy {\n const store = new Map<string, CacheEntry>()\n const tagIndex = new Map<string, Set<string>>() // tag -> Set of keys\n const defaultTtl = options?.defaultTtl\n const maxEntries = normalizeMaxEntries(options?.maxEntries)\n let writesSinceSweep = 0\n // Lightweight observability counters so operators can tell whether the bound\n // is actively protecting the process. Process-global for this instance, not\n // tenant-scoped; surfaced via stats().\n let evictions = 0\n let sweeps = 0\n let lastSweepReclaimed = 0\n\n function isExpired(entry: CacheEntry): boolean {\n if (entry.expiresAt === null) return false\n return Date.now() > entry.expiresAt\n }\n\n // LRU bookkeeping: re-insert on read so the most-recently-used entry moves\n // to the tail (Map preserves insertion order), mirroring the rbacDefaultCache\n // precedent; evictIfNeeded then drops from the head (least-recently-used).\n function touchKey(key: string, entry: CacheEntry): void {\n if (maxEntries === Number.POSITIVE_INFINITY) return\n store.delete(key)\n store.set(key, entry)\n }\n\n function evictIfNeeded(): void {\n if (maxEntries === Number.POSITIVE_INFINITY) return\n while (store.size > maxEntries) {\n const oldest = store.keys().next().value\n if (typeof oldest !== 'string') break\n const entry = store.get(oldest)\n store.delete(oldest)\n if (entry) removeFromTagIndex(oldest, entry.tags)\n evictions++\n }\n }\n\n function cleanupExpiredEntry(key: string, entry: CacheEntry): void {\n store.delete(key)\n // Remove from tag index\n for (const tag of entry.tags) {\n const keys = tagIndex.get(tag)\n if (keys) {\n keys.delete(key)\n if (keys.size === 0) {\n tagIndex.delete(tag)\n }\n }\n }\n }\n\n function addToTagIndex(key: string, tags: string[]): void {\n for (const tag of tags) {\n if (!tagIndex.has(tag)) {\n tagIndex.set(tag, new Set())\n }\n tagIndex.get(tag)!.add(key)\n }\n }\n\n function removeFromTagIndex(key: string, tags: string[]): void {\n for (const tag of tags) {\n const keys = tagIndex.get(tag)\n if (keys) {\n keys.delete(key)\n if (keys.size === 0) {\n tagIndex.delete(tag)\n }\n }\n }\n }\n\n // Amortized reclamation of already-expired entries. Runs every N writes\n // instead of on a timer, keeping the no-shared-state property that makes the\n // per-request default safe. Independent of the LRU cap so expired-but-cold\n // entries are reclaimed even when the store stays under `maxEntries`. The\n // scan is budgeted (EXPIRED_SWEEP_MAX_SCAN entries from the LRU head per pass)\n // so it stays O(budget) on large stores instead of scanning every entry.\n function sweepExpiredIfDue(): void {\n if (++writesSinceSweep < EXPIRED_SWEEP_WRITE_INTERVAL) return\n writesSinceSweep = 0\n sweeps++\n let scanned = 0\n let reclaimed = 0\n for (const [key, entry] of store.entries()) {\n if (scanned >= EXPIRED_SWEEP_MAX_SCAN) break\n scanned++\n if (isExpired(entry)) {\n cleanupExpiredEntry(key, entry)\n reclaimed++\n }\n }\n lastSweepReclaimed = reclaimed\n }\n\n const get = async (key: string, options?: CacheGetOptions): Promise<CacheValue | null> => {\n const entry = store.get(key)\n if (!entry) return null\n\n if (isExpired(entry)) {\n if (options?.returnExpired) {\n return entry.value\n }\n cleanupExpiredEntry(key, entry)\n return null\n }\n\n touchKey(key, entry)\n return entry.value\n }\n\n const set = async (key: string, value: CacheValue, options?: CacheSetOptions): Promise<void> => {\n // Remove old entry from tag index if it exists\n const oldEntry = store.get(key)\n if (oldEntry) {\n removeFromTagIndex(key, oldEntry.tags)\n }\n\n const ttl = options?.ttl ?? defaultTtl\n const tags = options?.tags || []\n const expiresAt = ttl ? Date.now() + ttl : null\n\n const entry: CacheEntry = {\n key,\n value,\n tags,\n expiresAt,\n createdAt: Date.now(),\n }\n\n store.set(key, entry)\n addToTagIndex(key, tags)\n sweepExpiredIfDue()\n evictIfNeeded()\n }\n\n const has = async (key: string): Promise<boolean> => {\n const entry = store.get(key)\n if (!entry) return false\n if (isExpired(entry)) {\n cleanupExpiredEntry(key, entry)\n return false\n }\n return true\n }\n\n const deleteKey = async (key: string): Promise<boolean> => {\n const entry = store.get(key)\n if (!entry) return false\n\n removeFromTagIndex(key, entry.tags)\n return store.delete(key)\n }\n\n const deleteByTags = async (tags: string[]): Promise<number> => {\n const keysToDelete = new Set<string>()\n\n // Collect all keys that have any of the specified tags\n for (const tag of tags) {\n const keys = tagIndex.get(tag)\n if (keys) {\n for (const key of keys) {\n keysToDelete.add(key)\n }\n }\n }\n\n // Delete all collected keys\n let deleted = 0\n for (const key of keysToDelete) {\n const success = await deleteKey(key)\n if (success) deleted++\n }\n\n return deleted\n }\n\n const clear = async (): Promise<number> => {\n const size = store.size\n store.clear()\n tagIndex.clear()\n return size\n }\n\n const keys = async (pattern?: string): Promise<string[]> => {\n const allKeys = Array.from(store.keys())\n if (!pattern) return allKeys\n return allKeys.filter((key) => matchCacheKeyPattern(key, pattern))\n }\n\n const stats = async (): Promise<{\n size: number\n expired: number\n evictions: number\n sweeps: number\n lastSweepReclaimed: number\n }> => {\n let expired = 0\n for (const entry of store.values()) {\n if (isExpired(entry)) {\n expired++\n }\n }\n return { size: store.size, expired, evictions, sweeps, lastSweepReclaimed }\n }\n\n const cleanup = async (): Promise<number> => {\n let removed = 0\n for (const [key, entry] of store.entries()) {\n if (isExpired(entry)) {\n cleanupExpiredEntry(key, entry)\n removed++\n }\n }\n return removed\n }\n\n return {\n get,\n set,\n has,\n delete: deleteKey,\n deleteByTags,\n clear,\n keys,\n stats,\n cleanup,\n }\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,4BAA4B;AAErC,MAAM,+BAA+B;AAUrC,MAAM,yBAAyB;AASxB,MAAM,6BAA6B;AAE1C,SAAS,oBAAoB,KAAsB;AACjD,MAAI,QAAQ,OAAW,QAAO;AAC9B,MAAI,CAAC,OAAO,SAAS,GAAG,KAAK,OAAO,EAAG,QAAO,OAAO;AACrD,SAAO,KAAK,MAAM,GAAG;AACvB;AAgBO,SAAS,qBAAqB,
|
|
4
|
+
"sourcesContent": ["import type { CacheStrategy, CacheEntry, CacheGetOptions, CacheSetOptions, CacheValue } from '@open-mercato/cache-provider-kit'\nimport { matchCacheKeyPattern } from '@open-mercato/cache-provider-kit/patterns'\n\nconst EXPIRED_SWEEP_WRITE_INTERVAL = 256\n\n/**\n * Upper bound on entries scanned by a single amortized sweep pass. Keeps the\n * sweep O(budget) instead of O(store size) so a large, hot store never pays a\n * full-store scan latency spike on the `set()` that crosses the sweep interval.\n * The scan walks from the LRU head, where expired-and-cold entries accumulate;\n * anything beyond the budget is reclaimed on a later sweep, on access, by LRU\n * eviction once the cap is exceeded, or by an explicit `cleanup()`.\n */\nconst EXPIRED_SWEEP_MAX_SCAN = 1_000\n\n/**\n * Default upper bound on the number of entries a memory cache retains.\n * Bounds memory for long-lived (process-wide) instances; LRU eviction drops\n * the least-recently-used entries once the cap is exceeded. Override via the\n * `maxEntries` option (or `CACHE_MEMORY_MAX_ENTRIES`); a non-positive value\n * disables the cap (unbounded \u2014 only safe for short-lived instances).\n */\nexport const DEFAULT_MEMORY_MAX_ENTRIES = 50_000\n\nfunction normalizeMaxEntries(raw?: number): number {\n if (raw === undefined) return DEFAULT_MEMORY_MAX_ENTRIES\n if (!Number.isFinite(raw) || raw <= 0) return Number.POSITIVE_INFINITY\n return Math.floor(raw)\n}\n\n/**\n * In-memory cache strategy with tag support.\n * Fast but data is lost when process restarts.\n *\n * Bounded by an LRU cap (`maxEntries`, default {@link DEFAULT_MEMORY_MAX_ENTRIES},\n * env-tunable via `CACHE_MEMORY_MAX_ENTRIES` resolved in the cache service) so a\n * process-shared instance (OM_BOOTSTRAP_CACHE, long-lived workers, memory-backed\n * CRUD list cache) cannot grow without limit on user-controllable key\n * cardinality. Recency is refreshed on read (Map re-insertion), the oldest\n * entries are evicted on write, and expired entries are reclaimed by an\n * amortized sweep every N writes \u2014 no per-instance timer, so the per-request\n * default stays leak-free (a per-instance setInterval would pin every request's\n * Maps for the process lifetime).\n */\nexport function createMemoryStrategy(options?: { defaultTtl?: number; maxEntries?: number }): CacheStrategy & {\n peek: (key: string) => Promise<CacheValue | null>\n} {\n const store = new Map<string, CacheEntry>()\n const tagIndex = new Map<string, Set<string>>() // tag -> Set of keys\n const defaultTtl = options?.defaultTtl\n const maxEntries = normalizeMaxEntries(options?.maxEntries)\n let writesSinceSweep = 0\n // Lightweight observability counters so operators can tell whether the bound\n // is actively protecting the process. Process-global for this instance, not\n // tenant-scoped; surfaced via stats().\n let evictions = 0\n let sweeps = 0\n let lastSweepReclaimed = 0\n\n function isExpired(entry: CacheEntry): boolean {\n if (entry.expiresAt === null) return false\n return Date.now() > entry.expiresAt\n }\n\n // LRU bookkeeping: re-insert on read so the most-recently-used entry moves\n // to the tail (Map preserves insertion order), mirroring the rbacDefaultCache\n // precedent; evictIfNeeded then drops from the head (least-recently-used).\n function touchKey(key: string, entry: CacheEntry): void {\n if (maxEntries === Number.POSITIVE_INFINITY) return\n store.delete(key)\n store.set(key, entry)\n }\n\n function evictIfNeeded(): void {\n if (maxEntries === Number.POSITIVE_INFINITY) return\n while (store.size > maxEntries) {\n const oldest = store.keys().next().value\n if (typeof oldest !== 'string') break\n const entry = store.get(oldest)\n store.delete(oldest)\n if (entry) removeFromTagIndex(oldest, entry.tags)\n evictions++\n }\n }\n\n function cleanupExpiredEntry(key: string, entry: CacheEntry): void {\n store.delete(key)\n // Remove from tag index\n for (const tag of entry.tags) {\n const keys = tagIndex.get(tag)\n if (keys) {\n keys.delete(key)\n if (keys.size === 0) {\n tagIndex.delete(tag)\n }\n }\n }\n }\n\n function addToTagIndex(key: string, tags: string[]): void {\n for (const tag of tags) {\n if (!tagIndex.has(tag)) {\n tagIndex.set(tag, new Set())\n }\n tagIndex.get(tag)!.add(key)\n }\n }\n\n function removeFromTagIndex(key: string, tags: string[]): void {\n for (const tag of tags) {\n const keys = tagIndex.get(tag)\n if (keys) {\n keys.delete(key)\n if (keys.size === 0) {\n tagIndex.delete(tag)\n }\n }\n }\n }\n\n // Amortized reclamation of already-expired entries. Runs every N writes\n // instead of on a timer, keeping the no-shared-state property that makes the\n // per-request default safe. Independent of the LRU cap so expired-but-cold\n // entries are reclaimed even when the store stays under `maxEntries`. The\n // scan is budgeted (EXPIRED_SWEEP_MAX_SCAN entries from the LRU head per pass)\n // so it stays O(budget) on large stores instead of scanning every entry.\n function sweepExpiredIfDue(): void {\n if (++writesSinceSweep < EXPIRED_SWEEP_WRITE_INTERVAL) return\n writesSinceSweep = 0\n sweeps++\n let scanned = 0\n let reclaimed = 0\n for (const [key, entry] of store.entries()) {\n if (scanned >= EXPIRED_SWEEP_MAX_SCAN) break\n scanned++\n if (isExpired(entry)) {\n cleanupExpiredEntry(key, entry)\n reclaimed++\n }\n }\n lastSweepReclaimed = reclaimed\n }\n\n const get = async (key: string, options?: CacheGetOptions): Promise<CacheValue | null> => {\n const entry = store.get(key)\n if (!entry) return null\n\n if (isExpired(entry)) {\n if (options?.returnExpired) {\n return entry.value\n }\n cleanupExpiredEntry(key, entry)\n return null\n }\n\n touchKey(key, entry)\n return entry.value\n }\n\n const peek = async (key: string): Promise<CacheValue | null> => store.get(key)?.value ?? null\n\n const set = async (key: string, value: CacheValue, options?: CacheSetOptions): Promise<void> => {\n // Remove old entry from tag index if it exists\n const oldEntry = store.get(key)\n if (oldEntry) {\n removeFromTagIndex(key, oldEntry.tags)\n }\n\n const ttl = options?.ttl ?? defaultTtl\n const tags = options?.tags || []\n const expiresAt = ttl ? Date.now() + ttl : null\n\n const entry: CacheEntry = {\n key,\n value,\n tags,\n expiresAt,\n createdAt: Date.now(),\n }\n\n store.set(key, entry)\n addToTagIndex(key, tags)\n sweepExpiredIfDue()\n evictIfNeeded()\n }\n\n const has = async (key: string): Promise<boolean> => {\n const entry = store.get(key)\n if (!entry) return false\n if (isExpired(entry)) {\n cleanupExpiredEntry(key, entry)\n return false\n }\n return true\n }\n\n const deleteKey = async (key: string): Promise<boolean> => {\n const entry = store.get(key)\n if (!entry) return false\n\n removeFromTagIndex(key, entry.tags)\n return store.delete(key)\n }\n\n const deleteByTags = async (tags: string[]): Promise<number> => {\n const keysToDelete = new Set<string>()\n\n // Collect all keys that have any of the specified tags\n for (const tag of tags) {\n const keys = tagIndex.get(tag)\n if (keys) {\n for (const key of keys) {\n keysToDelete.add(key)\n }\n }\n }\n\n // Delete all collected keys\n let deleted = 0\n for (const key of keysToDelete) {\n const success = await deleteKey(key)\n if (success) deleted++\n }\n\n return deleted\n }\n\n const clear = async (): Promise<number> => {\n const size = store.size\n store.clear()\n tagIndex.clear()\n return size\n }\n\n const keys = async (pattern?: string): Promise<string[]> => {\n const allKeys = Array.from(store.keys())\n if (!pattern) return allKeys\n return allKeys.filter((key) => matchCacheKeyPattern(key, pattern))\n }\n\n const stats = async (): Promise<{\n size: number\n expired: number\n evictions: number\n sweeps: number\n lastSweepReclaimed: number\n }> => {\n let expired = 0\n for (const entry of store.values()) {\n if (isExpired(entry)) {\n expired++\n }\n }\n return { size: store.size, expired, evictions, sweeps, lastSweepReclaimed }\n }\n\n const cleanup = async (): Promise<number> => {\n let removed = 0\n for (const [key, entry] of store.entries()) {\n if (isExpired(entry)) {\n cleanupExpiredEntry(key, entry)\n removed++\n }\n }\n return removed\n }\n\n return {\n get,\n peek,\n set,\n has,\n delete: deleteKey,\n deleteByTags,\n clear,\n keys,\n stats,\n cleanup,\n }\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,4BAA4B;AAErC,MAAM,+BAA+B;AAUrC,MAAM,yBAAyB;AASxB,MAAM,6BAA6B;AAE1C,SAAS,oBAAoB,KAAsB;AACjD,MAAI,QAAQ,OAAW,QAAO;AAC9B,MAAI,CAAC,OAAO,SAAS,GAAG,KAAK,OAAO,EAAG,QAAO,OAAO;AACrD,SAAO,KAAK,MAAM,GAAG;AACvB;AAgBO,SAAS,qBAAqB,SAEnC;AACA,QAAM,QAAQ,oBAAI,IAAwB;AAC1C,QAAM,WAAW,oBAAI,IAAyB;AAC9C,QAAM,aAAa,SAAS;AAC5B,QAAM,aAAa,oBAAoB,SAAS,UAAU;AAC1D,MAAI,mBAAmB;AAIvB,MAAI,YAAY;AAChB,MAAI,SAAS;AACb,MAAI,qBAAqB;AAEzB,WAAS,UAAU,OAA4B;AAC7C,QAAI,MAAM,cAAc,KAAM,QAAO;AACrC,WAAO,KAAK,IAAI,IAAI,MAAM;AAAA,EAC5B;AAKA,WAAS,SAAS,KAAa,OAAyB;AACtD,QAAI,eAAe,OAAO,kBAAmB;AAC7C,UAAM,OAAO,GAAG;AAChB,UAAM,IAAI,KAAK,KAAK;AAAA,EACtB;AAEA,WAAS,gBAAsB;AAC7B,QAAI,eAAe,OAAO,kBAAmB;AAC7C,WAAO,MAAM,OAAO,YAAY;AAC9B,YAAM,SAAS,MAAM,KAAK,EAAE,KAAK,EAAE;AACnC,UAAI,OAAO,WAAW,SAAU;AAChC,YAAM,QAAQ,MAAM,IAAI,MAAM;AAC9B,YAAM,OAAO,MAAM;AACnB,UAAI,MAAO,oBAAmB,QAAQ,MAAM,IAAI;AAChD;AAAA,IACF;AAAA,EACF;AAEA,WAAS,oBAAoB,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ACF;AAGA,QAAI,UAAU;AACd,eAAW,OAAO,cAAc;AAC9B,YAAM,UAAU,MAAM,UAAU,GAAG;AACnC,UAAI,QAAS;AAAA,IACf;AAEA,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,YAA6B;AACzC,UAAM,OAAO,MAAM;AACnB,UAAM,MAAM;AACZ,aAAS,MAAM;AACf,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,OAAO,YAAwC;AAC1D,UAAM,UAAU,MAAM,KAAK,MAAM,KAAK,CAAC;AACvC,QAAI,CAAC,QAAS,QAAO;AACrB,WAAO,QAAQ,OAAO,CAAC,QAAQ,qBAAqB,KAAK,OAAO,CAAC;AAAA,EACnE;AAEA,QAAM,QAAQ,YAMR;AACJ,QAAI,UAAU;AACd,eAAW,SAAS,MAAM,OAAO,GAAG;AAClC,UAAI,UAAU,KAAK,GAAG;AACpB;AAAA,MACF;AAAA,IACF;AACA,WAAO,EAAE,MAAM,MAAM,MAAM,SAAS,WAAW,QAAQ,mBAAmB;AAAA,EAC5E;AAEA,QAAM,UAAU,YAA6B;AAC3C,QAAI,UAAU;AACd,eAAW,CAAC,KAAK,KAAK,KAAK,MAAM,QAAQ,GAAG;AAC1C,UAAI,UAAU,KAAK,GAAG;AACpB,4BAAoB,KAAK,KAAK;AAC9B;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["keys", "options"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/cache-provider-memory",
|
|
3
|
-
"version": "0.11.1-develop.
|
|
3
|
+
"version": "0.11.1-develop.8759.1.ac7b7cb1db",
|
|
4
4
|
"description": "Stock memory cache factory for Open Mercato",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"directory": "packages/cache-provider-memory"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@open-mercato/cache-provider-kit": "0.11.1-develop.
|
|
32
|
+
"@open-mercato/cache-provider-kit": "0.11.1-develop.8759.1.ac7b7cb1db"
|
|
33
33
|
},
|
|
34
34
|
"stableVersion": "0.11.0"
|
|
35
35
|
}
|
package/src/index.ts
CHANGED
|
@@ -42,7 +42,9 @@ function normalizeMaxEntries(raw?: number): number {
|
|
|
42
42
|
* default stays leak-free (a per-instance setInterval would pin every request's
|
|
43
43
|
* Maps for the process lifetime).
|
|
44
44
|
*/
|
|
45
|
-
export function createMemoryStrategy(options?: { defaultTtl?: number; maxEntries?: number }): CacheStrategy {
|
|
45
|
+
export function createMemoryStrategy(options?: { defaultTtl?: number; maxEntries?: number }): CacheStrategy & {
|
|
46
|
+
peek: (key: string) => Promise<CacheValue | null>
|
|
47
|
+
} {
|
|
46
48
|
const store = new Map<string, CacheEntry>()
|
|
47
49
|
const tagIndex = new Map<string, Set<string>>() // tag -> Set of keys
|
|
48
50
|
const defaultTtl = options?.defaultTtl
|
|
@@ -155,6 +157,8 @@ export function createMemoryStrategy(options?: { defaultTtl?: number; maxEntries
|
|
|
155
157
|
return entry.value
|
|
156
158
|
}
|
|
157
159
|
|
|
160
|
+
const peek = async (key: string): Promise<CacheValue | null> => store.get(key)?.value ?? null
|
|
161
|
+
|
|
158
162
|
const set = async (key: string, value: CacheValue, options?: CacheSetOptions): Promise<void> => {
|
|
159
163
|
// Remove old entry from tag index if it exists
|
|
160
164
|
const oldEntry = store.get(key)
|
|
@@ -263,6 +267,7 @@ export function createMemoryStrategy(options?: { defaultTtl?: number; maxEntries
|
|
|
263
267
|
|
|
264
268
|
return {
|
|
265
269
|
get,
|
|
270
|
+
peek,
|
|
266
271
|
set,
|
|
267
272
|
has,
|
|
268
273
|
delete: deleteKey,
|