Cacheable 1.0.1
An Utility Class (mixin) allowing the instances of a class implementing it to use a simple crossbrowser caching system.
Details
- Author
- Kevin Chapelier
- Current version
- 1.0.1
- GitHub
- DEEO/Cacheable
- Downloads
- 1651
- Category
- Utilities
- Tags
- Report
- GitHub Issues
How to use
if(console && console.log) alert = console.log;
var Example = new Class({
Implements: [Cacheable],
saveData: function()
{
this.setCache('index', 'value', 60); //duration of 60 sec.
this.setCache('otherindex', {
some: 'value 123',
someOther: 'value 456'
}); //unspecified duration => one hour
},
readData: function()
{
alert(this.getCache('index'));
alert(this.getCache('otherindex'));
}
});
var object = new Example();
object.readData(); // at first nothing here, reload the page
object.saveData();
object.readData();
History
1.0.1 (2010-10-24)
- Fix the Cookie Storage class.
- Much less pollution of the global namespace (all Storage classes moved to Cacheable.Storages).
- UserData is now implemented as a possible storage.
- A simple Hash is now implemented as a possible storage.
- You can now specify your own order of preference (see docs).
1.0.0 (2010-10-22)
- First release
Notes
- Feature detections are from the modernizr project (except for the userData 'detection').
- Currently use (sorted by the default order of preference) localStorage, globalStorage, sessionStorage, userData behavior and finally Cookie.
- Undocumented classes and methods are subject to changes in the next version.
- Never store critical data such as credentials in the cache.
Known Issues
- None.
Real World Exemple
var SomeTwitterClient = new Class({
Implements: [Cacheable],
initialize: function()
{
//defining a custom prefix (and duration) is a best practice
this.setCacheStorage('twitter', 60 * 20);
},
getTweets: function(screename)
{
var tweets = this.getCache(screename);
if(!tweets)
{
//make request to the twitter API and populate 'tweets'
this.setCache(screename, tweets);
}
return tweets;
}
});
var twidget = new SomeTwitterClient();
//send a request (do not if you reload the page after less than 20 minutes)
twidget.getTweets('mootools');
//do not send any request, use the cache
twidget.getTweets('mootools');
Discuss
A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.
Found a bug in this plugin? Please report it this repository's Github Issues.
blog comments powered by Disqus