catbox-memory
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -15,5 +15,24 @@ // Load modules | ||
// Provides a named reference for memory debugging | ||
internals.MemoryCacheSegment = function MemoryCacheSegment () { | ||
}; | ||
exports = module.exports = internals.Connection = function (options) { | ||
internals.MemoryCacheEntry = function MemoryCacheEntry (key, value, ttl) { | ||
// stringify() to prevent value from changing while in the cache | ||
var stringifiedValue = JSON.stringify(value); | ||
this.item = stringifiedValue; | ||
this.stored = Date.now(); | ||
this.ttl = ttl; | ||
// Approximate cache entry size without value: 144 bytes | ||
this.byteSize = 144 + Buffer.byteLength(stringifiedValue) + Buffer.byteLength(key.segment) + Buffer.byteLength(key.id); | ||
this.timeoutId = null; | ||
}; | ||
exports = module.exports = internals.Connection = function MemoryCache (options) { | ||
Hoek.assert(this.constructor === internals.Connection, 'Memory cache client must be instantiated using new'); | ||
@@ -118,17 +137,10 @@ Hoek.assert(!options || options.maxByteSize === undefined || options.maxByteSize >= 0, 'Invalid cache maxByteSize value'); | ||
var stringifiedValue = null; // stringify() to prevent value from changing while in the cache | ||
var envelope = null; | ||
try { | ||
stringifiedValue = JSON.stringify(value); | ||
} | ||
catch (err) { | ||
envelope = new internals.MemoryCacheEntry(key, value, ttl); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
var envelope = { | ||
item: stringifiedValue, | ||
stored: Date.now(), | ||
ttl: ttl | ||
}; | ||
this.cache[key.segment] = this.cache[key.segment] || {}; | ||
this.cache[key.segment] = this.cache[key.segment] || new internals.MemoryCacheSegment(); | ||
var segment = this.cache[key.segment]; | ||
@@ -141,9 +153,6 @@ | ||
clearTimeout(cachedItem.timeoutId); | ||
if (cachedItem.byteSize) { | ||
self.byteSize -= cachedItem.byteSize; // If the item existed, decrement the byteSize as the value could be different | ||
} | ||
self.byteSize -= cachedItem.byteSize; // If the item existed, decrement the byteSize as the value could be different | ||
} | ||
if (this.settings.maxByteSize) { | ||
envelope.byteSize = 53 + Buffer.byteLength(envelope.item) + Buffer.byteLength(key.segment) + Buffer.byteLength(key.id); // Envelope size without value: 53 bytes | ||
if (self.byteSize + envelope.byteSize > this.settings.maxByteSize) { | ||
@@ -162,2 +171,4 @@ return callback(new Error('Cache size limit reached')); | ||
segment[key.id] = envelope; | ||
this.byteSize += envelope.byteSize; | ||
return callback(null); | ||
@@ -179,3 +190,3 @@ }; | ||
if (item && item.byteSize) { | ||
if (item) { | ||
this.byteSize -= item.byteSize; | ||
@@ -182,0 +193,0 @@ } |
{ | ||
"name": "catbox-memory", | ||
"description": "Memory adapter for catbox", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)", | ||
@@ -24,3 +24,3 @@ "contributors": [ | ||
"catbox": "3.x.x", | ||
"lab": "3.x.x" | ||
"lab": "4.x.x" | ||
}, | ||
@@ -27,0 +27,0 @@ "scripts": { |
@@ -15,7 +15,8 @@ // Load modules | ||
var lab = exports.lab = Lab.script(); | ||
var describe = lab.describe; | ||
var it = lab.it; | ||
var before = lab.before; | ||
var after = lab.after; | ||
var expect = Lab.expect; | ||
var before = Lab.before; | ||
var after = Lab.after; | ||
var describe = Lab.experiment; | ||
var it = Lab.test; | ||
@@ -478,3 +479,5 @@ | ||
var memory = new Memory({ maxByteSize: 70 }); | ||
// maxByteSize is slightly larger than the first key so we are left with a small | ||
// amount of free space, but not enough for the second key to be created. | ||
var memory = new Memory({ maxByteSize: 200 }); | ||
expect(memory.cache).to.not.exist; | ||
@@ -485,4 +488,5 @@ | ||
expect(memory.cache).to.exist; | ||
memory.set(key1, 'my', 10, function () { | ||
memory.set(key1, 'my', 10, function (err) { | ||
expect(err).to.not.exist; | ||
expect(memory.cache[key1.segment][key1.id].item).to.equal('"my"'); | ||
@@ -521,3 +525,4 @@ | ||
expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(113); | ||
expect(memory.byteSize).to.equal(204); | ||
expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(204); | ||
expect(memory.cache[key1.segment][key1.id].item).to.exist; | ||
@@ -552,7 +557,7 @@ done(); | ||
expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(113); | ||
expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(204); | ||
expect(memory.cache[key1.segment][key1.id].item).to.exist; | ||
memory.set(key1, itemToStore, 10, function () { | ||
expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(113); | ||
expect(memory.cache[key1.segment][key1.id].byteSize).to.equal(204); | ||
expect(memory.cache[key1.segment][key1.id].item).to.exist; | ||
@@ -559,0 +564,0 @@ done(); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
27634
10
645
1