Skip to content

Commit

Permalink
fix(refreshThreshold): don't run if ttl is -1 (#604)
Browse files Browse the repository at this point in the history
* fix(refreshThreshold): don't run if ttl is -1

* chore: update docs

* chore: revert auto-formatting

* chore(docs): revert fmt

---------

Co-authored-by: Jared Wray <me@jaredwray.com>
  • Loading branch information
mihirgupta0900 and jaredwray authored Oct 11, 2023
1 parent 9a703de commit 66246e9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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).
2 changes: 1 addition & 1 deletion src/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function caching<S extends Store, T extends object = never>(
} 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<T>(key, result, cacheTTL));
}
}
Expand Down

0 comments on commit 66246e9

Please sign in to comment.