A simple library that emulates memcache functions using HTML5 localStorage
This is a simple library that emulates
memcache functions using HTML5
localStorage, so that you can cache data on the client
and associate an expiration time with each piece of data. If the
localStorage limit (~5MB) is exceeded, it tries to create space by removing the items that are closest to expiring anyway. If
localStorage is not available at all in the browser, the library degrades by simply not caching and all cache requests return null.
The library exposes 5 methods:
Stores the value in localStorage. Expires after specified number of minutes.
Retrieves specified value from localStorage, if not expired.
string | Object : The stored value.
Removes a value from localStorage.
Removes all lscache items from localStorage without affecting other data.
Appends CACHE_PREFIX so lscache will partition data in to different buckets
The interface should be familiar to those of you who have used
memcache, and should be easy to understand for those of you who haven't.
For example, you can store a string for 2 minutes using
lscacheset'greeting' 'Hello World!' 2;
You can then retrieve that string with
You can remove that string from the cache entirely with
You can remove all items from the cache entirely with
You can remove only expired items from the cache entirely with
The library also takes care of serializing objects, so you can store more complex data:
lscacheset'data' 'name': 'Pamela' 'age': 26 2;
And then when you retrieve it, you will get it back as an object:
If you have multiple instances of lscache running on the same domain, you can partition data in a certain bucket via:
lscacheset'response' '...' 2;lscachesetBucket'lib';lscacheset'path' '...' 2;lscacheflush; //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
This library was originally developed with the use case of caching results of JSON API queries to speed up my webapps and give them better protection against flaky APIs. (More on that in this blog post)
For example, RageTube uses
lscache to fetch Youtube API results for 10 minutes:
var key = 'youtube:' + query;var json = lscachegetkey;if jsonprocessJSONjson;elsefetchJSONquery;// ..var searchUrl = '';var params ='v': '2' 'alt': 'jsonc' 'q': encodeURIComponentqueryJSONPgetsearchUrl params nullprocessJSONjson;lscachesetkey json 10;;
It does not have to be used for only expiration-based caching, however. It can also be used as just a wrapper for
localStorage, as it provides the benefit of handling JS object (de-)serialization.
For example, the QuizCards Chrome extensions use
to store the user statistics for each user bucket, and those stats are an array
var bucket1 = ;for var i = 0; i < CARDS_DATAlength; i++var datum = CARDS_DATAi;bucket1push'id': datumid 'lastAsked': 0;lscachesetLS_BUCKET + 1 bucket1;lscachesetLS_BUCKET + 2 ;lscachesetLS_BUCKET + 3 ;lscachesetLS_BUCKET + 4 ;lscachesetLS_BUCKET + 5 ;lscachesetLS_INIT 'true'
lscache library should work in all browsers where
localStorage is supported.
A list of those is here: