@isaacs/ttlcache 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -63,7 +63,7 @@ timer in this way will of course prevent anything from expiring.
63
63
 
64
64
  Default export is the `TTLCache` class.
65
65
 
66
- ### `new TTLCache({ ttl, max = Infinty, updateAgeOnGet = false, noUpdateTTL = false, noDisposeOnSet = false })`
66
+ ### `new TTLCache({ ttl, max = Infinty, updateAgeOnGet = false, checkAgeOnGet = false, noUpdateTTL = false, noDisposeOnSet = false })`
67
67
 
68
68
  Create a new `TTLCache` object.
69
69
 
@@ -77,6 +77,11 @@ Create a new `TTLCache` object.
77
77
  call.
78
78
  * `updateAgeOnGet` Should the age of an item be updated when it is
79
79
  retrieved? Defaults to `false`. Overridable on the `get()` method.
80
+ * `checkAgeOnGet` Check the TTL whenever an item is retrieved
81
+ with `get()`. If the item is past its ttl, but the timer has
82
+ not yet fired, then delete it and return undefined. By default,
83
+ the cache will return a value if it has one, even if it is
84
+ technically beyond its TTL.
80
85
  * `noUpdateTTL` Should setting a new value for an existing key leave the
81
86
  TTL unchanged? Defaults to `false`. Overridable on the `set()` method.
82
87
  (Note that TTL is _always_ updated if the item is expired, since that is
@@ -113,14 +118,18 @@ Store a value in the cache for the specified time.
113
118
 
114
119
  Returns the cache object.
115
120
 
116
- ### `cache.get(key, {updateAgeOnGet, ttl} = {})`
121
+ ### `cache.get(key, {updateAgeOnGet, checkAgeOnGet, ttl} = {})`
117
122
 
118
123
  Get an item stored in the cache. Returns `undefined` if the item is not in
119
124
  the cache (including if it has expired and been purged).
120
125
 
121
- If `updateAgeOnGet` is `true`, then re-add the item into the cache with the
122
- updated `ttl` value. Both options default to the settings on the
123
- constructor.
126
+ If `updateAgeOnGet` is `true`, then re-add the item into the
127
+ cache with the updated `ttl` value. All options default to the
128
+ settings on the constructor.
129
+
130
+ If `checkAgeOnGet`, then an item will be deleted if it is found
131
+ to be beyond its TTL, which can happen if the setTimeout timer
132
+ has not yet fired to trigger its expiration.
124
133
 
125
134
  Note that using `updateAgeOnGet` _can_ effectively simulate a
126
135
  "least-recently-used" type of algorithm, by repeatedly updating
package/index.d.ts CHANGED
@@ -6,6 +6,13 @@
6
6
  declare class TTLCache<K, V> implements Iterable<[K, V]> {
7
7
  constructor(options?: TTLCache.Options<K, V>)
8
8
 
9
+ ttl: number
10
+ max: number
11
+ updateAgeOnGet: boolean
12
+ checkAgeOnGet: boolean
13
+ noUpdateTTL: boolean
14
+ noDisposeOnSet: boolean
15
+
9
16
  /**
10
17
  * The total number of items held in the cache at the current moment.
11
18
  */
@@ -142,6 +149,19 @@ declare namespace TTLCache {
142
149
  */
143
150
  updateAgeOnGet?: boolean
144
151
 
152
+ /**
153
+ * In the event that an item's expiration timer hasn't yet fired,
154
+ * and an attempt is made to get() it, then return undefined and
155
+ * delete it, rather than returning the cached value.
156
+ *
157
+ * By default, items are only expired when their timer fires, so there's
158
+ * a bit of a "best effort" expiration, and the cache will return a value
159
+ * if it has one, even if it's technically stale.
160
+ *
161
+ * @default false
162
+ */
163
+ checkAgeOnGet?: boolean
164
+
145
165
  /**
146
166
  * Do not call dispose() function when overwriting a key with a new value
147
167
  *
@@ -182,10 +202,25 @@ declare namespace TTLCache {
182
202
 
183
203
  type GetOptions = {
184
204
  /**
185
- * Update the age of item being retrieved.
205
+ * Update the age of items on cache.get(), renewing their TTL
206
+ *
207
+ * @default false
186
208
  */
187
209
  updateAgeOnGet?: boolean
188
210
 
211
+ /**
212
+ * In the event that an item's expiration timer hasn't yet fired,
213
+ * and an attempt is made to get() it, then return undefined and
214
+ * delete it, rather than returning the cached value.
215
+ *
216
+ * By default, items are only expired when their timer fires, so there's
217
+ * a bit of a "best effort" expiration, and the cache will return a value
218
+ * if it has one, even if it's technically stale.
219
+ *
220
+ * @default false
221
+ */
222
+ checkAgeOnGet?: boolean
223
+
189
224
  /**
190
225
  * Set new TTL, applied only when `updateAgeOnGet` is true
191
226
  */
package/index.js CHANGED
@@ -20,6 +20,7 @@ class TTLCache {
20
20
  max = Infinity,
21
21
  ttl,
22
22
  updateAgeOnGet = false,
23
+ checkAgeOnGet = false,
23
24
  noUpdateTTL = false,
24
25
  dispose,
25
26
  noDisposeOnSet = false,
@@ -40,9 +41,10 @@ class TTLCache {
40
41
  }
41
42
  this.ttl = ttl
42
43
  this.max = max
43
- this.updateAgeOnGet = updateAgeOnGet
44
- this.noUpdateTTL = noUpdateTTL
45
- this.noDisposeOnSet = noDisposeOnSet
44
+ this.updateAgeOnGet = !!updateAgeOnGet
45
+ this.checkAgeOnGet = !!checkAgeOnGet
46
+ this.noUpdateTTL = !!noUpdateTTL
47
+ this.noDisposeOnSet = !!noDisposeOnSet
46
48
  if (dispose !== undefined) {
47
49
  if (typeof dispose !== 'function') {
48
50
  throw new TypeError('dispose must be function if set')
@@ -54,7 +56,7 @@ class TTLCache {
54
56
  this.timerExpiration = undefined
55
57
  }
56
58
 
57
- setTimer (expiration, ttl) {
59
+ setTimer(expiration, ttl) {
58
60
  if (this.timerExpiration < expiration) {
59
61
  return
60
62
  }
@@ -93,9 +95,11 @@ class TTLCache {
93
95
 
94
96
  /* istanbul ignore next */
95
97
  cancelTimers() {
96
- process.emitWarning('TTLCache.cancelTimers has been renamed to ' +
97
- 'TTLCache.cancelTimer (no "s"), and will be removed in the next ' +
98
- 'major version update')
98
+ process.emitWarning(
99
+ 'TTLCache.cancelTimers has been renamed to ' +
100
+ 'TTLCache.cancelTimer (no "s"), and will be removed in the next ' +
101
+ 'major version update'
102
+ )
99
103
  return this.cancelTimer()
100
104
  }
101
105
 
@@ -188,9 +192,17 @@ class TTLCache {
188
192
 
189
193
  get(
190
194
  key,
191
- { updateAgeOnGet = this.updateAgeOnGet, ttl = this.ttl } = {}
195
+ {
196
+ updateAgeOnGet = this.updateAgeOnGet,
197
+ ttl = this.ttl,
198
+ checkAgeOnGet = this.checkAgeOnGet,
199
+ } = {}
192
200
  ) {
193
201
  const val = this.data.get(key)
202
+ if (checkAgeOnGet && this.getRemainingTTL(key) === 0) {
203
+ this.delete(key)
204
+ return undefined
205
+ }
194
206
  if (updateAgeOnGet) {
195
207
  this.setTTL(key, ttl)
196
208
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isaacs/ttlcache",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "files": [
5
5
  "index.js",
6
6
  "index.d.ts"