1.0.6 • Published 2 years ago
node-supercache v1.0.6
Node SuperCache
Installation
npm install node-supercache
Quick start
- 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
});
- 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,
});
- 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,
});
- 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)
}
- 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...')
});
- Execute
curl -X GET http://localhost:8080/api/v1/un-cached
to get the un-cached api response all the time and use - Execute
curl -X GET http://localhost:8080/api/v1/cached
to get cached api response after first try.