callTimeout option notworking
Closed this issue · 1 comments
iamhassanfaraji commented
i set callTimeout for reset caching but dont work
node: 18.12.1
nano-memoize: 2.0.0
docker image: node:18.12.1
const checkAccess = memoization(async function (user, ip) {
const type = typeApproach(user)
switch (type) {
case 'easy':
const checkTraffic = await checkMaximumTraffic(user)
report(checkTraffic.resultCheck, user, ip)
return checkTraffic
case 'hard':
const result = await hardApproach(user, ip)
report(result.resultCheck, user, ip)
return result
}
}, { callTimeout: 5000 }) //5 seconds
anywhichway commented
@hassan-faraji callTimeout
is not for flushing the cache. It is for invoking callbacks. Use maxAge
. See these unit tests:
it("callTimeout",function(done) {
const callTimeout = nanomemoize(function(a,b,cb) { var result = a + b; cb(result); return result; },{maxArgs:2,callTimeout:10});
let result = 0;
const res1 = callTimeout(1,2,(value) => result = value + 1);
expect(res1).to.equal(3);
setTimeout(() => {
expect(result).to.equal(4);
done();
},100)
});
it("maxAge - flush cache",function(done) {
const memoized = nanomemoize((a,b) => a + b,{maxAge: 100})
let keys = memoized.keys(),
values = memoized.values();
expect(keys.length).to.equal(0);
expect(values.length).to.equal(0);
const response = memoized(1,2);
expect(response).to.equal(3);
keys = memoized.keys();
values = memoized.values();
expect(keys.length).to.equal(1);
expect(keys[0][0]).to.equal(1);
expect(keys[0][1]).to.equal(2);
expect(values.length).to.equal(1);
expect(values[0]).to.equal(3);
setTimeout(() => {
let keys = memoized.keys(),
values = memoized.values();
expect(keys.length).to.equal(0); // cache cleared
expect(values.length).to.equal(0); // cache cleared
const response = memoized(1,3);
expect(response).to.equal(4);
keys = memoized.keys();
values = memoized.values();
expect(keys.length).to.equal(1); // new cache value
expect(keys[0][0]).to.equal(1);
expect(keys[0][1]).to.equal(3);
expect(values.length).to.equal(1);
expect(values[0]).to.equal(4);
done();
},1000)
});