An Utility Class (mixin) allowing the instances of a class implementing it to use a simple crossbrowser caching system.
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();
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');
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