0.1.5 • Published 5 years ago
cache-response v0.1.5
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
Property | Default | Description |
---|---|---|
duration | 12 hours | ttl in redis |
key | req.originalUrl | key in redis |
prefix_key | string: key in redis has a same prefix to facilitate remove multiple keys |