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

node-supercache

v1.0.6

Published

An efficient and fast cache operation for API endpoints

Downloads

10

Readme

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.