Code Monkey home page Code Monkey logo

express-redis-cache's Introduction

express-redis-cache

A module to make Express interact with Redis (create, get, delete). You can automatically cache all your most popular routes in Redis.

Install

npm install -g express-redis-cache

Automatically cache a route

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.

Redis connexion info

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() })

Errors

You can catch errors by adding a listener:

cache.on('error', function (error) {
    // ...
});

Messages

express-redis-cache logs some information at runtime. You can access them like this:

cache.on('message', function (message) {
    // ...
});

Objects

The Entry object

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)
}

The ConstructorOptions Object

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.
}

Commands

Constructor

cache( Object ConstructorOPtions? )

Route

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')
);

Set an expiration date for the cache entry

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);

Get the list of all cache entries

cache.ls( Function ( Error, [Entry] ) )

Feed a callback with an array of the cache entry names.

Get a single cache entry by name

cache.get( String name, Function( Error, Entry ) )

Add a new cache 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);

Delete a cache entry

cache.del( String name, Function ( Error, Number deletedEntries ) )

Get cache size for all entries

cache.size( Function ( Error, Number bytes ) )

Command line

We ship with a CLI. You can invoke it like this: express-redis-cache

Test

node test/test --host <redis-host> --port <redis-port>

# or

npm test --host=<redis-host> --port=<redis-port>

express-redis-cache's People

Contributors

co2-git avatar faceair avatar tcbeutler avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.