Skip to content

Commit

Permalink
[minor] allow for infinite staleness (#28)
Browse files Browse the repository at this point in the history
* [minor] allow for infinite staleness

* Update index.js

* Update README.md

Co-authored-by: Jacob Page <[email protected]>

* Update lib/multi-level.js

Co-authored-by: Jacob Page <[email protected]>

* Update multi-level.js

* Update multi-level.js

Co-authored-by: Jacob Page <[email protected]>
  • Loading branch information
runruh-godaddy and jpage-godaddy authored Jan 10, 2022
1 parent 39c5ce3 commit 4bd3d47
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ You can instantiate the cache with 3 configurable options: `maxAge`,
`maxStaleness`, and `fsCachePath`.

- `maxAge`: The duration, in milliseconds, before a cached item expires
- `maxStaleness`: The duration, in milliseconds, in which expired cache
items are still served
- `maxStaleness`: (Optional) The duration, in milliseconds, in which expired cache
items are still served. Defaults to 0, meaning never serve data past expiration. Can also be set to `Infinity`, meaning always give at least a cached version.
- `fsCachePath`: (Optional) file path to create a file-system based cache
- `shouldCache`: (Optional) a function to determine whether or not you will
cache a found item
Expand Down
2 changes: 1 addition & 1 deletion lib/multi-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function getValue(opts, key, defaultValue) {
*
* @param {Object} opts Options for the Client instance
* @param {Number} [opts.maxAge=600000] The duration, in milliseconds, before a cached item expires (defaults to 600,000)
* @param {Number} [opts.maxStaleness=0] The duration, in milliseconds, in which expired cache items are still served
* @param {Number} [opts.maxStaleness=0] The duration, in milliseconds, in which expired cache items are still served. Supports Infinity.
* @param {ShouldCache} [opts.shouldCache=()=>true] Function to determine whether to not we should cache the result
* @param {Cache[]} opts.caches An Array of cache objects. See `./fs` and `./memory` for sample caches.
*/
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ describe('Out of Band cache', () => {
assume(expired.value).to.equal('milk');
});

it('refreshes on an expired item, but returns stale if infinite staleness', async function () {
const fridge = new Cache({ maxAge: -10, maxStaleness: Infinity }); // items are immediately stale

await fridge.get('old milk', {}, async () => 'milk');
await sleep(100);
const expired = await fridge.get('old milk', { }, async () => { throw new Error('gross'); });

assume(expired.fromCache).is.truthy();
assume(expired.value).to.equal('milk');
});

it('can override the cache-level maxStaleness', async function () {
const fridge = new Cache({ maxAge: -10, maxStaleness: -10 }); // items are immediately stale

Expand Down

0 comments on commit 4bd3d47

Please sign in to comment.