From 6d18a94eae5365d2be09eb6b80f98192633034bf Mon Sep 17 00:00:00 2001 From: Ruben Gil Date: Thu, 25 May 2017 02:00:52 -0400 Subject: [PATCH 1/5] Add test for an expiration exceeding setTimeout's maximum time --- test.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index 9a770f7..d3140ce 100644 --- a/test.js +++ b/test.js @@ -84,6 +84,17 @@ describe('node-cache', function() { expect(spy).to.have.been.calledOnce.and.calledWith('key', 'value'); }); + it('should cause the timeout callback to fire once 30 days later', function() { + var spy = sinon.spy(); + const thirtyDays = 2592000000; + const timeoutMax = 2147483648; + cache.put('key', 'value', thirtyDays, spy); + clock.tick(timeoutMax); + expect(spy).to.not.have.been.called; + clock.tick(thirtyDays-timeoutMax); + expect(spy).to.have.been.calledOnce.and.calledWith('key', 'value') + }); + it('should override the timeout callback on a new put() with a different timeout callback', function() { var spy1 = sinon.spy(); var spy2 = sinon.spy(); @@ -196,7 +207,7 @@ describe('node-cache', function() { clock.tick(1000); expect(spy).to.not.have.been.called; }); - + it('should handle deletion of many items', function(done) { clock.restore(); var num = 1000; From dfb9ccd3763e114253d66831667f5522d25eeefe Mon Sep 17 00:00:00 2001 From: Ruben Gil Date: Thu, 25 May 2017 02:02:38 -0400 Subject: [PATCH 2/5] Modify put function to support a time that exceeds setTimeout's maximum time --- index.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c112383..601181c 100644 --- a/index.js +++ b/index.js @@ -29,13 +29,34 @@ exports.put = function(key, value, time, timeoutCallback) { expire: time + Date.now() }; - if (!isNaN(record.expire)) { - record.timeout = setTimeout(function() { - _del(key); - if (timeoutCallback) { - timeoutCallback(key, value); + + const extendedTimeout = (expiresMs) => { + // Max time that setTimeOut can handle (in milliseconds) + const maxTimeoutMs = 2147483647; + // If the time passed in to extendedTimeout is too large for setTimeout, the + // maxTimeoutMs has to be used instead. + const timeoutMs = (expiresMs > maxTimeoutMs) ? maxTimeoutMs : expiresMs; + + + record.timeout = setTimeout(() => { + // Calculates how much time is left till the cached data expires + const timeLeft = expiresMs - timeoutMs; + + // When timeLeft is <= 0, then the cached data has expired + if (timeLeft <= 0) { + _del(key); + if (timeoutCallback) { + timeoutCallback(key, value); + } + } else { + extendedTimeout(timeLeft); } - }, time); + }, timeoutMs); + }; + + + if (!isNaN(record.expire)) { + extendedTimeout(time); } cache[key] = record; From bf6f2f38061cba45676e8a195d633d75f624ad14 Mon Sep 17 00:00:00 2001 From: Ruben Gil Date: Thu, 25 May 2017 20:32:51 -0400 Subject: [PATCH 3/5] Change all const variables to var --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 601181c..167308b 100644 --- a/index.js +++ b/index.js @@ -30,17 +30,17 @@ exports.put = function(key, value, time, timeoutCallback) { }; - const extendedTimeout = (expiresMs) => { + var extendedTimeout = (expiresMs) => { // Max time that setTimeOut can handle (in milliseconds) - const maxTimeoutMs = 2147483647; + var maxTimeoutMs = 2147483647; // If the time passed in to extendedTimeout is too large for setTimeout, the // maxTimeoutMs has to be used instead. - const timeoutMs = (expiresMs > maxTimeoutMs) ? maxTimeoutMs : expiresMs; + var timeoutMs = (expiresMs > maxTimeoutMs) ? maxTimeoutMs : expiresMs; record.timeout = setTimeout(() => { // Calculates how much time is left till the cached data expires - const timeLeft = expiresMs - timeoutMs; + var timeLeft = expiresMs - timeoutMs; // When timeLeft is <= 0, then the cached data has expired if (timeLeft <= 0) { @@ -53,7 +53,7 @@ exports.put = function(key, value, time, timeoutCallback) { } }, timeoutMs); }; - + if (!isNaN(record.expire)) { extendedTimeout(time); From 1249c59d57912fac379b0a7af8a8a37d07b394f3 Mon Sep 17 00:00:00 2001 From: Ruben Gil Date: Thu, 25 May 2017 20:37:24 -0400 Subject: [PATCH 4/5] Change all const variables in tests to var --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index d3140ce..83386bc 100644 --- a/test.js +++ b/test.js @@ -86,8 +86,8 @@ describe('node-cache', function() { it('should cause the timeout callback to fire once 30 days later', function() { var spy = sinon.spy(); - const thirtyDays = 2592000000; - const timeoutMax = 2147483648; + var thirtyDays = 2592000000; + var timeoutMax = 2147483648; cache.put('key', 'value', thirtyDays, spy); clock.tick(timeoutMax); expect(spy).to.not.have.been.called; From a02431c38f1fb48c4d92bcc45ee47e17c7a9d7ee Mon Sep 17 00:00:00 2001 From: Ruben Gil Date: Thu, 25 May 2017 20:47:12 -0400 Subject: [PATCH 5/5] Change all arrow functions to ES5 functions --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 167308b..9ac98d5 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ exports.put = function(key, value, time, timeoutCallback) { }; - var extendedTimeout = (expiresMs) => { + var extendedTimeout = function(expiresMs) { // Max time that setTimeOut can handle (in milliseconds) var maxTimeoutMs = 2147483647; // If the time passed in to extendedTimeout is too large for setTimeout, the @@ -38,7 +38,7 @@ exports.put = function(key, value, time, timeoutCallback) { var timeoutMs = (expiresMs > maxTimeoutMs) ? maxTimeoutMs : expiresMs; - record.timeout = setTimeout(() => { + record.timeout = setTimeout(function() { // Calculates how much time is left till the cached data expires var timeLeft = expiresMs - timeoutMs;