@axah/cache
Advanced tools
Comparing version 4.0.0 to 5.0.0
@@ -17,2 +17,5 @@ "use strict"; | ||
}, | ||
async cleanup() { | ||
// no-op | ||
}, | ||
}; | ||
@@ -19,0 +22,0 @@ } |
@@ -30,2 +30,8 @@ "use strict"; | ||
}, | ||
async cleanup() { | ||
for (const [, entry] of cache) { | ||
clearTimeout(entry.timeout); | ||
} | ||
cache.clear(); | ||
}, | ||
}; | ||
@@ -32,0 +38,0 @@ } |
@@ -42,2 +42,5 @@ "use strict"; | ||
}, | ||
async cleanup() { | ||
await client.quit(); | ||
}, | ||
}; | ||
@@ -44,0 +47,0 @@ } |
@@ -5,2 +5,3 @@ export type Backend = { | ||
delete(key: string): Promise<void>; | ||
cleanup(): Promise<void>; | ||
}; |
@@ -15,3 +15,5 @@ import type { Logger } from 'pino'; | ||
export default function createCache(options: CacheOptions, log: Logger): Promise<Backend>; | ||
export declare function cached<ARGS extends ReadonlyArray<any>, RESULT>(options: CacheOptions, log: Logger, ttl: number, fn: (...ARGS: ARGS) => Promise<RESULT> | RESULT, keyCreator: (...ARGS: ARGS) => string): (...ARGS: ARGS) => Promise<RESULT>; | ||
export declare function cached<ARGS extends ReadonlyArray<any>, RESULT>(options: CacheOptions, log: Logger, ttl: number, fn: (...ARGS: ARGS) => Promise<RESULT> | RESULT, keyCreator: (...ARGS: ARGS) => string, cleanup: { | ||
addListener(fn: () => Promise<void>): void; | ||
}): (...ARGS: ARGS) => Promise<RESULT>; | ||
export {}; |
@@ -25,6 +25,10 @@ "use strict"; | ||
exports.default = createCache; | ||
function cached(options, log, ttl, fn, keyCreator) { | ||
function cached(options, log, ttl, fn, keyCreator, cleanup) { | ||
const cacheAsync = createCache(options, log); | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
cacheAsync.catch(() => { }); // prevent unhandled promise rejection... | ||
cleanup.addListener(async () => { | ||
const cache = await cacheAsync; | ||
await cache.cleanup(); | ||
}); | ||
return async function doCached(...args) { | ||
@@ -31,0 +35,0 @@ const cache = await cacheAsync; |
{ | ||
"name": "@axah/cache", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Provides a cache either backed by RAM or redis", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13121
200