Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

itp-express-redis-cache

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

itp-express-redis-cache

A light api route cache system with Redis for Express

  • 1.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-20%
Maintainers
1
Weekly downloads
 
Created
Source

ITP Express Redis Cache

NPM version MIT License

A light api route cache system with Redis for Express.js

Installation

npm install itp-express-redis-cache

Usage

const express = require('express');
const app = express();

const ITPExpressRedisCache = require('itp-express-redis-cache')();

app.get('/', ITPExpressRedisCache.route(), (req, res) => {
  res.json({ foo: 'bar' });
});

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

Options

Init options:

const ITPExpressRedisCache = require('itp-express-redis-cache')({
  port: 6379, // redis port
  host: 'localhost', // redis host
  authPass: null, // redis pass
  db: 0, // redis database id
  prefix: 'my-sample-app', // redis key prefix, e.g. 'my-sample-app:route:GET:/'
  enabled: true, // disable/enable route caching, for example in debug mode
  excludeStatuscodes: 500, // disable response caching based on response statuscode. Possible values: number, array, function (excludes 500 and higher by default)
});

The excludeStatuscodes parameter can also be an array:

const ITPExpressRedisCache = require('itp-express-redis-cache')({
  port: 6379,
  host: 'localhost',
  authPass: null,
  db: 0,
  prefix: 'my-sample-app',
  enabled: true,
  excludeStatuscodes: [404, 406, 408, 410], // disable response caching based on response statuscode. Possible values: number, array, function (excludes 500 and higher by default)
});

Route middleware options:

ITPExpressRedisCache.route({
  key: 'custom-redis-key-for-route', // custom redis key
  expire: 120, // expiration time in seconds
})

The route key parameter can also be a function:

ITPExpressRedisCache.route({
  key: (req) => `custom-key:${req.originalUrl}`, // custom function
  expire: 120, // expiration time in seconds
})

The expire parameter can also be a function:

ITPExpressRedisCache.route({
  key: (req) => `custom-key:${req.originalUrl}`, // custom function
  expire: (req) => 120 + 4, // expiration time in seconds
})

Application-level middleware

Simply use app.use of express to use ITPExpressRedisCache as an Application-level middleware.

app.use(ITPExpressRedisCache.route())

Disable Caching inside the Route

You can disable caching for specific routes by adding res.skipCache = true to opt out the route from getting cached.

app.get('/:paramKey', (req, res) => {
    res.skipCache = paramKey === 1300;
    res.send('Hello');
});

Supported env variables

  • REDIS_HOST
  • REDIS_PORT
  • REDIS_PASS
  • REDIS_DB

License

ITP-Express-Redis-Cache is freely distributable under the terms of the MIT license.

Keywords

FAQs

Package last updated on 21 Mar 2018

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc