0.2.3 • Published 4 years ago

response-cache v0.2.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Installation

npm install response-cache

Example

// create redisClient
const redis = require('redis')
const ResponseCache = require('response-cache')
const redisClient = redis.createClient()
const responseCache = ResponseCache(redisClient)

const options = {
	duration: 60 // seconds,
}

app.get('/api/example', responseCache.cache(option), (req, res) => {
	  res.send('GET request to the homepage')
  })
// Cache all routes
app.use(responseCache.cache(option))

Clear cache

// create redisClient
const redis = require('redis')
const ResponseCache = require('response-cache')
const redisClient = redis.createClient()
const responseCache = ResponseCache(redisClient)

// manually
app.get('/api/clear', (req, res) => {
	responseCache.clear('key') // clear cache for key in redis
	res.send('clears cache')
  })  

options object properties

PropertyDefaultDescription
duration12 hoursttl in redis
keyreq.originalUrlkey in redis
prefix_keystring (boolean)

1. responseCache.cache(option)

  • I defined a string const cache-response: to Centralized management by key in redis. Each url saved to Redis has the form: cache-response:/api/example
  • I provide a prefix_key property with 2 data type: string and boolean to save in Redis in group.

... update