Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkperiod only trigger once? #78

Closed
JimmyHwang opened this issue Dec 17, 2016 · 5 comments
Closed

checkperiod only trigger once? #78

JimmyHwang opened this issue Dec 17, 2016 · 5 comments

Comments

@JimmyHwang
Copy link

I tried following code, but seems only show "A:1 is expired", "A:2" never be show

var myCache = new node_cache( { stdTTL: 50, checkperiod: 20 } );
myCache.set("A:1", Code, 10);
myCache.set("A:2", Code, 25);

myCache.on("expired", function( key, value ){
console.log(key + " is expired");
// ... do something ...
});

How can make it run the expired check by interval?

@mpneuried
Copy link
Contributor

probably your script will just end before your defined ttl's expire.
i use .unref() to prevent node-cache from blocking your script to exit.
So if your script ends teh process will stopped.

You can just run your own timeout like this example:

var node_cache = require("node-cache");
var myCache = new node_cache( { stdTTL: 50, checkperiod: 2 } );
myCache.set("A:1", 123, 5);
myCache.set("A:2", 456, 10);

myCache.on("expired", function( key, value ){
  console.log(key + " is expired");
  // ... do something ...
});
console.log( "waiting ... " );

// add a timeout to prevent the script to exit
setTimeout( function(  ){
  console.log( "exit" );
}, 15000 )

See working example here: https://runkit.com/mpneuried/58576ddb558ed60013d25574

@JimmyHwang
Copy link
Author

Yes, I put the code to runkit is work well, but it is doesn't work on my 2 linux machine, Do you try the code on real machine?

Ubuntu 14.04 x64
node-cache 4.1.0

@mpneuried
Copy link
Contributor

i tested some environments with docker.

The only case i found your problem is ubuntu:14.04 with node v0.10.25.

Which Node version do you use?

-------------
Docker Test (FROM node:0.10)
-------------
Node Version: v0.10.48

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM node:0.12)
-------------
Node Version: v0.12.17

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM node:4.2)
-------------
Node Version: v4.2.6

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM node:5.0)
-------------
Node Version: v5.0.0

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM node:5.4)
-------------
Node Version: v5.4.1

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM node:argon)
-------------
Node Version: v4.7.0

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM node:latest)
-------------
Node Version: v7.2.1

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM mhart/alpine-node-auto:4)
-------------
Node Version: v4.7.0

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM mhart/alpine-node-auto:6)
-------------
Node Version: v6.9.2

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM mhart/alpine-node-auto:7)
-------------
Node Version: v7.2.1

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM ubuntu:14.04)
-------------
Node Version: v0.10.25

waiting ...
exit

-------------
Docker Test (FROM ubuntu:14.04)
-------------
Node Version: v4.7.0

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM ubuntu:16.04)
-------------
Node Version: v4.2.6

waiting ...
A:1 is expired
A:2 is expired
exit

-------------
Docker Test (FROM ubuntu:16.10)
-------------
Node Version: v4.2.6

waiting ...
A:1 is expired
A:2 is expired
exit

mpneuried added a commit that referenced this issue Dec 20, 2016
@JimmyHwang
Copy link
Author

I tried update nodejs to v6.9.2, it is work well, but it is not default for Ubuntu apt-get install, so I look into the code of node-cache, the checkTimeout without parameters, so default parameter will be 0 on nodejs 0.10.25, cause startPeriod = 0 on 2nd time, it is will not create timeout again when trigger once

NodeCache.prototype._checkData = function(startPeriod) {
this.checkTimeout = setTimeout(this._checkData, this.options.checkperiod * 1000);

change to

NodeCache.prototype._checkData = function(startPeriod) {
this.checkTimeout = setTimeout(this._checkData, this.options.checkperiod * 1000, startPeriod);

it is work well on nodejs 0.10.25
I guess nodejs 6.9.2 timeout event will bring current parameter into, but maybe we should specified it

@mpneuried
Copy link
Contributor

I added your fix to 4.1.1 and tested it against a bunch of versions and environments.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants