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

redis-cache-express

v1.3.1

Published

Provides a seamless way to cache response data when using express

Downloads

35

Readme

Redis-Express caching middleware

This library implements an Express middleware that allows for caching of HTML / JSON web responses using a Redis store.

Note:

  • This library has a hard-dependency on the Node Redis NPM package_
  • This middleware will only cache requests that return a 200 HTTP status code

Installation

npm install --save redis-express-cache

Usage Examples

Initialize the middleware using a Redis client:

/*
Scenario:
- You have an already existing node-redis instance
- Connected node-redis instance is passed to the cache-redis middleware
*/

var app = express()();
var redis_cache = require('redis-express-cache');
var redis = require('redis');

var client = redis.createClient();

var options = { client: redis_client };

redis_cache = redis_cache(options).middleware;

app.get('/url_to_cache', function (req, res) {
  res.send('Will cache this response!')
}, redis_cache)

app.listen(3000, function () {
  console.log('Starting up!')
});

Initialize the middleware without a Redis client [connection options are provided]:


/*
Scenario:
- A connected Redis client is not provided
- Connection options ( host and port ) are provided instead
- Middleware will attempt a connection on provided connection params
*/

var app = express()();
var redis_cache = require('redis-express-cache');
var connection_options = { port: 6379, host: 'localhost' }

redis_cache = redis_cache(connection_options).middleware;

app.get('/url_to_cache', function (req, res) {
  res.send('Will cache this response!')
}, redis_cache)

Initialize the middleware without a Redis client [connection options are not provided]:


/*
Scenario:
- A connected Redis client is not provided
- Connection options ( host and port ) are not provided either
- Middleware will attempt a connection to Redis on default values ( localhost : 6379 )
*/

var app = express()();
var redis_cache = require('redis-express-cache')().middleware;

app.get('/url_to_cache', function (req, res) {
  res.send('Will cache this response!')
}, redis_cache)

Enable cache invalidation through URL query params:


/*
Scenario:
- For a cached URL : http://www.example-domain.com/cached_path
- Purge it using   : http://www.example-domain.com/cached_path?refresh_cache=8c8c279a7a98069d432271c8db9d7df2
*/

var hash_value = '8c8c279a7a98069d432271c8db9d7df2'
var refresh_key = 'refresh_cache';

var options = {
  invalidate: {
    param_key: refresh_key,
    param_value: hash_value
  }
}

var redis_cache = redis_cache(options).middleware;

app.get('/url_to_cache', function (req, res) {
  res.send('Will cache this response!')
}, redis_cache)

Available options:

Redis client

  • Pass in a Redis client that the middleware can use as a cache store
  • When present, the provided client will be used
  • When absent, the middleware will attempt a connection to a client on localhost
/* Usage */
var options = { client: redis.createClient() }

TTL

  • Provide a number in seconds that dictates the cache duration
  • When present, the given URL will be cached for given seconds
  • When absent, the given URL will be cached without an expiry
/* Usage */
var options = { ttl: 86400 } /* Cache for 1 day */

Include host

  • Enables usage of cache keys that include host + url path instead of just the url path (default)
  • When disabled a cache key will consist only of the url => /some/random/path
  • When enabled a cache key will include the host as well => www.some-domain.com/some/random/path
/* Usage */
var options = { include_host: true } /* Cache for 1 day */

Enable cache invalidation via GET query params

  • Allows for easy invalidation of cache entries by appending a GET param to the concerned URL
  • Example:
    • Cached URL => https://www.url.com/path_to_cache
    • Can be invalidated by calling https://www.url.com/path_to_cache?refresh_cache=true
/* Usage */
var options = {
  invalidate: {
    param_key: 'refresh_cache',
    param_value: 'true'
  }
}