@hapi/catbox
Advanced tools
Comparing version 11.0.0 to 11.0.1
@@ -8,7 +8,12 @@ 'use strict'; | ||
id = null; | ||
timeout = null; | ||
count = 1; | ||
rule = null; | ||
resolve = null; | ||
reject = null; | ||
constructor(id, rule) { | ||
this.id = id; | ||
this.timeoutTimer = null; | ||
this.count = 1; | ||
this.rule = rule; | ||
@@ -31,3 +36,3 @@ | ||
clearTimeout(this.timeoutTimer); | ||
clearTimeout(this.timeout); | ||
@@ -55,5 +60,5 @@ if (err && | ||
clearTimeout(this.timeoutTimer); | ||
this.timeoutTimer = setTimeout(fn, timeoutMs); | ||
clearTimeout(this.timeout); | ||
this.timeout = setTimeout(fn, timeoutMs); | ||
} | ||
}; |
@@ -53,19 +53,23 @@ 'use strict'; | ||
rule = null; | ||
stats = { | ||
sets: 0, | ||
gets: 0, | ||
hits: 0, | ||
stales: 0, | ||
generates: 0, | ||
errors: 0 | ||
}; | ||
_events = null; | ||
_cache = null; | ||
_segment = null; | ||
_pendings = new Map(); // id -> Pending | ||
_pendingGenerateCall = new Map(); // id -> timer | ||
constructor(options, cache, segment) { | ||
this._events = null; | ||
this._cache = cache; | ||
this._pendings = new Map(); // id -> Pending | ||
this._pendingGenerateCall = new Map(); // id -> timer | ||
this.rules(options); | ||
this.stats = { | ||
sets: 0, | ||
gets: 0, | ||
hits: 0, | ||
stales: 0, | ||
generates: 0, | ||
errors: 0 | ||
}; | ||
if (cache) { | ||
@@ -108,10 +112,15 @@ const nameErr = cache.validateSegmentName(segment); | ||
const id = (key && typeof key === 'object') ? key.id : key; | ||
let pending = this._pendings.get(id); | ||
if (!key || | ||
typeof key === 'string') { | ||
key = { id: key, string: true }; | ||
} | ||
let pending = this._pendings.get(key.id); | ||
if (pending !== undefined) { | ||
return await pending.join(); | ||
return pending.join(); | ||
} | ||
pending = new Pending(id, this.rule); | ||
this._pendings.set(id, pending); | ||
pending = new Pending(key.id, this.rule); | ||
this._pendings.set(key.id, pending); | ||
@@ -122,3 +131,3 @@ try { | ||
catch (err) { | ||
this._send(pending, err); // Safeguard to ensure that the pending rejects on any processing errors | ||
this._send(key, err); // Safeguard to ensure that the pending rejects on any processing errors | ||
} | ||
@@ -139,6 +148,5 @@ | ||
let cached = null; | ||
if (this._cache) { | ||
try { | ||
cached = await this._cache.get({ segment: this._segment, id: pending.id }); | ||
var cached = await this._cache.get({ segment: this._segment, id: key.id }); | ||
} | ||
@@ -158,3 +166,3 @@ catch (err) { | ||
const staleIn = typeof this.rule.staleIn === 'function' ? this.rule.staleIn(cached.stored, cached.ttl) : this.rule.staleIn; | ||
cached.isStale = (staleIn ? (Date.now() - cached.stored) >= staleIn : false); | ||
cached.isStale = staleIn ? Date.now() - cached.stored >= staleIn : false; | ||
report.isStale = cached.isStale; | ||
@@ -170,5 +178,5 @@ | ||
if (!this.rule.generateFunc || | ||
(report.error && !this.rule.generateOnReadError)) { | ||
report.error && !this.rule.generateOnReadError) { | ||
this._send(pending, report.error, cached ? cached.item : null, cached, report); | ||
this._send(key, report.error, cached ? cached.item : null, cached, report); | ||
return; | ||
@@ -182,3 +190,3 @@ } | ||
this._send(pending, null, cached.item, cached, report); | ||
this._send(key, null, cached.item, cached, report); | ||
return; | ||
@@ -189,3 +197,3 @@ } | ||
await Promise.race([ | ||
return Promise.race([ | ||
pending.promise, | ||
@@ -198,3 +206,3 @@ this._generate(pending, key, cached, report) | ||
if (cached) { // Must be stale | ||
if (cached) { // Must be stale | ||
@@ -209,3 +217,3 @@ // Set stale timeout | ||
pending.setTimeout(() => this._send(pending, null, cached.item, cached, report), this.rule.staleTimeout); | ||
pending.setTimeout(() => this._send(key, null, cached.item, cached, report), this.rule.staleTimeout); | ||
} | ||
@@ -216,3 +224,3 @@ else if (this.rule.generateTimeout) { | ||
pending.setTimeout(() => this._send(pending, Boom.serverUnavailable(), null, null, report), this.rule.generateTimeout); | ||
pending.setTimeout(() => this._send(key, Boom.serverUnavailable(), null, null, report), this.rule.generateTimeout); | ||
} | ||
@@ -222,3 +230,3 @@ | ||
if (this._pendingGenerateCall.has(pending.id)) { | ||
if (this._pendingGenerateCall.has(key.id)) { | ||
return; | ||
@@ -229,13 +237,13 @@ } | ||
++this.stats.generates; // Record generation before call in case it times out | ||
++this.stats.generates; // Record generation before call in case it times out | ||
if (this.rule.pendingGenerateTimeout) { | ||
const timeout = setTimeout(() => this._pendingGenerateCall.delete(pending.id), this.rule.pendingGenerateTimeout); | ||
this._pendingGenerateCall.set(pending.id, timeout); | ||
const timeout = setTimeout(() => this._pendingGenerateCall.delete(key.id), this.rule.pendingGenerateTimeout); | ||
this._pendingGenerateCall.set(key.id, timeout); | ||
} | ||
return this._callGenerateFunc(pending, key, cached, report); | ||
return this._callGenerateFunc(key, cached, report); | ||
} | ||
async _callGenerateFunc(pending, key, cached, report) { | ||
async _callGenerateFunc(key, cached, report) { | ||
@@ -245,3 +253,3 @@ const flags = {}; | ||
try { | ||
var value = await this.rule.generateFunc(key, flags); | ||
var value = await this.rule.generateFunc(key.string ? key.id : key, flags); | ||
} | ||
@@ -253,8 +261,6 @@ catch (err) { | ||
const pendingTimeout = this._pendingGenerateCall.get(pending.id); | ||
const pendingTimeout = this._pendingGenerateCall.get(key.id); | ||
if (pendingTimeout) { | ||
clearTimeout(pendingTimeout); | ||
this._pendingGenerateCall.delete(pending.id); | ||
pending = this._pendings.get(pending.id) || pending; // Fetch latest - it might have changed | ||
this._pendingGenerateCall.delete(key.id); | ||
} | ||
@@ -266,8 +272,8 @@ | ||
if (flags.ttl === 0 || // null or undefined means use policy | ||
(generateError && this.rule.dropOnError)) { | ||
generateError && this.rule.dropOnError) { | ||
await this.drop(pending.id); // Invalidate cache | ||
await this.drop(key.id); // Invalidate cache | ||
} | ||
else if (!generateError) { | ||
await this.set(pending.id, value, flags.ttl); // Replace stale cache copy with late-coming fresh copy | ||
await this.set(key.id, value, flags.ttl); // Replace stale cache copy with late-coming fresh copy | ||
} | ||
@@ -285,13 +291,18 @@ } | ||
this._send(pending, error, cached.item, cached, report); | ||
this._send(key, error, cached.item, cached, report); | ||
return; | ||
} | ||
this._send(pending, error, value, null, report); // Ignored if stale value already returned | ||
this._send(key, error, value, null, report); // Ignored if stale value already returned | ||
} | ||
_send(pending, err, value, cached, report) { | ||
_send(key, err, value, cached, report) { | ||
const pending = this._pendings.get(key.id); | ||
if (!pending) { | ||
return; | ||
} | ||
this._pendings.delete(key.id); | ||
pending.send(err, value, cached, report); | ||
this._pendings.delete(pending.id); | ||
@@ -311,7 +322,4 @@ if (report && report.isStale !== undefined) { | ||
ttl = ttl || internals.Policy.ttl(this.rule); | ||
const id = (key && typeof key === 'object') ? key.id : key; | ||
try { | ||
await this._cache.set({ segment: this._segment, id }, value, ttl); | ||
await this._cache.set({ segment: this._segment, id: internals.id(key) }, value, ttl || internals.Policy.ttl(this.rule)); | ||
} | ||
@@ -330,6 +338,4 @@ catch (err) { | ||
const id = (key && typeof key === 'object') ? key.id : key; | ||
try { | ||
await this._cache.drop({ segment: this._segment, id }); | ||
await this._cache.drop({ segment: this._segment, id: internals.id(key) }); | ||
return; | ||
@@ -392,3 +398,3 @@ } | ||
Hoek.assert(!options.staleTimeout || !hasExpiresIn || options.staleTimeout < options.expiresIn, 'staleTimeout must be less than expiresIn'); | ||
Hoek.assert(!options.staleTimeout || !hasExpiresIn || typeof options.staleIn === 'function' || options.staleTimeout < (options.expiresIn - options.staleIn), 'staleTimeout must be less than the delta between expiresIn and staleIn'); | ||
Hoek.assert(!options.staleTimeout || !hasExpiresIn || typeof options.staleIn === 'function' || options.staleTimeout < options.expiresIn - options.staleIn, 'staleTimeout must be less than the delta between expiresIn and staleIn'); | ||
Hoek.assert(!options.staleTimeout || !options.pendingGenerateTimeout || options.staleTimeout < options.pendingGenerateTimeout, 'pendingGenerateTimeout must be greater than staleTimeout if specified'); | ||
@@ -482,1 +488,7 @@ | ||
}; | ||
internals.id = function (key) { | ||
return key && typeof key === 'object' ? key.id : key; | ||
}; |
{ | ||
"name": "@hapi/catbox", | ||
"description": "Multi-strategy object caching service", | ||
"version": "11.0.0", | ||
"version": "11.0.1", | ||
"repository": "git://github.com/hapijs/catbox", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
470
21532