diff --git a/README.md b/README.md index a1ae1577..36c1621d 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ NOTES: * In case of multicaching, the store that will be checked for refresh is the one where the key will be found first (highest priority). * If the threshold is low and the worker function is slow, the key may expire and you may encounter a racing condition with updating values. * The background refresh mechanism currently does not support providing multiple keys to `wrap` function. +* If no `ttl` is set for the key, the refresh mechanism will not be triggered. For redis, the `ttl` is set to -1 by default. For example, pass the refreshThreshold to `caching` like this: @@ -197,4 +198,4 @@ for any new features or bug fixes. ## License -node-cache-manager is licensed under the [MIT license](./LICENSE). +node-cache-manager is licensed under the [MIT license](./LICENSE). \ No newline at end of file diff --git a/src/caching.ts b/src/caching.ts index be22c578..820f2fae 100644 --- a/src/caching.ts +++ b/src/caching.ts @@ -89,7 +89,7 @@ export async function caching( } else if (args?.refreshThreshold) { const cacheTTL = typeof ttl === 'function' ? ttl(value) : ttl; const remainingTtl = await store.ttl(key); - if (remainingTtl < args.refreshThreshold) { + if (remainingTtl !== -1 && remainingTtl < args.refreshThreshold) { fn().then((result) => store.set(key, result, cacheTTL)); } }