Skip to content

Use default time if none set, use 0 to set no timeout (just like memcached) #20

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lscache.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -195,11 +198,15 @@ 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) {
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.
removeItem(expirationKey(key));
} else {
setItem(expirationKey(key), (currentTime() + DEFAULT_TIMEOUT).toString(EXPIRY_RADIX));
}
},

Expand Down
20 changes: 20 additions & 0 deletions test.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down