Comparing version 1.0.0 to 1.1.0
33
index.js
@@ -1,7 +0,16 @@ | ||
"use strict"; | ||
'use strict'; | ||
var ASC = require( './ASC.js' ); | ||
var ASC = require( './lib/asc.js' ); | ||
var caches = {}; | ||
/** | ||
* ASC Class | ||
* | ||
* Create stand alone (not shared) instances of ASC | ||
* | ||
* @type {ASC} The cache class | ||
*/ | ||
module.exports = ASC; | ||
/** | ||
* Clear Cache | ||
@@ -14,7 +23,7 @@ * | ||
*/ | ||
module.exports.clear = function ( name ) { | ||
module.exports.clear = function( name ) { | ||
if ( caches[name] ) { | ||
caches[name].clearAll(); | ||
delete caches[name]; | ||
if ( caches[ name ] ) { | ||
caches[ name ].clearAll(); | ||
delete caches[ name ]; | ||
} | ||
@@ -53,12 +62,12 @@ | ||
* | ||
* @return {ASC} An instance of the ASC cache | ||
* @return {ASC} A cache instance | ||
*/ | ||
module.exports.getCache = function ( name, options ) { | ||
module.exports.getCache = function( name, options ) { | ||
// cache does not exist, create it | ||
if ( !caches[name] ) { | ||
if ( !caches[ name ] ) { | ||
if ( !options ) { | ||
options = {}; | ||
} | ||
caches[name] = new ASC( options ); | ||
caches[ name ] = new ASC( options ); | ||
} | ||
@@ -68,9 +77,9 @@ | ||
else if ( options !== undefined ) { | ||
caches[name].updateOptions( options ); | ||
caches[ name ].updateOptions( options ); | ||
} | ||
// return the cache | ||
return caches[name]; | ||
return caches[ name ]; | ||
}; | ||
{ | ||
"name": "asc", | ||
"description": "A middleware layer between a service and a client. The service could be an in-process library, a file, an external web service, anything. Any time the results from a resource call can be cached, you can use ASC as a proxy to that resource.", | ||
"version": "1.0.0", | ||
"author": "Anthony Hildoer <anthony@bluerival.com>", | ||
"repository": { | ||
"name": "asc", | ||
"description": "A middleware layer between a service and a client. The service could be an in-process library, a file, an external web service, anything. Any time the results from a resource call can be cached, you can use ASC as a proxy to that resource.", | ||
"version": "1.1.0", | ||
"author": "Anthony Hildoer <anthony@bluerival.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bluerival/asc.git" | ||
"url": "git://github.com/bluerival/asc.git" | ||
}, | ||
"dependencies": { | ||
"async": "0.9.0" | ||
"dependencies": { | ||
"async": "1.2.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "2.0.1" | ||
"mocha": "2.2.5" | ||
}, | ||
"keywords": [ | ||
"keywords": [ | ||
"cache", | ||
@@ -22,6 +22,6 @@ "service proxy", | ||
], | ||
"engines": { | ||
"node": ">=0.10.33" | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"license": "MIT" | ||
"license": "MIT" | ||
} |
@@ -46,11 +46,49 @@ ASC (pronounced "ASK") | ||
Soon to come: | ||
* More documentation and examples | ||
Create Instance | ||
======== | ||
The ASC module exports the cache class. This is useful for creating caches that | ||
you do not want to share system wide. | ||
```js | ||
/** | ||
* The Cache Class for ASC | ||
* | ||
* The constructor for a single cache instance. | ||
* | ||
* @param {object} [options] Configuration for the cache | ||
* @param {number ? 300000} [options.ttl] The number of milliseconds each | ||
* key exist in the cache | ||
* @param {function} options.update A function used to update the cache one | ||
* key at a time. Prototype defined below | ||
* @param {function} [options.updateBatch] A function used to update the | ||
* cache in bulk. Prototype defined below | ||
* @param {function} [options.clear] A function called when a key is | ||
* cleared, by a timeout or otherwise. The key is passed to the | ||
* function | ||
* | ||
* options.update = function( key, callback ), where key is the lookup key | ||
* and callback is a function that should be passed a single parameter, | ||
* the value corresponding to key, or undefined if key has no | ||
* corresponding value or if value can not be determined for any reason. | ||
* | ||
* options.updateBatch = function( keys, callback ), where keys is an array | ||
* of keys to lookup in batch, and callback is a function that should be | ||
* passed a single array, containing one entry for each key in the | ||
* corresponding index in keys. Note: if this function is omitted, then | ||
* batch lookups against the cache will fall back to multiple update | ||
* calls in the background. | ||
* | ||
* @constructor | ||
*/ | ||
var ASC = require( 'asc' ); | ||
var profileCache = new ASC( [options] ); | ||
``` | ||
Factory | ||
======== | ||
In order to get a cache, you need to query the factory for an instance. This is | ||
accomplished with the get() method on the asc module. | ||
You can also use the ASC module as a factory and shared cache store, using the | ||
getCache() method on the asc module. | ||
@@ -211,3 +249,3 @@ ```js | ||
Copyright (c) 2012 BlueRival Software <anthony@bluerival.com> | ||
Copyright (c) 2015 BlueRival Software <anthony@bluerival.com> | ||
@@ -214,0 +252,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of |
'use strict'; | ||
var factory = require( '../' ); | ||
var ASC = require( '../' ); | ||
var assert = require( 'assert' ); | ||
var async = require( 'async' ); | ||
describe( 'ASC Factory', function () { | ||
describe( 'ASC Factory', function() { | ||
describe( 'getCache', function () { | ||
var lastUpdateKey; | ||
var updates = 0; | ||
var updateTimeout = 0; | ||
var updateTimeouts = []; | ||
var keyUpdates = {}; | ||
var cacheOne = factory.getCache( 'getCache1', { | ||
ttl: 1000, | ||
update: function ( key, callback ) { | ||
callback( key + 'u' ); | ||
var cleared = 0; | ||
var testCache = ASC.getCache( 'getCache1', { | ||
ttl: 1000, | ||
clear: function() { | ||
cleared++; | ||
}, | ||
update: function( key, callback ) { | ||
var keyStr = JSON.stringify( key ); | ||
lastUpdateKey = key; | ||
if ( !keyUpdates.hasOwnProperty( keyStr ) ) { | ||
keyUpdates[ keyStr ] = 0; | ||
} | ||
keyUpdates[ keyStr ]++; | ||
if ( updateTimeout ) { | ||
updateTimeouts.push( setTimeout( function() { | ||
updates++; | ||
callback( keyStr + 'check' ); | ||
}, updateTimeout ) ); | ||
} else { | ||
updates++; | ||
callback( keyStr + 'check' ); | ||
} | ||
} | ||
} ); | ||
describe( 'getCache', function() { | ||
it( 'should return the same cache object for the same cache name', function() { | ||
assert.strictEqual( ASC.getCache( 'getCache1' ), testCache ); | ||
} ); | ||
it( 'should return the same cache object for the same cache name', function () { | ||
assert.strictEqual( factory.getCache( 'getCache1' ), cacheOne ); | ||
it( 'should NOT return the same cache object for different cache name', function() { | ||
assert.notStrictEqual( ASC.getCache( 'getCache2' ), testCache ); | ||
} ); | ||
it( 'should NOT return the same cache object for different cache name', function () { | ||
assert.notStrictEqual( factory.getCache( 'getCache2' ), cacheOne ); | ||
it( 'should return a new cache object if a cache name is previously cleared', function() { | ||
ASC.clear( 'getCache1' ); | ||
assert.notStrictEqual( ASC.getCache( 'getCache1' ), testCache ); | ||
} ); | ||
} ); | ||
it( 'should return a new cache object if a cache name is previously cleared', function () { | ||
factory.clear( 'getCache1' ); | ||
assert.notStrictEqual( factory.getCache( 'getCache1' ), cacheOne ); | ||
describe( 'get', function() { | ||
var expectedCleared = 0; | ||
var createCheckValueGetTest = function( value ) { | ||
expectedCleared++; | ||
it( 'should return "' + value + 'check"', function( done ) { | ||
testCache.get( value, function( returnValue ) { | ||
assert.strictEqual( returnValue, value + 'check' ); | ||
done(); | ||
} ); | ||
} ); | ||
}; | ||
createCheckValueGetTest( 1 ); | ||
createCheckValueGetTest( 2 ); | ||
createCheckValueGetTest( 3 ); | ||
it( 'should have cleared ' + expectedCleared + ' keys by now', function( done ) { | ||
// allow up to a 10% error rate in setTimeout | ||
// accuracy (100ms for the setTimeout to clear | ||
// keys + 100ms for the setTimeout here) | ||
setTimeout( function() { | ||
assert.strictEqual( cleared, expectedCleared ); | ||
cleared = 0; | ||
done(); | ||
}, 1200 ); | ||
} ); | ||
var simultaneousCalls = 10000; | ||
it( 'should have triggered only 1 call to update for ' + simultaneousCalls + ' parallel calls to the same key', function( done ) { | ||
updates = 0; | ||
keyUpdates = {}; | ||
var tasks = []; | ||
var pushTask = function() { | ||
tasks.push( function( done ) { | ||
async.parallel( [ | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
} | ||
], done ); | ||
} ); | ||
}; | ||
for ( var i = 0; i < simultaneousCalls; i++ ) { | ||
pushTask(); | ||
} | ||
async.parallel( tasks, function() { | ||
assert.strictEqual( updates, 2 ); | ||
assert.strictEqual( keyUpdates[ '"set1"' ], 1 ); | ||
assert.strictEqual( keyUpdates[ '"set2"' ], 1 ); | ||
done(); | ||
} ); | ||
} ); | ||
var simultaneousSetCalls = 10000; | ||
it( 'should have triggered only 0 calls to update for ' + simultaneousSetCalls + ' parallel calls to the same key, and should have gotten data first', function( done ) { | ||
updates = 0; | ||
updateTimeout = 2000; | ||
keyUpdates = {}; | ||
var tasks = []; | ||
testCache.clear( 'set1' ); | ||
testCache.clear( 'set2' ); | ||
testCache.set( 'set1', 'value' ); | ||
testCache.set( 'set2', 'value' ); | ||
var pushTask = function() { | ||
tasks.push( function( done ) { | ||
async.parallel( [ | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
} | ||
], done ); | ||
} ); | ||
}; | ||
for ( var i = 0; i < simultaneousSetCalls; i++ ) { | ||
pushTask(); | ||
} | ||
async.parallel( tasks, function() { | ||
assert.strictEqual( updates, 0 ); | ||
testCache.clear( 'set1' ); | ||
testCache.clear( 'set2' ); | ||
updateTimeout = 0; | ||
done(); | ||
} ); | ||
} ); | ||
it( 'should have triggered only 0 calls to update for ' + simultaneousSetCalls + ' parallel calls to the same key, and should have gotten data first', function( done ) { | ||
// setup test framework | ||
updates = 0; | ||
updateTimeout = 20000; | ||
testCache.clear( 'set1' ); | ||
testCache.clear( 'set2' ); | ||
async.parallel( [ | ||
function( done ) { | ||
testCache.get( 'set1', function( value ) { | ||
try { | ||
assert.strictEqual( value, 'value1' ); | ||
assert.strictEqual( updates, 0 ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
}, | ||
function( done ) { | ||
testCache.get( 'set2', function( value ) { | ||
try { | ||
assert.strictEqual( value, 'value2' ); | ||
assert.strictEqual( updates, 0 ); | ||
done(); | ||
} catch ( e ) { | ||
done( e ); | ||
} | ||
} ); | ||
} | ||
], function( err ) { | ||
// clean up test framework | ||
testCache.clear( 'set1' ); | ||
testCache.clear( 'set2' ); | ||
updateTimeouts.forEach( function( timeout ) { | ||
clearTimeout( timeout ); | ||
} ); | ||
updateTimeout = 0; | ||
if ( err ) { | ||
done( err ); | ||
return; | ||
} | ||
done(); | ||
} ); | ||
setTimeout( function() { | ||
testCache.set( 'set1', 'value1' ); | ||
testCache.set( 'set2', 'value2' ); | ||
}, 100 ); | ||
} ); | ||
it( 'should pass the same key (exact same object, not clone) to update functions', function( done ) { | ||
var complexKey = { | ||
one: 1, | ||
two: 2, | ||
three: 3 | ||
}; | ||
testCache.get( complexKey, function() { | ||
assert.strictEqual( lastUpdateKey, complexKey ); | ||
done(); | ||
} ); | ||
} ); | ||
it( 'should support deep keys which consist of any scalar type, arrays or primitive objects', function( done ) { | ||
var deepKey = { | ||
one: 1, | ||
true: true, | ||
booleanFalse: false, | ||
deepObject: { | ||
two: "two", | ||
deeper: { | ||
three: "tres", | ||
4: [ 1, "two", "three", "four" ], | ||
"five": "5ster" | ||
}, | ||
name: "anthony" | ||
}, | ||
nulled: null, | ||
objectNulled: { | ||
nulled: null, | ||
six: 7 | ||
} | ||
}; | ||
testCache.get( deepKey, function() { | ||
assert.deepEqual( lastUpdateKey, deepKey ); | ||
done(); | ||
} ); | ||
} ); | ||
} ); | ||
} ); | ||
describe( 'ASC', function () { | ||
describe( 'ASC', function() { | ||
var lastUpdateKey; | ||
var updates = 0; | ||
var updateTimeout = 0; | ||
var keyUpdates = {}; | ||
@@ -49,10 +349,10 @@ | ||
describe( 'get', function () { | ||
describe( 'get', function() { | ||
testCache = factory.getCache( 'testCache1', { | ||
testCache = new ASC( { | ||
ttl: 1000, | ||
clear: function () { | ||
clear: function() { | ||
cleared++; | ||
}, | ||
update: function ( key, callback ) { | ||
update: function( key, callback ) { | ||
var keyStr = JSON.stringify( key ); | ||
@@ -62,8 +362,18 @@ lastUpdateKey = key; | ||
if ( !keyUpdates.hasOwnProperty( keyStr ) ) { | ||
keyUpdates[keyStr] = 0; | ||
keyUpdates[ keyStr ] = 0; | ||
} | ||
keyUpdates[keyStr]++; | ||
updates++; | ||
callback( keyStr + 'check' ); | ||
keyUpdates[ keyStr ]++; | ||
if ( updateTimeout ) { | ||
setTimeout( function() { | ||
updates++; | ||
callback( keyStr + 'check' ); | ||
}, updateTimeout ); | ||
updateTimeout = 0; | ||
} else { | ||
updates++; | ||
callback( keyStr + 'check' ); | ||
} | ||
} | ||
@@ -73,8 +383,8 @@ } ); | ||
var expectedCleared = 0; | ||
var createCheckValueGetTest = function ( value ) { | ||
var createCheckValueGetTest = function( value ) { | ||
expectedCleared++; | ||
it( 'should return "' + value + 'check"', function ( done ) { | ||
testCache.get( value, function ( returnValue ) { | ||
it( 'should return "' + value + 'check"', function( done ) { | ||
testCache.get( value, function( returnValue ) { | ||
assert.strictEqual( returnValue, value + 'check' ); | ||
@@ -91,18 +401,17 @@ done(); | ||
it( 'should have cleared ' + expectedCleared + ' keys by now', | ||
function ( done ) { | ||
it( 'should have cleared ' + expectedCleared + ' keys by now', function( done ) { | ||
// allow up to a 10% error rate in setTimeout | ||
// accuracy (100ms for the setTimeout to clear | ||
// keys + 100ms for the setTimeout here) | ||
setTimeout( function () { | ||
assert.strictEqual( cleared, expectedCleared ); | ||
cleared = 0; | ||
done(); | ||
}, 1200 ); | ||
// allow up to a 10% error rate in setTimeout | ||
// accuracy (100ms for the setTimeout to clear | ||
// keys + 100ms for the setTimeout here) | ||
setTimeout( function() { | ||
assert.strictEqual( cleared, expectedCleared ); | ||
cleared = 0; | ||
done(); | ||
}, 1200 ); | ||
} ); | ||
} ); | ||
var simultaneousCalls = 10000; | ||
it( 'should have triggered only 1 call to update for ' + simultaneousCalls + ' parallel calls to the same key', function ( done ) { | ||
it( 'should have triggered only 1 call to update for ' + simultaneousCalls + ' parallel calls to the same key', function( done ) { | ||
@@ -113,33 +422,33 @@ updates = 0; | ||
var pushTask = function () { | ||
tasks.push( function ( done ) { | ||
var pushTask = function() { | ||
tasks.push( function( done ) { | ||
async.parallel( [ | ||
function ( done ) { | ||
testCache.get( 'set1', function () { | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function ( done ) { | ||
testCache.get( 'set1', function () { | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function ( done ) { | ||
testCache.get( 'set1', function () { | ||
function( done ) { | ||
testCache.get( 'set1', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function ( done ) { | ||
testCache.get( 'set2', function () { | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function ( done ) { | ||
testCache.get( 'set2', function () { | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
} ); | ||
}, | ||
function ( done ) { | ||
testCache.get( 'set2', function () { | ||
function( done ) { | ||
testCache.get( 'set2', function() { | ||
done( null, true ); | ||
@@ -157,6 +466,6 @@ } ); | ||
async.parallel( tasks, function () { | ||
async.parallel( tasks, function() { | ||
assert.strictEqual( updates, 2 ); | ||
assert.strictEqual( keyUpdates['"set1"'], 1 ); | ||
assert.strictEqual( keyUpdates['"set2"'], 1 ); | ||
assert.strictEqual( keyUpdates[ '"set1"' ], 1 ); | ||
assert.strictEqual( keyUpdates[ '"set2"' ], 1 ); | ||
done(); | ||
@@ -167,3 +476,3 @@ } ); | ||
it( 'should pass the same key (exact same object, not clone) to update functions', function ( done ) { | ||
it( 'should pass the same key (exact same object, not clone) to update functions', function( done ) { | ||
@@ -176,3 +485,3 @@ var complexKey = { | ||
testCache.get( complexKey, function () { | ||
testCache.get( complexKey, function() { | ||
assert.strictEqual( lastUpdateKey, complexKey ); | ||
@@ -184,3 +493,3 @@ done(); | ||
it( 'should support deep keys which consist of any scalar type, arrays or primitive objects', function ( done ) { | ||
it( 'should support deep keys which consist of any scalar type, arrays or primitive objects', function( done ) { | ||
@@ -195,3 +504,3 @@ var deepKey = { | ||
three: "tres", | ||
4: [1, "two", "three", "four"], | ||
4: [ 1, "two", "three", "four" ], | ||
"five": "5ster" | ||
@@ -208,3 +517,3 @@ }, | ||
testCache.get( deepKey, function () { | ||
testCache.get( deepKey, function() { | ||
assert.deepEqual( lastUpdateKey, deepKey ); | ||
@@ -218,20 +527,20 @@ done(); | ||
describe( 'getBatch', function () { | ||
describe( 'getBatch', function() { | ||
it( 'should return keys as values with "check" appended in the same order they were passed in as keys, with no updateBatch defined', function ( done ) { | ||
it( 'should return keys as values with "check" appended in the same order they were passed in as keys, with no updateBatch defined', function( done ) { | ||
// redefine to add updateBatch | ||
testCache = factory.getCache( 'testCache2', { | ||
testCache = new ASC( { | ||
ttl: 1000, | ||
clear: function () { | ||
clear: function() { | ||
cleared++; | ||
}, | ||
update: function ( key, callback ) { | ||
update: function( key, callback ) { | ||
lastUpdateKey = key; | ||
var keyStr = JSON.stringify( key ); | ||
if ( !keyUpdates.hasOwnProperty( keyStr ) ) { | ||
keyUpdates[keyStr] = 0; | ||
keyUpdates[ keyStr ] = 0; | ||
} | ||
keyUpdates[keyStr]++; | ||
keyUpdates[ keyStr ]++; | ||
updates++; | ||
@@ -242,8 +551,8 @@ callback( key + 'check' ); | ||
var keys = ["one", "two", "three", "four"]; | ||
var keys = [ "one", "two", "three", "four" ]; | ||
testCache.getBatch( keys, function ( values ) { | ||
testCache.getBatch( keys, function( values ) { | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
assert.strictEqual( values[i], keys[i] + "check" ); | ||
assert.strictEqual( values[ i ], keys[ i ] + "check" ); | ||
} | ||
@@ -257,22 +566,22 @@ | ||
it( 'should return keys as values with "check" appended in the same order they were passed in as keys, with updateBatch defined', function ( done ) { | ||
it( 'should return keys as values with "check" appended in the same order they were passed in as keys, with updateBatch defined', function( done ) { | ||
// redefine to add updateBatch | ||
testCache = factory.getCache( 'testCache3', { | ||
testCache = new ASC( { | ||
ttl: 1000, | ||
clear: function () { | ||
clear: function() { | ||
cleared++; | ||
}, | ||
update: function ( key, callback ) { | ||
update: function( key, callback ) { | ||
lastUpdateKey = key; | ||
var keyStr = JSON.stringify( key ); | ||
if ( !keyUpdates.hasOwnProperty( keyStr ) ) { | ||
keyUpdates[keyStr] = 0; | ||
keyUpdates[ keyStr ] = 0; | ||
} | ||
keyUpdates[keyStr]++; | ||
keyUpdates[ keyStr ]++; | ||
updates++; | ||
callback( key + 'check' ); | ||
}, | ||
updateBatch: function ( keys, callback ) { | ||
updateBatch: function( keys, callback ) { | ||
var data = []; | ||
@@ -282,3 +591,3 @@ lastUpdateKeys = keys; | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
data[i] = keys[i] + 'check'; | ||
data[ i ] = keys[ i ] + 'check'; | ||
} | ||
@@ -289,8 +598,8 @@ callback( data ); | ||
var keys = ["one", "two", "three", "four"]; | ||
var keys = [ "one", "two", "three", "four" ]; | ||
testCache.getBatch( keys, function ( values ) { | ||
testCache.getBatch( keys, function( values ) { | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
assert.equal( values[i], keys[i] + "check" ); | ||
assert.equal( values[ i ], keys[ i ] + "check" ); | ||
} | ||
@@ -304,3 +613,3 @@ | ||
it( 'should trigger 3 calls to update, and 1 call to updateBatch', function ( done ) { | ||
it( 'should trigger 3 calls to update, and 1 call to updateBatch', function( done ) { | ||
@@ -312,19 +621,19 @@ cleared = 0; | ||
// redefine to add updateBatch | ||
testCache = factory.getCache( 'testCache4', { | ||
testCache = new ASC( { | ||
ttl: 1000, | ||
clear: function () { | ||
clear: function() { | ||
cleared++; | ||
}, | ||
update: function ( key, callback ) { | ||
update: function( key, callback ) { | ||
lastUpdateKey = key; | ||
var keyStr = JSON.stringify( key ); | ||
if ( !keyUpdates.hasOwnProperty( keyStr ) ) { | ||
keyUpdates[keyStr] = 0; | ||
keyUpdates[ keyStr ] = 0; | ||
} | ||
keyUpdates[keyStr]++; | ||
keyUpdates[ keyStr ]++; | ||
updates++; | ||
callback( key + 'check' ); | ||
}, | ||
updateBatch: function ( keys, callback ) { | ||
updateBatch: function( keys, callback ) { | ||
var data = []; | ||
@@ -334,3 +643,3 @@ lastUpdateKeys = keys; | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
data[i] = keys[i] + 'check'; | ||
data[ i ] = keys[ i ] + 'check'; | ||
} | ||
@@ -342,20 +651,20 @@ callback( data ); | ||
// get one key before we even try anything, assuring that there is a direct cache hit | ||
testCache.get( "four", function () { | ||
testCache.get( "four", function() { | ||
var batchKeys = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]; | ||
var batchKeys = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; | ||
var batchValues = null; | ||
var tasks = [ | ||
function ( callback ) { | ||
testCache.get( "three", function () { | ||
function( callback ) { | ||
testCache.get( "three", function() { | ||
callback( null, true ); | ||
} ); | ||
}, | ||
function ( callback ) { | ||
testCache.get( "one", function () { | ||
function( callback ) { | ||
testCache.get( "one", function() { | ||
callback( null, true ); | ||
} ); | ||
}, | ||
function ( callback ) { | ||
testCache.getBatch( batchKeys, function ( values ) { | ||
function( callback ) { | ||
testCache.getBatch( batchKeys, function( values ) { | ||
batchValues = values; | ||
@@ -365,9 +674,9 @@ callback( null, true ); | ||
}, | ||
function ( callback ) { | ||
testCache.get( "two", function () { | ||
function( callback ) { | ||
testCache.get( "two", function() { | ||
callback( null, true ); | ||
} ); | ||
}, | ||
function ( callback ) { | ||
testCache.get( "six", function () { | ||
function( callback ) { | ||
testCache.get( "six", function() { | ||
callback( null, true ); | ||
@@ -378,5 +687,5 @@ } ); | ||
async.parallel( tasks, function () { | ||
async.parallel( tasks, function() { | ||
for ( var i = 0; i < batchKeys.length; i++ ) { | ||
assert.strictEqual( batchValues[i], batchKeys[i] + "check" ); | ||
assert.strictEqual( batchValues[ i ], batchKeys[ i ] + "check" ); | ||
} | ||
@@ -392,10 +701,10 @@ assert.strictEqual( updates, 3 ); | ||
it( 'should return undefined for all keys', function ( done ) { | ||
it( 'should return undefined for all keys', function( done ) { | ||
testCache = factory.getCache( 'testCache5', { | ||
testCache = new ASC( { | ||
ttl: 1000, | ||
update: function ( key, callback ) { | ||
update: function( key, callback ) { | ||
callback( undefined ); | ||
}, | ||
updateBatch: function ( keys, callback ) { | ||
updateBatch: function( keys, callback ) { | ||
callback( undefined ); | ||
@@ -405,5 +714,5 @@ } | ||
var batchKeys = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]; | ||
var batchKeys = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; | ||
testCache.getBatch( batchKeys, function ( values ) { | ||
testCache.getBatch( batchKeys, function( values ) { | ||
@@ -415,4 +724,4 @@ // values should have one entry for each entry in batch keys | ||
for ( var i = 0; i < batchKeys.length; i++ ) { | ||
var type = typeof values[i]; | ||
assert.strictEqual( type, 'undefined', 'should have returned `undefined` for index ' + i + ', instead found: ' + type ); | ||
var type = typeof values[ i ]; | ||
assert.strictEqual( type, 'undefined', 'should have returned `undefined` for ASC ' + i + ', instead found: ' + type ); | ||
} | ||
@@ -426,16 +735,15 @@ | ||
it( 'should pass the same keys (exact same objects, not clones) to updateBatch functions', function( done ) { | ||
it( 'should pass the same keys (exact same objects, not clones) to updateBatch functions', function ( done ) { | ||
testCache = factory.getCache( 'testCache6', { | ||
testCache = new ASC( { | ||
ttl: 1000, | ||
update: function ( key, callback ) { | ||
update: function( key, callback ) { | ||
lastUpdateKey = key; | ||
callback( key + 'check' ); | ||
}, | ||
updateBatch: function ( keys, callback ) { | ||
updateBatch: function( keys, callback ) { | ||
var data = []; | ||
lastUpdateKeys = keys; | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
data[i] = keys[i]; | ||
data[ i ] = keys[ i ]; | ||
} | ||
@@ -470,7 +778,7 @@ callback( data ); | ||
testCache.getBatch( batchKeys, function () { | ||
testCache.getBatch( batchKeys, function() { | ||
// all values should have clamped to undefined | ||
for ( var i = 0; i < batchKeys.length; i++ ) { | ||
assert.strictEqual( lastUpdateKeys[i], batchKeys[i] ); | ||
assert.strictEqual( lastUpdateKeys[ i ], batchKeys[ i ] ); | ||
} | ||
@@ -477,0 +785,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
43009
1049
267
1
+ Addedasync@1.2.1(transitive)
- Removedasync@0.9.0(transitive)
Updatedasync@1.2.1