Skip to content

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

License

Notifications You must be signed in to change notification settings

otwm/express-cache

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published