@isaacs/ttlcache 1.0.5 → 1.1.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.
Files changed (3) hide show
  1. package/index.d.ts +5 -0
  2. package/index.js +39 -42
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -58,6 +58,11 @@ declare class TTLCache<K, V> implements Iterable<[K, V]> {
58
58
  */
59
59
  public getRemainingTTL(key: K): number
60
60
 
61
+ /**
62
+ * Set the ttl explicitly to a value, defaulting to the TTL set on the ctor
63
+ */
64
+ public setTTL(key: K, ttl?: number): void
65
+
61
66
  /**
62
67
  * Return a generator yielding `[key, value]` pairs, from soonest expiring
63
68
  * to latest expiring. (Items expiring at the same time are walked in insertion order.)
package/index.js CHANGED
@@ -56,6 +56,29 @@ class TTLCache {
56
56
  }
57
57
  }
58
58
 
59
+ setTTL (key, ttl = this.ttl) {
60
+ const current = this.expirationMap.get(key)
61
+ if (current !== undefined) {
62
+ // remove from the expirations list, so it isn't purged
63
+ const exp = this.expirations[current]
64
+ if (!exp || exp.length <= 1) {
65
+ delete this.expirations[current]
66
+ } else {
67
+ this.expirations[current] = exp.filter(k => k !== key)
68
+ }
69
+ }
70
+
71
+ const expiration = Math.floor(now() + ttl)
72
+ this.expirationMap.set(key, expiration)
73
+ if (!this.expirations[expiration]) {
74
+ const t = setTimeout(() => this.purgeStale(), ttl)
75
+ /* istanbul ignore else - affordance for non-node envs */
76
+ if (t.unref) t.unref()
77
+ this.expirations[expiration] = []
78
+ }
79
+ this.expirations[expiration].push(key)
80
+ }
81
+
59
82
  set(
60
83
  key,
61
84
  val,
@@ -68,49 +91,27 @@ class TTLCache {
68
91
  if (!isPosInt(ttl)) {
69
92
  throw new TypeError('ttl must be positive integer')
70
93
  }
71
- const current = this.expirationMap.get(key)
72
- const time = now()
73
- const oldValue =
74
- current === undefined ? undefined : this.data.get(key)
75
- if (current !== undefined) {
76
- // we aren't updating the ttl, so just set the data
77
- if (noUpdateTTL && current > time) {
78
- if (oldValue !== val) {
79
- this.data.set(key, val)
80
- if (!noDisposeOnSet) {
81
- this.dispose(oldValue, key, 'set')
82
- }
83
- }
84
- return this
85
- } else {
86
- // just delete from expirations list, since we're about to
87
- // add to data and expirationsMap anyway
88
- const exp = this.expirations[current]
89
- if (!exp || exp.length <= 1) {
90
- delete this.expirations[current]
91
- } else {
92
- this.expirations[current] = exp.filter(k => k !== key)
94
+ if (this.expirationMap.has(key)) {
95
+ if (!noUpdateTTL) {
96
+ this.setTTL(key, ttl)
97
+ }
98
+ // has old value
99
+ const oldValue = this.data.get(key)
100
+ if (oldValue !== val) {
101
+ this.data.set(key, val)
102
+ if (!noDisposeOnSet) {
103
+ this.dispose(oldValue, key, 'set')
93
104
  }
94
105
  }
106
+ } else {
107
+ this.setTTL(key, ttl)
108
+ this.data.set(key, val)
95
109
  }
96
- const expiration = Math.ceil(time + ttl)
97
- this.expirationMap.set(key, expiration)
98
- this.data.set(key, val)
99
- if (!this.expirations[expiration]) {
100
- const t = setTimeout(() => this.purgeStale(), ttl)
101
- /* istanbul ignore else - affordance for non-node envs */
102
- if (t.unref) t.unref()
103
- this.expirations[expiration] = []
104
- }
105
- this.expirations[expiration].push(key)
106
110
 
107
111
  while (this.size > this.max) {
108
112
  this.purgeToCapacity()
109
113
  }
110
114
 
111
- if (!noDisposeOnSet && current && oldValue !== val) {
112
- this.dispose(oldValue, key, 'set')
113
- }
114
115
  return this
115
116
  }
116
117
 
@@ -121,7 +122,7 @@ class TTLCache {
121
122
  getRemainingTTL(key) {
122
123
  const expiration = this.expirationMap.get(key)
123
124
  return expiration !== undefined
124
- ? Math.max(0, expiration - now())
125
+ ? Math.max(0, Math.ceil(expiration - now()))
125
126
  : 0
126
127
  }
127
128
 
@@ -131,11 +132,7 @@ class TTLCache {
131
132
  ) {
132
133
  const val = this.data.get(key)
133
134
  if (updateAgeOnGet) {
134
- this.set(key, val, {
135
- noUpdateTTL: false,
136
- noDisposeOnSet: true,
137
- ttl,
138
- })
135
+ this.setTTL(key, ttl)
139
136
  }
140
137
  return val
141
138
  }
@@ -189,7 +186,7 @@ class TTLCache {
189
186
  }
190
187
 
191
188
  purgeStale() {
192
- const n = now()
189
+ const n = Math.ceil(now())
193
190
  for (const exp in this.expirations) {
194
191
  if (exp > n) {
195
192
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isaacs/ttlcache",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "files": [
5
5
  "index.js",
6
6
  "index.d.ts"