rediscache

Simple Node.js based Redis cache for storing large collections of data.

npm install rediscache
2 downloads in the last week
11 downloads in the last month

RedisCache

 

Install with npm: npm install rediscache

Simple Node.js based Redis cache for storing large collections of data.

RedisCache makes it very simple to asynchronously respond with a collection of models from the cache. It uses method chaining for clarity in conjunction with the promise pattern.

Installation

It's a prerequisite to have Redis installed. Once installed you can begin the Redis server with redis-server.

Initiate the Node.js server with node example/server.js and then point your browser to localhost:3000.

Getting Started

Using Node.js you need to first include RedisCache to begin using it.

var cache = require('../package/redis-cache');

RedisCache will establish a connection with the Redis server once you've invoked connect – passing in the port, host, and password – all of which being optional.

By default RedisCache will attempt to connect to 127.0.0.1 on port 6379 with no auth password.

cache.connect(6379).configure({
    expiry: 86400
});

With the above code example we're also invoking configure which allows us to specify additional options such as expiry (default is 86400).

RedisCache should now have connected with Redis, and you're ready to begin the code into your actions.

Like promises – which RedisCache uses, you need to setup a method chain for each step. RedisCache uses: fetch('cache-name') -> otherwise(function() {}) -> then(function(models) {}) -> fail(function() {}).

cache.fetch('words').otherwise(function(deferred) {

    // Read the file because we don't have a cache object currently.
    fs.readFile('words.json', 'utf8', function(error, models) {

        // Store the data in the Redis cache object.
        deferred.resolve(JSON.parse(models));

    });

}).then(function(models) {

    // We have the data so we can output it to the browser!
    response.send(models);

}).fail(function() {

    // Invoked when you reject the promise above.
    response.send([]);

});

In the above code we first attempt to load the cache with fetch, and if that doesn't exist then the otherwise method will be invoked – it's up to you to resolve or reject the promise. If it succeeds then it will go into then to output using response.send, else it will fall into fail and output an empty array ([]).

npm loves you