Skip to content

Commit

Permalink
Adds interface for lscache to store data in to different buckets. Inc…
Browse files Browse the repository at this point in the history
…s. README and test changes.
  • Loading branch information
mattpowell committed Apr 17, 2012
1 parent 926ca02 commit ea2780d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
14 changes: 14 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Removes a value from localStorage.
### lscache.flush
Removes all lscache items from localStorage without affecting other data.

* * *

### lscache.setBucket
Appends CACHE_PREFIX so lscache will partition data in to different buckets
#### Arguments
1. `bucket` (**string**)

Usage
-------
Expand Down Expand Up @@ -80,6 +86,14 @@ And then when you retrieve it, you will get it back as an object:
alert(lscache.get('data').name);
```

If you have multiple instances of lscache running on the same domain, you can partition data in a certain bucket via:
```js
lscache.set('response', '...', 2);
lscache.setBucket('lib');
lscache.set('path', '...', 2);
lscache.flush(); //only removes 'path' which was set in the lib bucket
```

For more live examples, play around with the demo here:
http://pamelafox.github.com/lscache/demo.html

Expand Down
30 changes: 23 additions & 7 deletions lscache.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var lscache = function() {

var cachedStorage;
var cachedJSON;
var cacheBucket = '';

// Determines if localStorage is supported in the browser;
// result is cached for better performance instead of being run each time.
Expand Down Expand Up @@ -94,17 +95,17 @@ var lscache = function() {
*/

function getItem(key) {
return localStorage.getItem(CACHE_PREFIX + key);
return localStorage.getItem(CACHE_PREFIX + cacheBucket + key);
}

function setItem(key, value) {
// Fix for iPad issue - sometimes throws QUOTA_EXCEEDED_ERR on setItem.
localStorage.removeItem(CACHE_PREFIX + key);
localStorage.setItem(CACHE_PREFIX + key, value);
localStorage.removeItem(CACHE_PREFIX + cacheBucket + key);
localStorage.setItem(CACHE_PREFIX + cacheBucket + key, value);
}

function removeItem(key) {
localStorage.removeItem(CACHE_PREFIX + key);
localStorage.removeItem(CACHE_PREFIX + cacheBucket + key);
}

return {
Expand Down Expand Up @@ -143,8 +144,8 @@ var lscache = function() {
for (var i = 0; i < localStorage.length; i++) {
storedKey = localStorage.key(i);

if (storedKey.indexOf(CACHE_PREFIX) === 0 && storedKey.indexOf(CACHE_SUFFIX) < 0) {
var mainKey = storedKey.substr(CACHE_PREFIX.length);
if (storedKey.indexOf(CACHE_PREFIX + cacheBucket) === 0 && storedKey.indexOf(CACHE_SUFFIX) < 0) {
var mainKey = storedKey.substr((CACHE_PREFIX + cacheBucket).length);
var exprKey = expirationKey(mainKey);
var expiration = getItem(exprKey);
if (expiration) {
Expand Down Expand Up @@ -258,10 +259,25 @@ var lscache = function() {
// Loop in reverse as removing items will change indices of tail
for (var i = localStorage.length-1; i >= 0 ; --i) {
var key = localStorage.key(i);
if (key.indexOf(CACHE_PREFIX) === 0) {
if (key.indexOf(CACHE_PREFIX + cacheBucket) === 0) {
localStorage.removeItem(key);
}
}
},

/**
* Appends CACHE_PREFIX so lscache will partition data in to different buckets.
* @param {string} bucket
*/
setBucket: function(bucket) {
cacheBucket = bucket;
},

/**
* Resets the string being appended to CACHE_PREFIX so lscache will use the default storage behavior.
*/
resetBucket: function() {
cacheBucket = '';
}
};
}();
37 changes: 36 additions & 1 deletion test.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@
equals(localStorage.getItem('outside-cache'), 'not part of lscache', 'We expect localStorage value to still persist');
});

test('Testing setBucket()', function() {
var key = 'thekey';
var value1 = 'awesome';
var value2 = 'awesomer';
var bucketName = 'BUCKETONE';

lscache.set(key, value1, 1);
lscache.setBucket(bucketName);
lscache.set(key, value2, 1);

equals(lscache.get(key), value2, 'We expect "' + value2 + '" to be returned for the current bucket: ' + bucketName);
lscache.flush();
equals(lscache.get(key), null, 'We expect "' + value2 + '" to be flushed for the current bucket');
lscache.resetBucket();
equals(lscache.get(key), value1, 'We expect "' + value1 + '", the non-bucket value, to persist');
});

test('Testing quota exceeding', function() {
var key = 'thekey';

Expand Down Expand Up @@ -117,7 +134,7 @@
asyncTest('Testing set() and get() with string and expiration', 1, function() {

var key = 'thekey';
var value = 'thevalue'
var value = 'thevalue';
var minutes = 1;
lscache.set(key, value, minutes);
setTimeout(function() {
Expand All @@ -126,6 +143,24 @@
}, 1000*60*minutes);
});

asyncTest('Testing set() and get() with string and expiration in a different bucket', 2, function() {

var key = 'thekey';
var value1 = 'thevalue1';
var value2 = 'thevalue2';
var minutes = 1;
var bucket = 'newbucket';
lscache.set(key, value1, minutes * 2);
lscache.setBucket(bucket);
lscache.set(key, value2, minutes);
setTimeout(function() {
equals(lscache.get(key), null, 'We expect value to be null for the bucket: ' + bucket);
lscache.resetBucket();
equals(lscache.get(key), value1, 'We expect value to be ' + value1 + ' for the base bucket.');
start();
}, 1000*60*minutes);
});

}

</script>
Expand Down

0 comments on commit ea2780d

Please sign in to comment.