HTML5 localStorage with timed expiry
For ajax or heavy computational stuff that could probably be done once a minute, rather than every time the functions/events are called.
Anything that supports localStorage. You could use a localStorage polyfill to use it with older browsers.
For brevity, the browsers that support localStorage are Firefox 3.5, Safari 4, IE8, Chrome 4+
Add cache.js
to your HTML, I've included a closure compressed cache.min.js
for use online.
When you're in the middle of doing some heavy ajax, you can store the response in cache.
Cache.set('a unique identifier', {foo: 'bar'});
Your key of 'a unique identifier' can now be returned using the get
method.
Cache.get('a unique identifier');
=> {foo: 'bar'}
It will only be available for 10000ms (the default)
Cache.get('will be null');
=> null
Of course, you can set the expiry on a per key basis
Cache.set('key', 'value', 5e4); // 50,000 Milliseconds
Or set a global expiry to be used for all caches
Cache.expiry = 5e4 // 50K ms
Pulling it all together, you might do something like this:
var cache = Cache.get("/url");
if(cache) return cache;
$.get("/url", function (response) {
Cache.set("/url", response);
// Do something awesome with your responseText
});
Often, you'll want to do something if the value is not cached. Pass a function to the get
function:
var result = Cache.get('empty key', function(key) {
return 'bacon';
});
// `result` is now 'bacon' since there was no stored value.
For example, you could execute an Ajax call if the local storage is empty. Here's the earlier example with a cleaner style:
return Cache.get("/url", function(key) {
return $.get(key, function (response) {
Cache.set(key, response);
return response;
});
});
- Report a bug
- To contribute, or send an idea wither github message me or fork the project.
- If you're changing functionality, please perouse the
test/
directory first. - You can run the tests on a Mac by running
rake test
, otherwise, just open suite.html in a browser
Cache.js uses a grunt based build system. Install node, grunt, cd to cache.js/, install via npm install
then run the build by running grunt
.
These contributors will be upheld in the record books as those of fine character and dignity. Eternal greatness ensues.