1
1
/*!
2
- * Copyright (c) 2020-2023 Digital Bazaar, Inc. All rights reserved.
2
+ * Copyright (c) 2020-2025 Digital Bazaar, Inc. All rights reserved.
3
3
*/
4
- import LRU from 'lru-cache' ;
4
+ import { LRUCache as LRU } from 'lru-cache' ;
5
5
6
6
/**
7
7
* LruCache uses the npm module `lru-cache` to memoize promises.
@@ -10,8 +10,15 @@ import LRU from 'lru-cache';
10
10
* @see https://en.wikipedia.org/wiki/Memoization
11
11
* @param {object } cacheOptions - Options for `lru-cache`.
12
12
* See the npm docs for more options.
13
- * @param {number } [cacheOptions.max] - The max size of the cache.
14
- * @param {number } [cacheOptions.maxAge] - The maxAge of an item in ms.
13
+ * @param {number } [cacheOptions.max=1000] - The max number of items in the
14
+ * cache. If no `max` is specified and no `maxSize` is specified, then `max`
15
+ * will default to `1000`, ensuring the cache is bounded.
16
+ * @param {number } [cacheOptions.maxSize] - An optional max size for the cache;
17
+ * if provided, then the size of each item must be calculated via a provided
18
+ * `sizeCalculation` function.
19
+ * @param {number } [cacheOptions.ttl] - The time-to-live of an item in ms.
20
+ * @param {Function } [cacheOptions.sizeCalculation] - A function that will
21
+ * calculate the size of an item; see lru-cache documentation.
15
22
* @param {boolean } [cacheOptions.updateAgeOnGet=false] - When using
16
23
* time-expiring entries with maxAge, setting this to true will make
17
24
* each entry's effective time update to the current time whenever it is
@@ -23,9 +30,13 @@ import LRU from 'lru-cache';
23
30
* @returns {LruCache } The class.
24
31
*/
25
32
export class LruCache {
26
- constructor ( cacheOptions = { } ) {
27
- this . options = cacheOptions ;
28
- this . cache = new LRU ( cacheOptions ) ;
33
+ constructor ( cacheOptions = { max : 1000 } ) {
34
+ if ( cacheOptions . maxAge !== undefined ) {
35
+ throw new TypeError (
36
+ '"cacheOptions.maxAge" is no longer supported; use "ttl" instead.' ) ;
37
+ }
38
+ this . options = { max : 1000 , ...cacheOptions } ;
39
+ this . cache = new LRU ( this . options ) ;
29
40
}
30
41
31
42
/**
@@ -36,7 +47,7 @@ export class LruCache {
36
47
* @returns {undefined }
37
48
*/
38
49
delete ( key ) {
39
- return this . cache . del ( key ) ;
50
+ return this . cache . delete ( key ) ;
40
51
}
41
52
42
53
/**
@@ -61,24 +72,24 @@ export class LruCache {
61
72
// cache miss
62
73
const cacheOptions = { ...this . options , ...options } ;
63
74
promise = fn ( ) ;
64
- // this version only supports `maxAge` and `disposeOnSettle`; a future
65
- // version will support more cache options
66
- const { maxAge } = cacheOptions ;
67
- this . cache . set ( key , promise , maxAge ) ;
75
+ // this version only supports passing `ttl` through to the underlying
76
+ // lru-cache instance; a future version will support more cache options
77
+ const { ttl } = cacheOptions ;
78
+ this . cache . set ( key , promise , { ttl } ) ;
68
79
69
80
try {
70
81
await promise ;
71
82
} catch ( e ) {
72
83
// if the promise rejects, delete it if the cache entry hasn't changed
73
84
if ( promise === this . cache . get ( key ) ) {
74
- this . cache . del ( key ) ;
85
+ this . cache . delete ( key ) ;
75
86
}
76
87
throw e ;
77
88
}
78
89
79
90
// dispose promise once settled (provided the cache entry hasn't changed)
80
91
if ( cacheOptions . disposeOnSettle && promise === this . cache . get ( key ) ) {
81
- this . cache . del ( key ) ;
92
+ this . cache . delete ( key ) ;
82
93
}
83
94
84
95
return promise ;
0 commit comments