const apicache = require('apicache');
const redis = require('redis');
const config = require('../config');


const cacheWithRedis = apicache
    .options({         
        debug: config.cache.debug,
        defaultDuration: config.cache.defaultDuration,
        enabled: config.cache.enabled,
        //redisClient: redis.createClient(),
        //appendKey: (req, res) => req.user ? req.user.id : '',
});

const cacheMiddleware = cacheWithRedis.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 = (time) => { return cacheMiddleware(time, onlyStatus200) };

module.exports = {
    apicache: cacheWithRedis,
    cacheMiddleware,
    cacheSuccesses,
}

