Skip to content

Commit

Permalink
Merge GH-12
Browse files Browse the repository at this point in the history
I implemented a change to your catch block in `set`. It now tracks how much space is being freed and stops once enough has been freed that the new item should fit.

I also reflowed your `get` method so that it doesn't require an inner function and doesn't call `getItem` multiple times for the same values.

Also I parameterized two values:

- the expiration unit size so that by changing the constant it can be something other than minutes
- the base of the expiration value so that doesn't need to be base-10. e.g., base-36 is a couple digits more compact per each entry

And finally I made some very minor tweaks so that it passes [JSHint](http://jshint.com) checking. I tried to stay respectful of your code style.

All original unit tests pass but I didn't add any additional.

+ Fixes GH-12

* patch-12:
  Fixing indentation
  Adding flush to readme
  adding method to flush all lscache items without affecting rest of localStorage
  removing my qunit to resolve conflict
  clear localStorage in setup/teardown
  Fixing a couple typos and removing dead code block
  adding missing qunit
  clear localStorage before tests
  minor tweaks
  removing items until item to be inserted fits
  reflow get for less repetition
  parameterize for tuning
  jshint.com recommendations
  • Loading branch information
pamelafox committed Mar 19, 2012
2 parents 19eceee + 1ac5db9 commit 926ca02
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
13 changes: 12 additions & 1 deletion README.markdown
Expand Up @@ -6,7 +6,7 @@ and associate an expiration time with each piece of data. If the `localStorage`
Methods
-------

The library exposes 3 methods: `set()`, `get()`, and `remove()`.
The library exposes 4 methods: `set()`, `get()`, `remove()`, and `flush()`.

* * *

Expand All @@ -33,6 +33,11 @@ Removes a value from localStorage.
#### Arguments
1. `key` (**string**)

* * *

### lscache.flush
Removes all lscache items from localStorage without affecting other data.


Usage
-------
Expand All @@ -57,6 +62,12 @@ You can remove that string from the cache entirely with `lscache.remove()`:
lscache.remove('greeting');
```

You can remove all items from the cache entirely with `lscache.flush()`:

```js
lscache.flush();
```

The library also takes care of serializing objects, so you can store more complex data:

```js
Expand Down
32 changes: 25 additions & 7 deletions lscache.js
Expand Up @@ -28,12 +28,15 @@ var lscache = function() {
// Suffix for the key name on the expiration items in localStorage
var CACHE_SUFFIX = '-cacheexpiration';

// expiration date base (store as Base-36 for space savings)
var EXPIRY_BASE = 10;
// expiration date radix (set to Base-36 for most space savings)
var EXPIRY_RADIX = 10;

// time resolution in minutes
var EXPIRY_UNITS = 60 * 1000;

// ECMAScript max Date (epoch + 1e8 days)
var MAX_DATE = Math.floor(8.64e15/EXPIRY_UNITS);

var cachedStorage;
var cachedJSON;

Expand All @@ -58,7 +61,7 @@ var lscache = function() {
cachedStorage = false;
}
return cachedStorage;
}
}

// Determines if native JSON (de-)serialization is supported in the browser.
function supportsJSON() {
Expand Down Expand Up @@ -145,10 +148,10 @@ var lscache = function() {
var exprKey = expirationKey(mainKey);
var expiration = getItem(exprKey);
if (expiration) {
expiration = parseInt(expiration, EXPIRY_BASE);
expiration = parseInt(expiration, EXPIRY_RADIX);
} else {
// TODO: Store date added for non-expiring items for smarter removal
expiration = 99999999999;
expiration = MAX_DATE;
}
storedKeys.push({
key: mainKey,
Expand Down Expand Up @@ -181,7 +184,7 @@ var lscache = function() {

// If a time is specified, store expiration info in localStorage
if (time) {
setItem(expirationKey(key), (currentTime() + time).toString(EXPIRY_BASE));
setItem(expirationKey(key), (currentTime() + time).toString(EXPIRY_RADIX));
} else {
// In case they previously set a time, remove that info from localStorage.
removeItem(expirationKey(key));
Expand All @@ -201,7 +204,7 @@ var lscache = function() {
var expr = getItem(exprKey);

if (expr) {
var expirationTime = parseInt(expr, EXPIRY_BASE);
var expirationTime = parseInt(expr, EXPIRY_RADIX);

// Check if we should actually kick item out of storage
if (currentTime() >= expirationTime) {
Expand Down Expand Up @@ -244,6 +247,21 @@ var lscache = function() {
*/
supported: function() {
return supportsStorage();
},

/**
* Flushes all lscache items and expiry markers without affecting rest of localStorage
*/
flush: function() {
if (!supportsStorage()) return;

// 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) {
localStorage.removeItem(key);
}
}
}
};
}();
9 changes: 9 additions & 0 deletions test.html
Expand Up @@ -59,6 +59,15 @@
equals(lscache.get(key), null, 'We expect value to be null');
});

test('Testing flush()', function() {
localStorage.setItem('outside-cache', 'not part of lscache');
var key = 'thekey';
lscache.set(key, 'bla', 100);
lscache.flush();
equals(lscache.get(key), null, 'We expect flushed value to be null');
equals(localStorage.getItem('outside-cache'), 'not part of lscache', 'We expect localStorage value to still persist');
});

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

Expand Down

0 comments on commit 926ca02

Please sign in to comment.