blogccasion

Persistent Storage JavaScript Library

Persistent Storage JavaScript Library

The last few weeks I have been working on a gadget framework that allows for AdWords API data to be accessed from within the iGoogle gadget infrastructure. I was looking for ways to store data persistently across sessions, and for obvious reasons thought of using Google Gears. A shortcoming of Gears is that it is not as widespread as it could be. Then I stumbled upon a blog entry in Ajaxian about PersistJS, a JavaScript library that promised persistent storage independent from the storage mechanism. Basically a high-level browser storage API. Having read through the post, I went to Paul Duncan's web page and downloaded the PersistJS code.

The library is ridiculously easy to use while still being perfectly elegant. It detects the available storage possibilities and goes for the first available one. Usage is as simple as (copied from the page above):

// create a new client-side persistent data store
var store = new Persist.Store('My Data Store');

// pretend data
var data = "pretend this is really long data that won't fit in a cookie";

// save data in store
store.set('saved_data', data);

// get data back from store, and prompt user with it
store.get('saved_data', function(ok, val) {
if (ok)
alert('saved data = ' + val);
});

// remove data from store
store.remove('saved_data');


I love the way this falls back to the greatest common divisor, namely cookies. And probably the best thing about the library is that it does not detect browsers, but browser capabilities. This way it defaults to native implementations like globalStorage if these get implemented in later/other browser(s) versions. Kudos to Paul Duncan and thanks for sharing this!