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

@otwmoo/express-cache

v0.0.13

Published

express-cache by LRU

Downloads

4

Readme

express cache

Cache Middleware for Express. The default is LRU-based cache

Build Status Coverage Status

how to use

basic

// server.js
import { getCacheMiddleware } from 'express-cache'
//...
app.use(getCacheMiddleware()) 

hook injection

// server.js
import { getCacheMiddleware } from 'express-cache'

// logging during cache operation
function logging(req, res, next, cached) {
  debug(`req info: ${req.url} ${req.method}`)
  debug(`cache info: ${key} ${value}`)
}

app.use(getCacheMiddleware({ hook: logging}))

Config LRU

ref. https://github.com/isaacs/node-lru-cache

// server.js
import { getCacheMiddleware } from 'express-cache'
//...
app.use(getCacheMiddleware({
  configLRU : {
    max: 1,
    maxAge: 100,
    length: function(value, key) {
      //...
    }
  }
}))
//

config cachePolicy

routeList 존재하면, 정확히 routeList 명시된 것에만 적용, 없으면 전체 적용 exceptList 존재하면, exceptList 제외

routeList If present, applies exactly to the routeList specified; exceptList Except, exceptList Except

// server.js
import { getCacheMiddleware, strategy } from 'express-cache'
//...
app.use(getCacheMiddleware({
  cachePolicy: {
    routeList: [ {
      id: 'someUrl',
      check: (req) => {
        return true // or false
      }, 
    }],
    exceptList: [{
      id: 'someUrl',
      check: (req) => {
        return true // or false
      }, 
    }],
  }
}))

inject LRU

cluster 등 LRU 공유 필요 시 외부에서 주입

// server.js
import { getCacheMiddleware } from 'express-cache'

//cluster, etc. If LRU sharing is required,
app.use(getCacheMiddleware({
  LRU: myLRU
}))

export LRU Cache

import { getLRU } from 'express-cache'

getLRU().resert()

default value

const sec = 1000
const minute = 1000 * 60
const hour = minute * 60
const halfDay = hour * 12
const day = hour * 24   

enum Time {
  sec, minute, hour, halfDay, day,
}

const defaultLRUConfig = {
  max: 1000,
  maxAge: 5 * Time.minute,
  // @ts-ignore
  length: function (value = '') {
    // @ts-ignore
    const stringLength = value => value.length / 10
    // @ts-ignore
    const NumberLength = value => value.toString().length

    const strOrNumberLength = (value: string | number) => {
      if (is(String, value)) {
        return stringLength(value)
      }
      if (is(Number, value)) {
        return NumberLength(value)
      }
      return 1 // other
    }
    if (is(Object, value)) {
      Object.values(value).map(strOrNumberLength).reduce((result, current) => {
        return result + current
      }, 0)
    }
    return strOrNumberLength(value)
  },
}

const generateKey: generateKeyFunc = function (req: express.Request) {
  return `${req.originalUrl}-${req.method}`
}

const isErrorFunc: isError = (_, res) => {
  return res.statusCode > 400
}

const resFunc: resFunc = (res: express.Response, cached) => {
  const reqUrl = path<string>(['req', 'url'], res)
  log(`cached send reqUrl : ${reqUrl}`)
  const {body, statusCode = 200, headersSent, headers} = cached
  if (headersSent && headers) {
    res.set(headers)
  }
  res.status(statusCode).send(body)
}

const defaultConfigCache = {
  configLRU: defaultLRUConfig,
  generateKey,
  resFunc,
  isError: isErrorFunc,
}

ref

https://github.com/isaacs/node-lru-cache