From a071d2ffd02f84eafb32240d8be2b95549729d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Palm=C3=A9r?= Date: Thu, 27 Oct 2016 14:58:29 +0200 Subject: [PATCH 1/5] Added maxTime as a option. It will check timestamp when you try to get an item. --- lib/simple_lru.js | 23 +++++++++++++++++------ test/simple_lru_tests.js | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/simple_lru.js b/lib/simple_lru.js index 82787b9..d30968e 100644 --- a/lib/simple_lru.js +++ b/lib/simple_lru.js @@ -4,11 +4,12 @@ * LRU cache based on a double linked list */ -function ListElement(before,next,key,value){ +function ListElement(before,next,time,key,value){ this.before = before this.next = next this.key = key this.value = value + this.time = time } ListElement.prototype.setKey = function(key){ @@ -19,11 +20,11 @@ ListElement.prototype.setValue = function(value){ this.value = value } - function Cache(options){ if(!options) options = {} this.maxSize = options.maxSize + this.maxTime = options.maxTime this.reset() } @@ -43,10 +44,20 @@ Cache.prototype.get = function(key,hit){ * it to the head of linked list */ hit = hit != undefined && hit != null ? hit : true; - if(cacheVal && hit) - this.hit(cacheVal) - else + + /* + * Define if maxTime if used and if it's valid + */ + + if(cacheVal && hit) { + if ((cacheVal.time == undefined) || (Date.now() - cacheVal.time < this.maxTime)) + this.hit(cacheVal) + else + return undefined; + } else { return undefined + } + return cacheVal.value } @@ -85,7 +96,7 @@ Cache.prototype.set = function(key,val,hit){ cacheVal.setValue(val) } - cacheVal = cacheVal ? cacheVal : new ListElement(undefined,undefined,key,val) + cacheVal = cacheVal ? cacheVal : new ListElement(undefined,undefined, this.maxTime ? Date.now() : undefined, key, val) this.cache[key] = cacheVal this.attach(cacheVal) } diff --git a/test/simple_lru_tests.js b/test/simple_lru_tests.js index 1229e05..ab7e93c 100644 --- a/test/simple_lru_tests.js +++ b/test/simple_lru_tests.js @@ -59,4 +59,25 @@ describe("BigCache Config",function(){ for(var i = 0; i < 100; i++) cache.get(i).should.equal("value_"+i+"_modif") }) + + it("Should not return timeout item", function(done){ + var cache = new SimpleCache({maxSize:1, maxTime: 1}) + cache.set("hello", "world") + setTimeout(function() + { + should.equal(cache.get("hello"), undefined); + done(); + }, 5); + }) + + it("Should return timeout item", function(done){ + var cache = new SimpleCache({maxSize:1, maxTime:100}) + cache.set("hello", "world") + setTimeout(function() + { + should.equal(cache.get("hello"), "world"); + done(); + }, 5); + }) + }) From aa09cef59dca9e4a407c3b2239be5159a3b190e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Palm=C3=A9r?= Date: Thu, 27 Oct 2016 15:21:00 +0200 Subject: [PATCH 2/5] clean up. --- lib/simple_lru.js | 15 +++++++-------- test/simple_lru_tests.js | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/simple_lru.js b/lib/simple_lru.js index d30968e..a2cbe04 100644 --- a/lib/simple_lru.js +++ b/lib/simple_lru.js @@ -46,14 +46,13 @@ Cache.prototype.get = function(key,hit){ hit = hit != undefined && hit != null ? hit : true; /* - * Define if maxTime if used and if it's valid - */ - - if(cacheVal && hit) { - if ((cacheVal.time == undefined) || (Date.now() - cacheVal.time < this.maxTime)) - this.hit(cacheVal) - else - return undefined; + * Define if maxTime is used and if it's valid. + */ + var cacheTimeValid = (cacheVal && cacheVal.time == undefined) || + (cacheVal && Date.now() - cacheVal.time < this.maxTime); + + if(cacheVal && hit && cacheTimeValid) { + this.hit(cacheVal) } else { return undefined } diff --git a/test/simple_lru_tests.js b/test/simple_lru_tests.js index ab7e93c..4e1e2eb 100644 --- a/test/simple_lru_tests.js +++ b/test/simple_lru_tests.js @@ -67,7 +67,7 @@ describe("BigCache Config",function(){ { should.equal(cache.get("hello"), undefined); done(); - }, 5); + }, 10); }) it("Should return timeout item", function(done){ @@ -77,7 +77,7 @@ describe("BigCache Config",function(){ { should.equal(cache.get("hello"), "world"); done(); - }, 5); + }, 10); }) }) From 34bc1b27004c1f4657310457239194a6fcb7bf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Palm=C3=A9r?= Date: Thu, 27 Oct 2016 16:16:35 +0200 Subject: [PATCH 3/5] cleanup --- lib/simple_lru.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/simple_lru.js b/lib/simple_lru.js index a2cbe04..9040c09 100644 --- a/lib/simple_lru.js +++ b/lib/simple_lru.js @@ -51,11 +51,10 @@ Cache.prototype.get = function(key,hit){ var cacheTimeValid = (cacheVal && cacheVal.time == undefined) || (cacheVal && Date.now() - cacheVal.time < this.maxTime); - if(cacheVal && hit && cacheTimeValid) { + if(cacheVal && hit && cacheTimeValid) this.hit(cacheVal) - } else { + else return undefined - } return cacheVal.value } From 23252f7ff7ff07c4bfc333295fdb9d630dacc9cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Palm=C3=A9r?= Date: Thu, 27 Oct 2016 17:04:30 +0200 Subject: [PATCH 4/5] asilvas suggestion --- lib/simple_lru.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/simple_lru.js b/lib/simple_lru.js index 9040c09..cd46158 100644 --- a/lib/simple_lru.js +++ b/lib/simple_lru.js @@ -45,13 +45,7 @@ Cache.prototype.get = function(key,hit){ */ hit = hit != undefined && hit != null ? hit : true; - /* - * Define if maxTime is used and if it's valid. - */ - var cacheTimeValid = (cacheVal && cacheVal.time == undefined) || - (cacheVal && Date.now() - cacheVal.time < this.maxTime); - - if(cacheVal && hit && cacheTimeValid) + if(cacheVal && hit && (!cacheVal.time || (Date.now() - cacheVal.time) < this.maxTime)) this.hit(cacheVal) else return undefined From a4c167aaf17734c7017afe3bd56d9a07e0b7c5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Palm=C3=A9r?= Date: Fri, 28 Oct 2016 10:31:04 +0200 Subject: [PATCH 5/5] set timestamp when you use Cache.set --- lib/simple_lru.js | 6 +++++- test/simple_lru_tests.js | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/simple_lru.js b/lib/simple_lru.js index cd46158..f47eaf7 100644 --- a/lib/simple_lru.js +++ b/lib/simple_lru.js @@ -64,7 +64,11 @@ Cache.prototype.set = function(key,val,hit){ if(actual){ actual.value = val - if(hit) this.hit(actual) + if(hit) + { + this.hit(actual) + actual.time = this.maxTime ? Date.now() : undefined + } }else{ var cacheVal if(this.size >= this.maxSize){ diff --git a/test/simple_lru_tests.js b/test/simple_lru_tests.js index 4e1e2eb..a5bd695 100644 --- a/test/simple_lru_tests.js +++ b/test/simple_lru_tests.js @@ -65,8 +65,8 @@ describe("BigCache Config",function(){ cache.set("hello", "world") setTimeout(function() { - should.equal(cache.get("hello"), undefined); - done(); + should.equal(cache.get("hello"), undefined); + done(); }, 10); }) @@ -75,9 +75,25 @@ describe("BigCache Config",function(){ cache.set("hello", "world") setTimeout(function() { - should.equal(cache.get("hello"), "world"); - done(); + should.equal(cache.get("hello"), "world") + done(); }, 10); }) + it("Should set timestamp when you use Cache.set", function(done){ + var cache = new SimpleCache({maxSize:1, maxTime:100}) + cache.set("hello", "world") + setTimeout(function() + { + should.equal(cache.get("hello"), "world") + cache.set("hello", "world again") + setTimeout(function() + { + should.equal(cache.get("hello"), "world again"); + done(); + }, 75); + }, 75); + }) + + })