jaredwray / cacheable

a robust, scalable, and maintained set of caching packages
https://cacheable.org
MIT License
1.63k stars 165 forks source link

Setting a multicache get freeze. #167

Closed nicosebey closed 2 years ago

nicosebey commented 3 years ago

Hi bryan im back. Now im trying to get my cache with multiples levels using cache-manager-ioredis (https://github.com/dabroek/node-cache-manager-ioredis#readme) but after setting it up and on the .wrap it get freeze. this is mi code. cacheManager.js

const cacheManager = require('cache-manager');
const redisStore = require('cache-manager-ioredis');

const isCacheableValue = (value) => value !== null && value !== false && value !== undefined;

// Redis Store Cache
const redisCache = cacheManager.caching(
    {
        store: redisStore,
        host: process.env.SQL_SERVER_HOST,
        port: process.env.SQL_SERVER_PORT,
        auth_pass: process.env.SQL_SERVER_PASS,
        refreshThreshold: 3,
        isCacheableValue,
        ttl: 600,
    },

const memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 600 }); // ttl=60 seconds

const multiCache = cacheManager.multiCaching([memoryCache, redisCache]);

module.exports = multiCache;

calling the endpoint

const multiCache = require('../services/cacheManager');
const { login } = require('../controller/auth');

function getWrappedUsers(key, req, res) {
    multiCache.wrap(key, (wrapCallback) => {
        login(req, res, wrapCallback);
    }, (err, users) => {
        if (!err) {
            res.json(users);
        } else {
            res.status(err.status).msg(err.msg);
        }
    });
}

router.post('/api/auth/login', [
    check('username', 'El nombre de usuario es obligatorio').not().isEmpty(),
    check('password', 'La contraseña es obligatoria').not().isEmpty(),
    validateFields,
], (req, res) => {
    const key = req.body.username + req.body.password;
    console.log(key);
    getWrappedUsers(key, req, res);
});

I hope you could figure out what's going on.

Nico

BryanDonovan commented 3 years ago

I would first make sure the 'single store' example is working (see https://github.com/dabroek/node-cache-manager-ioredis#single-store) with your redis config.

nicosebey commented 3 years ago

You were right again bryan. the problem was with node-cache-manager-ioredis but i realize that we didnt need it. We could just create a multiple cache with cache-manager. Now I ve 2 questions about this. 1) I am able to select in which cache level I want to save any data? 2) There is any method to invalidate any cache level (clean it) ? Thanks again!

BryanDonovan commented 3 years ago

Do you mean you'd make a multi-cache with two memory caches?

There is no way to specify which level to save to and there's no way to invalidate data at any particular level either. You can use the del method to delete at all levels though. You can always use a particular cache directly though, bypassing the multi-cache altogether.

nicosebey commented 3 years ago

Thanks bryan making some tests I realized about what you are saying, I also found the reset method to reset all the cache! Thank you for everything.

BryanDonovan commented 3 years ago

No problem!