A module to make Express interact with Redis (create, get, delete). You can automatically cache all your most popular routes in Redis.
npm install -g express-redis-cache
Just use it as a middleware in your route.
var cache = require('express-redis-cache')();
// replace
app.get('/',
function (req, res) { ... });
// by
app.get('/',
cache.route(),
function (req, res) { ... });
This will check if there is a cache entry for this route. If not. it will cache it and serve the cache next time route is called.
By default, redis-express-cache connects to Redis using localhost as host and nothing as port (using Redis default port). To use different port or host, declare them when you require express-redis-cache.
var cache = require('express-redis-cache')({
host: String, port: Number
});
You can pass a Redis client as well:
require('express-redis-cache')({ client: require('redis').createClient() })
You can catch errors by adding a listener:
cache.on('error', function (error) {
// ...
});
express-redis-cache
logs some information at runtime. You can access them like this:
cache.on('message', function (message) {
// ...
});
Object Entry {
body: String // the content of the cache
touched: Number // last time cache was set (created or updated) as a Unix timestamp
expire: Number // the seconds cache entry lives (-1 if does not expire)
}
Object ConstructorOptions {
host: String? // Redis Host
port: Number? // Redis port
prefix: String? // Cache entry name prefix,
expire: Number? // Default expiration time in seconds
client: RedisClient // A Redis client of npm/redis
errors: bool? // If true, errors are emitted. Otherwise, suppressed.
}
cache( Object ConstructorOPtions? )
cache.route( String name?, Number expire? )
If name
is a string, it is a cache entry name. If it is null, the route's URI (req.originalUrl
) will be used as the entry name.
app.get('/', cache.route('home'), require('./routes/'))
// will get/set a cache entry named 'home'
app.get('/about', cache.route(), require('./routes/'))
// will get/set a cache entry named '/about'
Optionally, you can get more naming control on defining res.expressRedisCacheName
:
app.get('/user/:userid',
function (req, res, next) {
res.expressRedisCacheName = '/user/' + req.params.userid;
next();
},
cache.route(),
require('./routes/user')
);
app.post('/search',
function (req, res, next) {
res.expressRedisCacheName = '/search/' + req.body.tag;
next();
},
cache.route(),
require('./routes/user')
);
The number of seconds the cache entry will live
cache.route('home', ( 60 * 5 ));
// cache will expire in 5 minutes
If you don't define an expiration date in your route but have set a default one in your constructor, the latter will be used. If you want your cache entry not to expire even though you have set a default expiration date in your constructor, do like this:
cache.route('my-page', cache.FOREVER);
cache.ls( Function ( Error, [Entry] ) )
Feed a callback with an array of the cache entry names.
cache.get( String name, Function( Error, Entry ) )
cache.add( String name, String body, Number expire?, Function( Error, Entry ) )
Example:
cache.add('user:info', JSON.stringify({ id: 1, email: '[email protected]' }), 60, console.log);
cache.del( String name, Function ( Error, Number deletedEntries ) )
cache.size( Function ( Error, Number bytes ) )
We ship with a CLI. You can invoke it like this: express-redis-cache
node test/test --host <redis-host> --port <redis-port>
# or
npm test --host=<redis-host> --port=<redis-port>