cache-manager
Advanced tools
Comparing version 5.5.2 to 5.5.3
@@ -30,2 +30,14 @@ import { type MemoryCache, type MemoryConfig, type MemoryStore } from './stores/index.js'; | ||
export type WrapTTL<T> = Milliseconds | ((v: T) => Milliseconds); | ||
export type ErrorEvent<T = never> = { | ||
operation: 'get' | 'set' | 'del' | 'wrap' | 'reset'; | ||
error: unknown; | ||
key?: string; | ||
data?: T; | ||
} | { | ||
operation: 'mget' | 'mset' | 'mdel'; | ||
error: unknown; | ||
keys: string[]; | ||
data?: T[]; | ||
}; | ||
export type ErrorEventHandler<T = unknown> = (event: ErrorEvent<T>) => void; | ||
export type Cache<S extends Store = Store> = { | ||
@@ -37,3 +49,4 @@ store: S; | ||
reset: () => Promise<void>; | ||
on: (event: 'error', handler: (error: Error) => void) => void; | ||
on: <T>(event: 'error', handler: ErrorEventHandler<T>) => void; | ||
removeListener: <T>(event: 'error', handler: ErrorEventHandler<T>) => void; | ||
wrap<T>(key: string, function_: () => Promise<T>, ttl?: WrapTTL<T>, refreshThreshold?: Milliseconds): Promise<T>; | ||
@@ -40,0 +53,0 @@ }; |
@@ -45,3 +45,4 @@ "use strict"; | ||
const value = await store.get(key).catch(error => { | ||
eventEmitter.emit('error', error); | ||
const errorEvent = { error, key, operation: 'wrap' }; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
@@ -51,3 +52,8 @@ if (value === undefined) { | ||
const cacheTtl = typeof ttl === 'function' ? ttl(result) : ttl; | ||
await store.set(key, result, cacheTtl).catch(error => eventEmitter.emit('error', error)); | ||
await store.set(key, result, cacheTtl).catch(error => { | ||
const errorEvent = { | ||
error, key, operation: 'wrap', data: result, | ||
}; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
return result; | ||
@@ -62,3 +68,6 @@ } | ||
.catch(async (error) => { | ||
eventEmitter.emit('error', error); | ||
const errorEvent = { | ||
error, key, operation: 'wrap', data: value, | ||
}; | ||
eventEmitter.emit('error', errorEvent); | ||
eventEmitter.emit('onBackgroundRefreshError', error); | ||
@@ -84,2 +93,3 @@ if (arguments_?.onBackgroundRefreshError) { | ||
on: (event, handler) => eventEmitter.on('error', handler), | ||
removeListener: (event, handler) => eventEmitter.removeListener(event, handler), | ||
}; | ||
@@ -86,0 +96,0 @@ } |
@@ -13,2 +13,5 @@ "use strict"; | ||
const eventEmitter = new eventemitter3_1.default(); | ||
for (const cache of caches) { | ||
cache.on('error', event => eventEmitter.emit('error', event)); | ||
} | ||
const get = async (key) => { | ||
@@ -24,3 +27,4 @@ for (const cache of caches) { | ||
catch (error) { | ||
eventEmitter.emit('error', error); | ||
const errorEvent = { error, key, operation: 'get' }; | ||
eventEmitter.emit('error', errorEvent); | ||
} | ||
@@ -30,3 +34,8 @@ } | ||
const set = async (key, data, ttl) => { | ||
await Promise.all(caches.map(async (cache) => cache.set(key, data, ttl))).catch(error => eventEmitter.emit('error', error)); | ||
await Promise.all(caches.map(async (cache) => cache.set(key, data, ttl))).catch(error => { | ||
const errorEvent = { | ||
error, key, operation: 'set', data, | ||
}; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
}; | ||
@@ -37,3 +46,6 @@ return { | ||
async del(key) { | ||
await Promise.all(caches.map(async (cache) => cache.del(key))).catch(error => eventEmitter.emit('error', error)); | ||
await Promise.all(caches.map(async (cache) => cache.del(key))).catch(error => { | ||
const errorEvent = { error, key, operation: 'del' }; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
}, | ||
@@ -52,3 +64,4 @@ async wrap(key, function_, ttl, refreshThreshold) { | ||
catch (error) { | ||
eventEmitter.emit('error', error); | ||
const errorEvent = { error, key, operation: 'wrap' }; | ||
eventEmitter.emit('error', errorEvent); | ||
} | ||
@@ -63,3 +76,8 @@ } | ||
const cacheTtl = typeof ttl === 'function' ? ttl(value) : ttl; | ||
await Promise.all(caches.slice(0, i).map(async (cache) => cache.set(key, value, cacheTtl))).then().catch(error => eventEmitter.emit('error', error)); | ||
await Promise.all(caches.slice(0, i).map(async (cache) => cache.set(key, value, cacheTtl))).then().catch(error => { | ||
const errorEvent = { | ||
error, key, operation: 'wrap', data: value, | ||
}; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
await caches[i].wrap(key, function_, ttl, refreshThreshold).then(); // Call wrap for store for internal refreshThreshold logic, see: src/caching.ts caching.wrap | ||
@@ -69,3 +87,6 @@ return value; | ||
async reset() { | ||
await Promise.all(caches.map(async (x) => x.reset())).catch(error => eventEmitter.emit('error', error)); | ||
await Promise.all(caches.map(async (x) => x.reset())).catch(error => { | ||
const errorEvent = { error, key: '', operation: 'reset' }; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
}, | ||
@@ -88,3 +109,4 @@ async mget(...keys) { | ||
catch (error) { | ||
eventEmitter.emit('error', error); | ||
const errorEvent = { error, keys, operation: 'mget' }; | ||
eventEmitter.emit('error', errorEvent); | ||
} | ||
@@ -95,9 +117,20 @@ } | ||
async mset(arguments_, ttl) { | ||
await Promise.all(caches.map(async (cache) => cache.store.mset(arguments_, ttl))).catch(error => eventEmitter.emit('error', error)); | ||
await Promise.all(caches.map(async (cache) => cache.store.mset(arguments_, ttl))).catch(error => { | ||
const keys = arguments_.map(([key]) => key); | ||
const data = arguments_.map(([, value]) => value); | ||
const errorEvent = { | ||
error, keys, data, operation: 'mset', | ||
}; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
}, | ||
async mdel(...keys) { | ||
await Promise.all(caches.map(async (cache) => cache.store.mdel(...keys))) | ||
.catch(error => eventEmitter.emit('error', error)); | ||
.catch(error => { | ||
const errorEvent = { error, keys, operation: 'mdel' }; | ||
eventEmitter.emit('error', errorEvent); | ||
}); | ||
}, | ||
on: (event, handler) => eventEmitter.on('error', handler), | ||
removeListener: (event, handler) => eventEmitter.removeListener(event, handler), | ||
}; | ||
@@ -104,0 +137,0 @@ } |
{ | ||
"name": "cache-manager", | ||
"version": "5.5.2", | ||
"version": "5.5.3", | ||
"description": "Cache module for Node.js", | ||
@@ -11,9 +11,2 @@ "main": "dist/index.js", | ||
], | ||
"scripts": { | ||
"build": "tsc -p tsconfig.build.json", | ||
"clean": "rimraf ./dist ./coverage ./node_modules ./package-lock.json ./yarn.lock ./pnpm-lock.yaml", | ||
"test": "xo --fix && vitest run --coverage", | ||
"test:ci": "xo && vitest run", | ||
"prepare": "pnpm build" | ||
}, | ||
"repository": { | ||
@@ -69,3 +62,9 @@ "type": "git", | ||
] | ||
}, | ||
"scripts": { | ||
"build": "rimraf ./dist && tsc -p tsconfig.build.json", | ||
"clean": "rimraf ./dist ./coverage ./node_modules ./package-lock.json ./yarn.lock ./pnpm-lock.yaml", | ||
"test": "xo --fix && vitest run --coverage", | ||
"test:ci": "xo && vitest run" | ||
} | ||
} | ||
} |
@@ -107,9 +107,9 @@ # cache-manager | ||
```typescript | ||
import { createCache, memoryStore } from 'node-cache-manager'; | ||
import { createCache, memoryStore } from 'cache-manager'; | ||
// Create memory cache synchronously | ||
const memoryCache = createCache(memoryStore(), { | ||
const memoryCache = createCache(memoryStore({ | ||
max: 100, | ||
ttl: 10 * 1000 /*milliseconds*/, | ||
}); | ||
})); | ||
@@ -116,0 +116,0 @@ // Default parameter in function |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31327
13
435