Git Product home page Git Product logo

rediscache's Introduction

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

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 adding Redis caching to your actions.

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

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

    // 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 ([]).

More Compact?

The above example may seem quite verbose for one line, however it's merely invoking a sequence of methods as is typical with promises. To make it more compact you could place each method into your object.

var responder = {};
cache
    .fetch('words')
    .otherwise(responder.loadCollection)
    .then(responder.sendResponse)
    .fail(responder.sendEmptyResponse);

That way you could abstract the responder and use that object for each one of your collection calls – otherwise sends the cache key as the second argument for abstraction purposes.

rediscache's People

Contributors

wildhoney avatar cdupoiron avatar

Watchers

 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.