Socket
Socket
Sign inDemoInstall

rediscache

Package Overview
Dependencies
5
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    rediscache

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


Version published
Weekly downloads
213
increased by5.97%
Maintainers
1
Install size
407 kB
Created
Weekly downloads
 

Readme

Source

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.

Keywords

FAQs

Last updated on 12 Mar 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc