From 69d5edf6a94e26d59e5e085556fc0668af0f50dc Mon Sep 17 00:00:00 2001 From: meconlin Date: Sat, 19 Oct 2013 13:32:56 -0400 Subject: [PATCH 1/2] added 0 for forever ttl --- lscache.js | 14 ++++++++++++-- test.html | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) mode change 100644 => 100755 lscache.js mode change 100644 => 100755 test.html diff --git a/lscache.js b/lscache.js old mode 100644 new mode 100755 index ecd48f7..ac411fe --- a/lscache.js +++ b/lscache.js @@ -25,6 +25,9 @@ var lscache = function() { // Prefix for all lscache keys var CACHE_PREFIX = 'lscache-'; + // Default ttl if none specified + var DEFAULT_TIMEOUT = 60 * 1000 * 5 // 5 minutes + // Suffix for the key name on the expiration items in localStorage var CACHE_SUFFIX = '-cacheexpiration'; @@ -195,11 +198,18 @@ var lscache = function() { } // If a time is specified, store expiration info in localStorage - if (time) { + // If time is set to 0, make ttl forever (by not setting one) + // If time is not set, use DEFAULT_TIMEOUT + if (time && time != 0) { + console.log('if then time : ', time) setItem(expirationKey(key), (currentTime() + time).toString(EXPIRY_RADIX)); - } else { + } else if (time === 0){ // In case they previously set a time, remove that info from localStorage. + console.log('if then time 0 : ', time) removeItem(expirationKey(key)); + } else { + console.log('if then time : NONE ', time) + setItem(expirationKey(key), (currentTime() + DEFAULT_TIMEOUT).toString(EXPIRY_RADIX)); } }, diff --git a/test.html b/test.html old mode 100644 new mode 100755 index 033f9ae..651b3b2 --- a/test.html +++ b/test.html @@ -53,8 +53,28 @@ value = {'name': 'Pamela', 'age': 26}; lscache.set(key, value, 3); equals(lscache.get(key).name, value.name, 'We expect name to be ' + value.name); + + +}); + +test('Testing set with default ttl and no ttl', function() { + + var CACHE_SUFFIX = '-cacheexpiration'; + + // no ttl, should use default + key = 'defaultttlkey'; + value = 7 + lscache.set(key, value) + ok(lscache.get(key+CACHE_SUFFIX), 'Key has a time to live value'); + + // 0 ttl, should not store a ttl (forever ttl) + key = 'foreverttlkey'; + value = 99 + lscache.set(key, value, 0) + equals(lscache.get(key+CACHE_SUFFIX), undefined, 'Key does not have a time to live'); }); + test('Testing remove()', function() { var key = 'thekey'; lscache.set(key, 'bla', 2); From 2499326c5606b8ae35464761f89b744399414ba9 Mon Sep 17 00:00:00 2001 From: meconlin Date: Sat, 19 Oct 2013 13:59:16 -0400 Subject: [PATCH 2/2] remove logging --- lscache.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lscache.js b/lscache.js index ac411fe..5dd229e 100755 --- a/lscache.js +++ b/lscache.js @@ -201,14 +201,11 @@ var lscache = function() { // If time is set to 0, make ttl forever (by not setting one) // If time is not set, use DEFAULT_TIMEOUT if (time && time != 0) { - console.log('if then time : ', time) setItem(expirationKey(key), (currentTime() + time).toString(EXPIRY_RADIX)); } else if (time === 0){ // In case they previously set a time, remove that info from localStorage. - console.log('if then time 0 : ', time) removeItem(expirationKey(key)); } else { - console.log('if then time : NONE ', time) setItem(expirationKey(key), (currentTime() + DEFAULT_TIMEOUT).toString(EXPIRY_RADIX)); } },