node-supercache
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Node SuperCache

Installation

npm install node-supercache

Quick start

  1. Setup individual Redis connections for Publishing, Subscribing and general activity.
import { Cached } from 'node-supercache';
import Client from "ioredis";
import Redlock from "redlock";

const redisPath = '127.0.0.1';
const redisPort = 6379;


const redisCommonOptions: any = {
    host: redisPath,
    port: redisPort,
    keepAlive: 1,
}

export const redisMain = new Client({
    enableAutoPipelining: true,
    ...redisCommonOptions
});


const redisSubscriber = new Client({
  autoResubscribe: true,
  enableAutoPipelining: true,
  ...redisCommonOptions
});


const redisPublisher = new Client({
  enableAutoPipelining: true,
  ...redisCommonOptions
});
  1. Initialize Redlock instance with desired settings and set of Redis nodes(3 for optimal performance) with a retry count as 0.
const redlock = new Redlock([redisMain], {
  driftFactor: 0.01,
  retryCount: 0,
  retryDelay: 100,
  retryJitter: 200,
  automaticExtensionThreshold: 200,
});
  1. Initialise node-supercache Cache instance with the above Redis connections and Redlock instance.
export const Cache = new Cached({
  redisMain: redisMain,
  redisPublisher: redisPublisher,
  redisSubscriber: redisSubscriber,
  redlock: redlock,
  debug: false,
});
  1. Setup Express HTTP server with a simple API endpoint which return a Date string.
const express = require("express");
import objectHash from 'object-hash';
var app = express()


const endpointHandler = async function(req: any, res: any){
  console.log('Produce data from Handler(not in cache)')
  await new Promise(resolve => setTimeout(resolve, 2000))
  return {
      data: new Date()
    }
}

const unCachedHandele = async (req: any, res: any, next: any) => {
    const data = await endpointHandler(req,res)
    return res.status(200).send(data)
}
  1. Setup 2 endpoints one without the caching mod and the other with caching.

Note: Currently the endpoint handler returned by the Caching mod needs an extra handle to resolve the request after processing.

app.get('/api/v1/un-cached', unCachedHandele)

app.get('/api/v1/cached',
    Cache.control({
      ttl: 100,
      prefix: '/api/v1/cached',
      callback: endpointHandler,
      cacheKeyHandle: async(req,res) => {
          return objectHash({
              url: req.originalUrl,
              body: req.body
          })
      }
    }),
    // Second handle to resolve the request, as the primary handler always calls next()
    async function(req: any, res: any){
      res.status(200).send(res.data)
    }
)

app.listen(8080, () => {
  console.log('listening on 8080...')
});
  1. Execute curl -X GET http://localhost:8080/api/v1/un-cached to get the un-cached api response all the time and use
  2. Execute curl -X GET http://localhost:8080/api/v1/cached to get cached api response after first try.

Readme

Keywords

Package Sidebar

Install

npm i node-supercache

Weekly Downloads

0

Version

1.0.6

License

MIT

Unpacked Size

33.3 kB

Total Files

15

Last publish

Collaborators

  • baxtech
  • kartis