express-redis-cache
Advanced tools
Comparing version 0.5.1 to 1.0.0
@@ -37,25 +37,52 @@ module.exports = (function () { | ||
self.client.keys(prefix + ':' + name, domain.intercept(function (keys) { | ||
if ( ! keys.length ) { | ||
callback(null, []); | ||
return domain.exit(); | ||
} | ||
var redisKey = prefix + ':' + (name ||'*'); | ||
require('async').parallel(keys.map(function (key) { | ||
return function (cb) { | ||
self.client.hgetall(key, domain.intercept(function (result) { | ||
var names = key.split(':'); | ||
result.name = names[1]; | ||
result.prefix = names[0]; | ||
self.emit('message', require('util').format('GET %s ~%d Kb', key, | ||
(require('../sizeof')(result) / 1024).toFixed(2))); | ||
cb(null, result); | ||
})); | ||
}; | ||
}), domain.intercept(function (results) { | ||
callback(null, results); | ||
domain.exit(); | ||
/** Detect wildcard syntax */ | ||
var hasWildcard = redisKey.indexOf('*') >= 0; | ||
var fetchKey = function (key, cb) { | ||
self.client.hgetall(key, domain.intercept(function (result) { | ||
if ( result ) { | ||
var names = key.split(':'); | ||
result.name = names[1]; | ||
result.prefix = names[0]; | ||
self.emit('message', require('util').format('GET %s ~%d Kb', key, | ||
(require('../sizeof')(result) / 1024).toFixed(2))); | ||
} | ||
cb(null, result); | ||
})); | ||
})); | ||
}; | ||
/** If has wildcard */ | ||
if ( hasWildcard ) { | ||
/** Get a list of keys using the wildcard */ | ||
self.client.keys(prefix + ':' + name, domain.intercept(function (keys) { | ||
if ( ! keys.length ) { | ||
callback(null, []); | ||
return domain.exit(); | ||
} | ||
require('async').parallel(keys.map(function (key) { | ||
return function (cb) { | ||
return fetchKey(key, cb); | ||
}; | ||
}), domain.intercept(function (results) { | ||
callback(null, results); | ||
domain.exit(); | ||
})); | ||
})); | ||
} | ||
/** No wildcard **/ | ||
else { | ||
fetchKey(redisKey, domain.intercept(function (result) { | ||
if( result ) { | ||
callback(null, [ result ]); | ||
domain.exit(); | ||
} | ||
else { | ||
callback(null, []); | ||
domain.exit(); | ||
} | ||
})); | ||
} | ||
}); | ||
@@ -62,0 +89,0 @@ } |
@@ -85,15 +85,2 @@ module.exports = (function () { | ||
// If the cache gets disconnected, call next() | ||
/* NOTE: The disconnected event is emitted after any redis client completes it's configured max_attempts and delay throttling. | ||
This will throw a nasty error regarding headers when this occurs along the lines of: | ||
Error: Can't set headers after they are sent. | ||
at ServerResponse.OutgoingMessage.setHeader... | ||
...but it will allow the request to be completed without blocking. Subsequent requests pass through as self.connected is false. | ||
*/ | ||
self.on('disconnected', function () { | ||
return next(); | ||
}); | ||
/** Cache entry name | ||
@@ -100,0 +87,0 @@ * |
{ | ||
"name": "express-redis-cache", | ||
"version": "0.5.1", | ||
"version": "1.0.0", | ||
"description": "A module to make Express interact with Redis (create, get, delete). You can automatically cache all your most popular routes in Redis.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -76,4 +76,49 @@ (function () { | ||
it ( 'should support wildcard gets', function (done) { | ||
cache.add('wildkey1', 'abc', | ||
function ($error, $name, $entry) { | ||
cache.add('wildkey2', 'def', | ||
function ($error, $name, $entry) { | ||
cache.get('wildkey*', function ($error, $results) { | ||
$results.should.be.an.Array; | ||
$results.should.have.a.lengthOf(2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it ( 'should support specific gets without calling keys', function (done) { | ||
// wrap the call to keys, so we can see if it's called | ||
var callCount = 0; | ||
var wrap = function(fn){ | ||
return function(){ | ||
console.log('What!?'); | ||
callCount++; | ||
return fn.apply(this, arguments); | ||
}; | ||
}; | ||
cache.client.keys = wrap(cache.client.keys); | ||
cache.add('wildkey1', 'abc', | ||
function ($error, $name, $entry) { | ||
cache.add('wildkey2', 'def', | ||
function ($error, $name, $entry) { | ||
cache.get('wildkey1', function ($error, $results) { | ||
try { | ||
$results.should.be.an.Array; | ||
$results.should.have.a.lengthOf(1); | ||
callCount.should.equal(0); | ||
done(); | ||
} catch (e) { | ||
done(e); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
})(); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1778
1
78090
26