Comparing version 0.3.1 to 0.3.2
@@ -40,3 +40,3 @@ var extend = require('deap').extendShallow, | ||
// First arg is always `key` | ||
key = params ? args.key : args.shift(), | ||
key = params ? args.key || args.keys : args.shift(), | ||
@@ -50,2 +50,4 @@ // Last arg is always `done` | ||
if(key instanceof Array) return this.mget.apply(this, arguments); | ||
if(typeof key !== 'string') throw new Error('Invalid arguments for cache.get: "key" must be a string'); | ||
@@ -75,5 +77,6 @@ if(typeof done !== 'function') throw new Error('Invalid arguments for cache.get: "done" must be a function'); | ||
set: function(/* key, value, tags, ttl, done */) { | ||
if(typeof arguments[0] === 'object') return this.mset.apply(this, arguments); | ||
var args = slice.call(arguments), | ||
done = args.pop(), | ||
done = typeof args[args.length - 1] === 'function' ? args.pop() : noop, | ||
key = args.shift(), | ||
@@ -106,3 +109,3 @@ value = args.shift(), | ||
// First arg is always `keys` | ||
keys = params ? args.keys : args.shift(), | ||
keys = params ? args.keys || args.key : args.shift(), | ||
@@ -140,3 +143,3 @@ // Last arg is always `done` | ||
var args = slice.call(arguments), | ||
done = args.pop(), | ||
done = typeof args[args.length - 1] === 'function' ? args.pop() : noop, | ||
keysValues = args.shift(), | ||
@@ -173,4 +176,8 @@ tags, ttl; | ||
ttl = undefined; | ||
} else if(typeof done !== 'function') { | ||
done = noop; | ||
} | ||
if(typeof key !== 'string') throw new Error('Invalid arguments for cache.get: "key" must be a string'); | ||
this.store.expire(key, parseTTL(ttl), done); | ||
@@ -177,0 +184,0 @@ |
{ | ||
"name": "cashbox", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "javascript cache library with configurable storage", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,3 +19,3 @@ [](http://ci.testling.com/selfcontained/cashbox) | ||
Using cashbox is pretty simple: | ||
Using cashbox is as follows: | ||
@@ -52,6 +52,6 @@ ```javascript | ||
+ [.get(key, [load], [ttl], callback)](https://github.com/selfcontained/cashbox#getkey-load-ttl-callback) | ||
+ [.set(key, value, [tags], [ttl], callback)](https://github.com/selfcontained/cashbox#setkey-value-tags-ttl-callback) | ||
+ [.set(key, value, [tags], [ttl], [callback])](https://github.com/selfcontained/cashbox#setkey-value-tags-ttl-callback) | ||
+ [.mget(keys, [load], [ttl], callback)](https://github.com/selfcontained/cashbox#mgetkeys-load-ttl-callback) | ||
+ [.mset(keysValuesMap, [tags], [ttl], callback)](https://github.com/selfcontained/cashbox#msetkeysvaluesmap-tags-ttl-callback) | ||
+ [.expire(key, [ttl], callback)](https://github.com/selfcontained/cashbox#expirekey-ttl-callback) | ||
+ [.mset(keysValuesMap, [tags], [ttl], [callback])](https://github.com/selfcontained/cashbox#msetkeysvaluesmap-tags-ttl-callback) | ||
+ [.expire(key, [ttl], [callback])](https://github.com/selfcontained/cashbox#expirekey-ttl-callback) | ||
+ [.getKeys(tags, callback)](https://github.com/selfcontained/cashbox#getkeystags-callback) | ||
@@ -111,3 +111,3 @@ | ||
### .set(key, value, [tags], [ttl], callback) | ||
### .set(key, value, [tags], [ttl], [callback]) | ||
@@ -156,3 +156,3 @@ + **key** is a string value used as the cache key | ||
### .mset(keysValuesMap, [tags], [ttl], callback) | ||
### .mset(keysValuesMap, [tags], [ttl], [callback]) | ||
@@ -165,3 +165,3 @@ + **keysValuesMap** is an object of keys => values to cache | ||
```javascript | ||
// simple mset with a 60 second ttl | ||
// mset with a 60 second ttl | ||
cache.mset({ 'one':1, 'two':2 }, 60, function(err, wasSet) { | ||
@@ -177,3 +177,3 @@ console.log('my values are cached for 60 seconds'); | ||
### .expire(key, [ttl], callback) | ||
### .expire(key, [ttl], [callback]) | ||
@@ -194,3 +194,3 @@ Used to set ttl on a key, or expire it right away | ||
}); | ||
// we could also just expire it right away | ||
@@ -211,3 +211,3 @@ cache.expire('myKey', function(err, expired) { | ||
cache.set('myKey', 'myValue', ['awesome'], function(err) { | ||
cache.getKeys('awesome', function(err, keys) { | ||
@@ -214,0 +214,0 @@ console.log(keys); // ['myKey'] |
@@ -6,3 +6,8 @@ var assert = require('chai').assert, | ||
var DEFAULT_TTL = 1, | ||
DEFAULT_TTL_STRING = '1 sec', | ||
DETAULT_TIMEOUT = 2000; | ||
describe('Redis Cache', function() { | ||
this.timeout(2250); | ||
@@ -180,3 +185,3 @@ var cache = new Cache({ | ||
cache.set(key, value, 1, function(err, set) { | ||
cache.set(key, value, DEFAULT_TTL, function(err, set) { | ||
assert.isNull(err); | ||
@@ -193,3 +198,3 @@ assert.isTrue(set); | ||
}); | ||
}, 1025); | ||
}, DETAULT_TIMEOUT); | ||
}); | ||
@@ -239,3 +244,3 @@ }); | ||
cache.get(key, load, 1, function(err, v) { | ||
cache.get(key, load, DEFAULT_TTL, function(err, v) { | ||
assert.isNull(err); | ||
@@ -253,3 +258,3 @@ assert.equal(v, value); | ||
}); | ||
}, 1025); | ||
}, DETAULT_TIMEOUT); | ||
@@ -442,3 +447,3 @@ }); | ||
cache.mget([key2, key1], loadIt, 1, function(err, results) { | ||
cache.mget([key2, key1], loadIt, DEFAULT_TTL, function(err, results) { | ||
assert.isNull(err); | ||
@@ -459,3 +464,3 @@ assert.lengthOf(results, 2); | ||
}); | ||
}, 1025); | ||
}, DETAULT_TIMEOUT); | ||
@@ -476,3 +481,3 @@ }); | ||
cache.mget([key2, key1], loadIt, '1 sec', function(err, results) { | ||
cache.mget([key2, key1], loadIt, DEFAULT_TTL_STRING, function(err, results) { | ||
assert.isNull(err); | ||
@@ -493,3 +498,3 @@ assert.lengthOf(results, 2); | ||
}); | ||
}, 1025); | ||
}, DETAULT_TIMEOUT); | ||
@@ -562,3 +567,3 @@ }); | ||
cache.mset(hash, 1, function(err, set) { | ||
cache.mset(hash, DEFAULT_TTL, function(err, set) { | ||
assert.isNull(err); | ||
@@ -576,3 +581,3 @@ assert.isTrue(set); | ||
}); | ||
}, 1025); | ||
}, DETAULT_TIMEOUT); | ||
@@ -711,3 +716,3 @@ }); | ||
cache.expire(key, 1, function(err, expired) { | ||
cache.expire(key, DEFAULT_TTL, function(err, expired) { | ||
assert.isNull(err); | ||
@@ -723,3 +728,3 @@ assert.isTrue(expired); | ||
}); | ||
}, 1025); | ||
}, DETAULT_TIMEOUT); | ||
}); | ||
@@ -746,3 +751,3 @@ }); | ||
function(cb) { | ||
setTimeout(cb, 500); | ||
cache.store.client.on('ready', cb); | ||
}, | ||
@@ -749,0 +754,0 @@ // set a value in the connected database |
@@ -50,2 +50,24 @@ var assert = require('chai').assert, | ||
it('should set without a callback', function(done) { | ||
var cache = new Cache(); | ||
assert.doesNotThrow(function() { | ||
cache.set(key, value); | ||
done(); | ||
}); | ||
}); | ||
it('should set syncronously when using Memory store', function(done) { | ||
var cache = new Cache(); | ||
cache.set(key, value); | ||
cache.get(key, function(err, v) { | ||
assert.isNull(err); | ||
assert.equal(v, value); | ||
done(); | ||
}); | ||
}); | ||
it('should set a null value', function(done) { | ||
@@ -252,383 +274,425 @@ var cache = new Cache(); | ||
describe('mget()', function() { | ||
var key1 = 'beep', | ||
key2 = 'boop', | ||
value1 = 'bop', | ||
value2 = 'burp'; | ||
function describeMultiGetTests(description, method) { | ||
describe(description, function() { | ||
var key1 = 'beep', | ||
key2 = 'boop', | ||
value1 = 'bop', | ||
value2 = 'burp'; | ||
it('should set 2 values, and return both in the same order', function(done) { | ||
var cache = new Cache(); | ||
it('should set 2 values, and return both in the same order', function(done) { | ||
var cache = new Cache(); | ||
cache.set(key1, value1, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.set(key2, value2, function(err, set) { | ||
cache.set(key1, value1, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.mget([key2, key1], function(err, results) { | ||
cache.set(key2, value2, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache[method]([key2, key1], function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should accept an object', function(done) { | ||
var cache = new Cache(); | ||
cache[method]({ | ||
keys: [key1, key2], | ||
ttl: 1, | ||
load: function(keys, cb) { | ||
cb(null, [value1, value2]); | ||
}, | ||
done: function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
done(); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
it('should accept an object', function(done) { | ||
var cache = new Cache(); | ||
it('should accept an object with tagging capabilities (as an array)', function(done) { | ||
var cache = new Cache(); | ||
cache.mget({ | ||
keys: [key1, key2], | ||
ttl: 1, | ||
load: function(keys, cb) { | ||
cb(null, [value1, value2]); | ||
}, | ||
done: function(err, results) { | ||
cache[method]({ | ||
keys: [key1, key2], | ||
ttl: 1, | ||
load: function(keys, cb) { | ||
cb( | ||
null, | ||
[value1, value2], | ||
['test=1', 'test=2'] | ||
); | ||
}, | ||
done: function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
cache.getKeys('test=2', function(err, keys) { | ||
assert.equal(keys[0], key2); | ||
done(); | ||
}); | ||
} | ||
}); | ||
}); | ||
it('should accept an object with a string ttl', function(done) { | ||
var cache = new Cache(); | ||
cache[method]({ | ||
keys: [key1, key2], | ||
ttl: '1 second', | ||
load: function(keys, cb) { | ||
cb(null, [value1, value2]); | ||
}, | ||
done: function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
done(); | ||
} | ||
}); | ||
}); | ||
it('should handle a load function for missing values', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
cache[method]([key2, key1], loadIt, function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
it('should accept an object with tagging capabilities (as an array)', function(done) { | ||
var cache = new Cache(); | ||
it('should handle a load function and a ttl for missing values', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
cache.mget({ | ||
keys: [key1, key2], | ||
ttl: 1, | ||
load: function(keys, cb) { | ||
cb( | ||
null, | ||
[value1, value2], | ||
['test=1', 'test=2'] | ||
); | ||
}, | ||
done: function(err, results) { | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
cache[method]([key2, key1], loadIt, 1, function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
cache.getKeys('test=2', function(err, keys) { | ||
assert.equal(keys[0], key2); | ||
done(); | ||
}); | ||
} | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should accept an object with a string ttl', function(done) { | ||
var cache = new Cache(); | ||
it('should handle a load function and a string ttl for missing values', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
cache.mget({ | ||
keys: [key1, key2], | ||
ttl: '1 second', | ||
load: function(keys, cb) { | ||
cb(null, [value1, value2]); | ||
}, | ||
done: function(err, results) { | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
cache[method]([key2, key1], loadIt, '1s', function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
it('should handle a load function for missing values', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
it('should handle a load function and a ttl for missing values and not return them after expiration', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
cache.mget([key2, key1], loadIt, function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
cache[method]([key2, key1], loadIt, 1, function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
done(); | ||
}); | ||
setTimeout(function() { | ||
cache[method]([key1, key2], function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.isUndefined(results[0]); | ||
assert.isUndefined(results[1]); | ||
}); | ||
done(); | ||
}); | ||
}, 1025); | ||
it('should handle a load function and a ttl for missing values', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
}); | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
}); | ||
cache.mget([key2, key1], loadIt, 1, function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
it('should handle a load function and a string ttl for missing values and not return them after expiration', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
done(); | ||
}); | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
}); | ||
cache[method]([key2, key1], loadIt, '1 sec', function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
it('should handle a load function and a string ttl for missing values', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
setTimeout(function() { | ||
cache[method]([key1, key2], function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.isUndefined(results[0]); | ||
assert.isUndefined(results[1]); | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
done(); | ||
}); | ||
}, 1025); | ||
cache.mget([key2, key1], loadIt, '1s', function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
} | ||
it('should handle a load function and a ttl for missing values and not return them after expiration', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
describeMultiGetTests('mget()', 'mget'); | ||
describeMultiGetTests('get() called with an array', 'get'); | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
function describeMultiSetTest(description, method) { | ||
describe(description, function() { | ||
var key1 = 'beep', | ||
key2 = 'boop', | ||
value1 = 'bop', | ||
value2 = 'burp'; | ||
cache.mget([key2, key1], loadIt, 1, function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
it('should set multiple values, and then allow them to be retreived', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
setTimeout(function() { | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
cache[method](hash, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.mget([key1, key2], function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.isUndefined(results[0]); | ||
assert.isUndefined(results[1]); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
done(); | ||
}); | ||
}, 1025); | ||
}); | ||
}); | ||
}); | ||
it('should set multiple values with a ttl, and then allow them to be retreived', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
it('should handle a load function and a string ttl for missing values and not return them after expiration', function(done) { | ||
var called = false, | ||
cache = new Cache(); | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
function loadIt(keys, cb) { | ||
called = true; | ||
cb(null, [value2, value1]); | ||
} | ||
cache[method](hash, 1, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.mget([key2, key1], loadIt, '1 sec', function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value2); | ||
assert.equal(results[1], value1); | ||
assert.isTrue(called); | ||
setTimeout(function() { | ||
cache.mget([key1, key2], function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.isUndefined(results[0]); | ||
assert.isUndefined(results[1]); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
done(); | ||
}); | ||
}, 1025); | ||
}); | ||
}); | ||
}); | ||
it('should set multiple values with a ttl, and then not allow them to be retreived after expiration', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
}); | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
describe('mset()', function() { | ||
var key1 = 'beep', | ||
key2 = 'boop', | ||
value1 = 'bop', | ||
value2 = 'burp'; | ||
cache[method](hash, 1, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
it('should set multiple values, and then allow them to be retreived', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
setTimeout(function() { | ||
cache.mget([key1, key2], function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.isUndefined(results[0]); | ||
assert.isUndefined(results[1]); | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
done(); | ||
}); | ||
}, 1025); | ||
cache.mset(hash, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.mget([key1, key2], function(err, results) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should set multiple values with a ttl, and then allow them to be retreived', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
it('should set one tag for one key', function(done) { | ||
var cache = new Cache(), | ||
hash = {}, | ||
tags = {}, | ||
tag = 'awesome'; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
cache.mset(hash, 1, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
tags[key1] = [tag]; | ||
cache.mget([key1, key2], function(err, results) { | ||
cache[method](hash, tags, function(err, set) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
assert.isTrue(set); | ||
done(); | ||
cache.getKeys(tag, function(err, keys) { | ||
assert.isNull(err); | ||
assert.deepEqual(keys, [key1]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should set multiple values with a ttl, and then not allow them to be retreived after expiration', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
it('should set two tags for one key', function(done) { | ||
var cache = new Cache(), | ||
hash = {}, | ||
tags = {}, | ||
tag1 = 'awesome', | ||
tag2 = 'rawesome'; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
cache.mset(hash, 1, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
tags[key1] = [tag1, tag2]; | ||
setTimeout(function() { | ||
cache.mget([key1, key2], function(err, results) { | ||
cache[method](hash, tags, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.getKeys([tag1, tag2], function(err, keys) { | ||
assert.isNull(err); | ||
assert.lengthOf(results, 2); | ||
assert.isUndefined(results[0]); | ||
assert.isUndefined(results[1]); | ||
assert.deepEqual(keys, [key1]); | ||
done(); | ||
}); | ||
}, 1025); | ||
}); | ||
}); | ||
}); | ||
it('should set one tag for one key', function(done) { | ||
var cache = new Cache(), | ||
hash = {}, | ||
tags = {}, | ||
tag = 'awesome'; | ||
it('should set two tags for one key and one tag for another', function(done) { | ||
var cache = new Cache(), | ||
hash = {}, | ||
tags = {}, | ||
tag1 = 'awesome', | ||
tag2 = 'rawesome'; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
tags[key1] = [tag]; | ||
tags[key1] = [tag1, tag2]; | ||
tags[key2] = [tag1]; | ||
cache.mset(hash, tags, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.getKeys(tag, function(err, keys) { | ||
cache[method](hash, tags, function(err, set) { | ||
assert.isNull(err); | ||
assert.deepEqual(keys, [key1]); | ||
assert.isTrue(set); | ||
done(); | ||
cache.getKeys([tag1, tag2], function(err, keys) { | ||
assert.isNull(err); | ||
assert.deepEqual(keys, [key1, key2]); | ||
cache.getKeys([tag1], function(err, keys){ | ||
assert.isNull(err); | ||
assert.deepEqual(keys, [key1, key2]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should set two tags for one key', function(done) { | ||
var cache = new Cache(), | ||
hash = {}, | ||
tags = {}, | ||
tag1 = 'awesome', | ||
tag2 = 'rawesome'; | ||
it('should function without a callback', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
tags[key1] = [tag1, tag2]; | ||
assert.doesNotThrow(function() { | ||
cache[method](hash); | ||
cache.mset(hash, tags, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.getKeys([tag1, tag2], function(err, keys) { | ||
assert.isNull(err); | ||
assert.deepEqual(keys, [key1]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should set two tags for one key and one tag for another', function(done) { | ||
var cache = new Cache(), | ||
hash = {}, | ||
tags = {}, | ||
tag1 = 'awesome', | ||
tag2 = 'rawesome'; | ||
it('should function syncronously when using Memory store', function(done) { | ||
var cache = new Cache(), | ||
hash = {}; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
hash[key1] = value1; | ||
hash[key2] = value2; | ||
tags[key1] = [tag1, tag2]; | ||
tags[key2] = [tag1]; | ||
cache.mset(hash, tags, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.getKeys([tag1, tag2], function(err, keys) { | ||
cache[method](hash); | ||
cache.mget([key1, key2], function(err, results) { | ||
assert.isNull(err); | ||
assert.deepEqual(keys, [key1, key2]); | ||
assert.lengthOf(results, 2); | ||
assert.equal(results[0], value1); | ||
assert.equal(results[1], value2); | ||
cache.getKeys([tag1], function(err, keys){ | ||
assert.isNull(err); | ||
assert.deepEqual(keys, [key1, key2]); | ||
done(); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
describeMultiSetTest('mset()', 'mset'); | ||
describeMultiSetTest('set() called with an array', 'set'); | ||
@@ -697,4 +761,33 @@ describe('expire()', function() { | ||
it('should function without a callback', function(done) { | ||
var cache = new Cache(); | ||
assert.doesNotThrow(function() { | ||
cache.expire('non-existent-key'); | ||
done(); | ||
}) | ||
}); | ||
it('should function syncronously when using the Memory store', function(done) { | ||
var cache = new Cache(), | ||
key = 'someKey', | ||
value = 'foobar'; | ||
cache.set(key, value, 10, function(err, set) { | ||
assert.isNull(err); | ||
assert.isTrue(set); | ||
cache.expire(key); | ||
cache.get(key, function(err, v) { | ||
assert.isNull(err); | ||
assert.isUndefined(v); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
59834
1773