Reputation: 91
I'm trying to use node cache's "on expired" event to pull latest data as and when the TTL for the cache key value pair ends. My understanding is it should get called immediately when the cache expires but I think it doesn't gets called until check period ends and the cache cleans up the expired values. Is that so or I'm doing something wrong?
https://www.npmjs.com/package/node-cache#expired
Upvotes: 2
Views: 6115
Reputation: 81
By default, node-cache will not periodically check for expired keys and thus the on expired event will only trigger once the key is accessed. You can change this behavior by setting the checkperiod
option when creating node-cache.
checkperiod: (default: 600) The period in seconds, as a number, used for the automatic delete check interval. 0 = no periodic check.
const NodeCache = require( "node-cache" );
const myCache = new NodeCache({ checkperiod: 120 }); // will check every 120 seconds
Upvotes: 6
Reputation: 31
I was also trying to achieve this. My experience was following, as you described:
My understanding is it should get called immediately when the cache expires but I think it doesn't gets called until check period ends and the cache cleans up the expired values
Upvotes: 0