app2-api/middlewares/cache.js

23 lines
584 B
JavaScript
Raw Normal View History

2019-07-24 21:03:10 +00:00
const apicache = require('apicache');
const redis = require('redis');
let cacheWithRedis = apicache
.options({
debug: true,
defaultDuration: '1 hour',
redisClient: redis.createClient()
})
.middleware;
// higher-order function returns false for responses of other status codes (e.g. 403, 404, 500, etc)
const onlyStatus200 = (req, res) => res.statusCode === 200
const cacheSuccesses = cacheWithRedis('1 hour', onlyStatus200);
2019-07-25 18:05:24 +00:00
const cache1year = cacheWithRedis('1 year', onlyStatus200);
2019-07-24 21:03:10 +00:00
module.exports = {
cacheSuccesses,
2019-07-25 18:05:24 +00:00
cache1year
2019-07-24 21:03:10 +00:00
}