Skip to content

Fix up docs/comments related to maxAge. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
previously accessed the underlying cache interface. The main interface
of this module has only changed in that the options it accepts when
creating the cache need to now conform to v11 of `lru-cache` instead of
v6. The v6 `maxAge` option, if given, will be coerced to `ttl` to match
v11.
v6. The v6 `maxAge` option, if given, will result in an error being thrown.
- **BREAKING**: The `delete()` method now returns `true` if the passed key was
removed from the cache and `false` if not, matching the v11 `delete()`
interface. Previously, `undefined` was returned in both cases.
Expand Down
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,17 @@ npm install
To import:

```js
import { LruCache } from '@digitalbazaar/lru-memoize';
// or
const { LruCache } = require('@digitalbazaar/lru-memoize');
import {LruCache} from '@digitalbazaar/lru-memoize';
```

The memoized `LruCache` constructor passes any options given to it through to
the `lru-cache` constructor, so see that repo for the full list of cache
the `lru-cache` constructor, so see that repo for the full list of cache
management options. Commonly used ones include:

* `max` (default: 100) - maximum size of the cache.
* `maxAge` (default: 5 sec/5000 ms) - maximum age of an item in ms.
* `max` (default: 1000) - maximum size of the cache.
* `ttl` - maximum age of an item in ms.
* `updateAgeOnGet` (default: `false`) - When using time-expiring entries with
`maxAge`, setting this to true will make each entry's effective time update to
`ttl`, setting this to true will make each entry's effective time update to
the current time whenever it is retrieved from cache, thereby extending the
expiration date of the entry.

Expand All @@ -59,10 +57,10 @@ For example, say you have a function `fetchStatus()` that retrieves a result fro
`delay()` wait). To cache the result of this function:

```js
import { LruCache } from '@digitalbazaar/lru-memoize';
import {LruCache} from '@digitalbazaar/lru-memoize';

// Cache expiration/TTL: 5 seconds
const myCache = new LruCache({ maxAge: 5000 });
// cache expiration/TTL: 5 seconds
const myCache = new LruCache({ttl: 5000});

async function fetchStatus() {
// simulate an async task
Expand All @@ -81,7 +79,7 @@ const result = await myCache.memoize({
const url = 'https://api.example';
const result = await myCache.memoize({
key: 'myResults',
fn: async () => fetchMyResultsFromWeb({ url })
fn: async () => fetchMyResultsFromWeb({url})
});
```

Expand Down
2 changes: 1 addition & 1 deletion lib/LruCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {LRUCache as LRU} from 'lru-cache';
* @param {Function} [cacheOptions.sizeCalculation] - A function that will
* calculate the size of an item; see lru-cache documentation.
* @param {boolean} [cacheOptions.updateAgeOnGet=false] - When using
* time-expiring entries with maxAge, setting this to true will make
* time-expiring entries with `ttl`, setting this to true will make
* each entry's effective time update to the current time whenever it is
* retrieved from cache, thereby extending the expiration date of the entry.
* @param {boolean} [cacheOptions.disposeOnSettle=false] - When set to true
Expand Down