npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rediscache

v0.2.0

Published

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

Downloads

1,057

Readme

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

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.