najm-cache
Advanced tools
+6
-0
@@ -15,2 +15,4 @@ import * as najm_core from 'najm-core'; | ||
| get(key: string): Promise<string | null>; | ||
| /** Get multiple values in key order. */ | ||
| getMany?(keys: string[]): Promise<Array<string | null>>; | ||
| /** | ||
@@ -101,2 +103,3 @@ * Set a value with optional TTL | ||
| get(key: string): Promise<string | null>; | ||
| getMany(keys: string[]): Promise<Array<string | null>>; | ||
| set(key: string, value: string, ttlMs?: number): Promise<void>; | ||
@@ -240,2 +243,3 @@ del(key: string): Promise<boolean>; | ||
| private config; | ||
| private pending; | ||
| constructor(config?: CacheConfig | null); | ||
@@ -259,2 +263,3 @@ /** | ||
| get(key: string): Promise<string | null>; | ||
| getMany(keys: string[]): Promise<Array<string | null>>; | ||
| /** | ||
@@ -359,2 +364,3 @@ * Set a value with optional TTL | ||
| get(key: string): Promise<string | null>; | ||
| getMany(keys: string[]): Promise<Array<string | null>>; | ||
| set(key: string, value: string, ttlMs?: number): Promise<void>; | ||
@@ -361,0 +367,0 @@ del(key: string): Promise<boolean>; |
+29
-3
@@ -48,2 +48,5 @@ var __defProp = Object.defineProperty; | ||
| } | ||
| async getMany(keys) { | ||
| return Promise.all(keys.map((key) => this.get(key))); | ||
| } | ||
| async set(key, value, ttlMs) { | ||
@@ -206,2 +209,7 @@ if (!this.data.has(key) && this.data.size >= this.maxKeys) { | ||
| } | ||
| async getMany(keys) { | ||
| if (keys.length === 0) | ||
| return []; | ||
| return this.getClient().mget(...keys.map((key) => this.prefixKey(key))); | ||
| } | ||
| async set(key, value, ttlMs) { | ||
@@ -298,2 +306,3 @@ const k = this.prefixKey(key); | ||
| config; | ||
| pending = /* @__PURE__ */ new Map(); | ||
| constructor(config) { | ||
@@ -360,2 +369,7 @@ this.config = config ?? { | ||
| } | ||
| getMany(keys) { | ||
| if (this.driver.getMany) | ||
| return this.driver.getMany(keys); | ||
| return Promise.all(keys.map((key) => this.driver.get(key))); | ||
| } | ||
| /** | ||
@@ -434,5 +448,16 @@ * Set a value with optional TTL | ||
| return cached; | ||
| const value = await factory(); | ||
| await this.setJson(key, value, ttlMs); | ||
| return value; | ||
| const existing = this.pending.get(key); | ||
| if (existing) | ||
| return existing; | ||
| const pending = (async () => { | ||
| const value = await factory(); | ||
| await this.setJson(key, value, ttlMs); | ||
| return value; | ||
| })(); | ||
| this.pending.set(key, pending); | ||
| try { | ||
| return await pending; | ||
| } finally { | ||
| this.pending.delete(key); | ||
| } | ||
| } | ||
@@ -469,2 +494,3 @@ /** | ||
| async destroy() { | ||
| this.pending.clear(); | ||
| if (this.driver.destroy) { | ||
@@ -471,0 +497,0 @@ await this.driver.destroy(); |
+2
-2
| { | ||
| "name": "najm-cache", | ||
| "version": "1.2.9", | ||
| "version": "1.2.10", | ||
| "description": "Unified cache plugin for najm framework - Memory and Redis drivers with auto-fallback", | ||
@@ -41,3 +41,3 @@ "type": "module", | ||
| "dependencies": { | ||
| "najm-core": "^1.2.10" | ||
| "najm-core": "^1.2.11" | ||
| }, | ||
@@ -44,0 +44,0 @@ "devDependencies": { |
26793
3.99%888
3.74%Updated