Closed manuarzave closed 3 years ago
Hi,
I think you might need to add a console.log
statement inside your getUsers
function to see if it's really getting called every time.
hi, thanks for the repply, i added that console.log, and also debbug it, and it is enter twice in the getUsers()
Does this code work for you?
const cacheManager = require('cache-manager');
const memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10});
function getUsersFromDb(key, cb) {
setTimeout(() => {
console.log("Fetching users from slow database.");
cb(null, [{id: 123, name: 'Bob'}, {id: 999, name: 'AnotherUser'}]);
}, 100);
}
function getWrappedUsers(key, cb) {
multiCache.wrap(key, innerCb => {
getUsersFromDb(key, innerCb);
}, (err, users) => {
cb(err, users);
});
}
const key = '123456';
const isCacheableValue = value => value !== null && value !== false && value !== undefined;
const multiCache = cacheManager.multiCaching(
[memoryCache], {isCacheableValue}
);
getWrappedUsers(key, (err, users) => {
console.log(users);
getWrappedUsers(key, (err, users) => {
console.log("This should come from cache");
console.log(users);
process.exit();
});
});
It should print:
Fetching users from slow database.
[ { id: 123, name: 'Bob' }, { id: 999, name: 'AnotherUser' } ]
This should come from cache
[ { id: 123, name: 'Bob' }, { id: 999, name: 'AnotherUser' } ]
thanks, it is working, i will try to acomplish this with my original algorithm
Hi bryan im @manuarzave partner. As he mention we try your example and worked but if we try to adapt it to make a request to our controller it entered the first time and in the second try it get freezed. here is our code. Could you realize why this is happening?
import {login} from controller/login;
function getWrappedUsers(key, cb, req, res) {
multiCache.wrap(key, () => {
login(req, res);
}, (err, users) => {
cb(err, users);
});
}
const key = '123456';
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) => {
getWrappedUsers(key, () => {
getWrappedUsers(key, () => {
console.log('This should come from cache');
process.exit();
}, req, res);
}, req, res);
});
I think you need a callback for the wrap
function:
function getWrappedUsers(key, cb, req, res) {
multiCache.wrap(key, wrapCallback => {
login(req, res, wrapCallback);
}, (err, users) => {
cb(err, users);
});
}
Does your login
function take a callback?
Hi bryan you were right, my problem was the missing callback in my login function. Now it is working correctly. Thanks for all.
No problem!
Hi, im trying to get a list of users, but the .wrap() function always fire the getUsers method and hit the database. this is my code
const cacheManager = require('cache-manager');
const isCacheableValue = (value) => value !== null && value !== false && value !== undefined;
const memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 60 }); // ttl=60 seconds
const multiCache = cacheManager.multiCaching( [memoryCache], { isCacheableValue }, );
function getWrapUsers(req, res) { const cacheKey = req.body.rol; multiCache.wrap(cacheKey, getUsers(req, res), (err, data) => { console.log(
RETURN FROM DATABASE ${cacheKey}
, err, data); multiCache.wrap(cacheKey, getUsers(req, res), (err2, data2) => console.log(RETURN FROM CACHE ${cacheKey}
, err2, data2)); }); }router.post('/api/getUser', [validateFields], (req, res) => { getWrapUsers(req, res); });