Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
express-redis-cache
Advanced tools
A module to make Express interact with Redis (create, get, delete). You can automatically cache all your most popular routes in Redis.
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
}
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.path
) 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'
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: 'john@doe.com' }), 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>
FAQs
A module to make Express interact with Redis (create, get, delete). You can automatically cache all your most popular routes in Redis.
The npm package express-redis-cache receives a total of 3,985 weekly downloads. As such, express-redis-cache popularity was classified as popular.
We found that express-redis-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.