wizjack
wizjack

Reputation: 91

Node cache doesn't fire the "on expired" callback as intended

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

Answers (2)

CapitaineToinon
CapitaineToinon

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

Link the doc explaining this.

Upvotes: 6

Asad Ullah
Asad Ullah

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

Related Questions