@isaacs/ttlcache
Advanced tools
Comparing version 1.0.3 to 1.0.4
71
index.js
@@ -6,3 +6,3 @@ // A simple TTL cache with max capacity option, ms resolution, | ||
const maybeReqPerfHooks = (fallback) => { | ||
const maybeReqPerfHooks = fallback => { | ||
try { | ||
@@ -14,7 +14,13 @@ return require('perf_hooks').performance | ||
} | ||
const {now} = maybeReqPerfHooks(Date) | ||
const { now } = maybeReqPerfHooks(Date) | ||
const isPosInt = n => n && n === Math.floor(n) && n > 0 && isFinite(n) | ||
class TTLCache { | ||
constructor ({ max = Infinity, ttl, updateAgeOnGet = false, noUpdateTTL = false, dispose }) { | ||
constructor({ | ||
max = Infinity, | ||
ttl, | ||
updateAgeOnGet = false, | ||
noUpdateTTL = false, | ||
dispose, | ||
}) { | ||
// {[expirationTime]: [keys]} | ||
@@ -34,4 +40,4 @@ this.expirations = Object.create(null) | ||
this.max = max | ||
this.updateAgeOnGet = updateAgeOnGet; | ||
this.noUpdateTTL = noUpdateTTL; | ||
this.updateAgeOnGet = updateAgeOnGet | ||
this.noUpdateTTL = noUpdateTTL | ||
if (dispose !== undefined) { | ||
@@ -45,4 +51,5 @@ if (typeof dispose !== 'function') { | ||
clear () { | ||
const entries = this.dispose !== TTLCache.prototype.dispose ? [...this] : [] | ||
clear() { | ||
const entries = | ||
this.dispose !== TTLCache.prototype.dispose ? [...this] : [] | ||
this.data.clear() | ||
@@ -56,3 +63,11 @@ this.expirationMap.clear() | ||
set (key, val, { ttl = this.ttl, noUpdateTTL = this.noUpdateTTL, noDisposeOnSet = this.noDisposeOnSet } = {}) { | ||
set( | ||
key, | ||
val, | ||
{ | ||
ttl = this.ttl, | ||
noUpdateTTL = this.noUpdateTTL, | ||
noDisposeOnSet = this.noDisposeOnSet, | ||
} = {} | ||
) { | ||
if (!isPosInt(ttl)) { | ||
@@ -63,3 +78,4 @@ throw new TypeError('ttl must be positive integer') | ||
const time = now() | ||
const oldValue = current === undefined ? undefined : this.data.get(key) | ||
const oldValue = | ||
current === undefined ? undefined : this.data.get(key) | ||
if (current !== undefined) { | ||
@@ -107,15 +123,24 @@ // we aren't updating the ttl, so just set the data | ||
has (key) { | ||
has(key) { | ||
return this.data.has(key) | ||
} | ||
getRemainingTTL (key) { | ||
getRemainingTTL(key) { | ||
const expiration = this.expirationMap.get(key) | ||
return expiration !== undefined ? Math.max(0, expiration - now()) : 0 | ||
return expiration !== undefined | ||
? Math.max(0, expiration - now()) | ||
: 0 | ||
} | ||
get (key, { updateAgeOnGet = this.updateAgeOnGet, ttl = this.ttl } = {}) { | ||
get( | ||
key, | ||
{ updateAgeOnGet = this.updateAgeOnGet, ttl = this.ttl } = {} | ||
) { | ||
const val = this.data.get(key) | ||
if (updateAgeOnGet) { | ||
this.set(key, val, { noUpdateTTL: false, noDisposeOnSet: true, ttl }) | ||
this.set(key, val, { | ||
noUpdateTTL: false, | ||
noDisposeOnSet: true, | ||
ttl, | ||
}) | ||
} | ||
@@ -125,5 +150,5 @@ return val | ||
dispose (value, key) {} | ||
dispose(_, __) {} | ||
delete (key) { | ||
delete(key) { | ||
const current = this.expirationMap.get(key) | ||
@@ -146,3 +171,3 @@ if (current !== undefined) { | ||
purgeToCapacity () { | ||
purgeToCapacity() { | ||
for (const exp in this.expirations) { | ||
@@ -171,7 +196,7 @@ const keys = this.expirations[exp] | ||
get size () { | ||
get size() { | ||
return this.data.size | ||
} | ||
purgeStale () { | ||
purgeStale() { | ||
const n = now() | ||
@@ -192,3 +217,3 @@ for (const exp in this.expirations) { | ||
*entries () { | ||
*entries() { | ||
for (const exp in this.expirations) { | ||
@@ -200,3 +225,3 @@ for (const key of this.expirations[exp]) { | ||
} | ||
*keys () { | ||
*keys() { | ||
for (const exp in this.expirations) { | ||
@@ -208,3 +233,3 @@ for (const key of this.expirations[exp]) { | ||
} | ||
*values () { | ||
*values() { | ||
for (const exp in this.expirations) { | ||
@@ -216,3 +241,3 @@ for (const key of this.expirations[exp]) { | ||
} | ||
[Symbol.iterator] () { | ||
[Symbol.iterator]() { | ||
return this.entries() | ||
@@ -219,0 +244,0 @@ } |
{ | ||
"name": "@isaacs/ttlcache", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"files": [ | ||
@@ -26,8 +26,33 @@ "index.js" | ||
"devDependencies": { | ||
"clock-mock": "^1.0.4", | ||
"tap": "^16.0.1" | ||
"@types/node": "^17.0.42", | ||
"@types/tap": "^15.0.7", | ||
"clock-mock": "^1.0.6", | ||
"prettier": "^2.7.0", | ||
"tap": "^16.0.1", | ||
"ts-node": "^10.8.1" | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"tap": { | ||
"nyc-arg": [ | ||
"--include=index.js" | ||
], | ||
"node-arg": [ | ||
"--require", | ||
"ts-node/register" | ||
], | ||
"ts": false | ||
}, | ||
"prettier": { | ||
"semi": false, | ||
"printWidth": 70, | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"jsxSingleQuote": false, | ||
"bracketSameLine": true, | ||
"arrowParens": "avoid", | ||
"endOfLine": "lf" | ||
} | ||
} |
13930
217
6