mootools
MooTools Cache is a new proof of concept plugin that I've created so that developers may easily "cache" values. Cache allows you to set, get, and refresh values. Cache also features a system of reading and writing from Cookies so that the cache remains persistent throughout different pages of your website.
MooTools Cache是我创建的新的概念证明插件,以便开发人员可以轻松地“缓存”值。 缓存允许您设置,获取和刷新值。 缓存还具有从Cookies读取和写入的系统,因此缓存在您网站的不同页面上保持持久性。
MooTools JavaScript (The MooTools JavaScript)
var Cache = new Class({
options: {
autoRefresh: true,
cookieName: 'cache-keys',
cookieOptions: {},
cookiePrefix: 'cache-',
duration: 60, //seconds
sep: ':://::'
},
Implements: [Options],
initialize: function(options) {
this.setOptions(options);
this.data = {};
},
set: function(key,value,duration,fn) {
this.data[key] = {
value: value,
expires: this.calculateDuration(duration || this.options.duration),
duration: (duration || this.options.duration)
};
if(fn) this.data[key]['fn'] = fn;
return this;
},
get: function(key) {
return this.isFresh(key) ? this.data[key]['value'] : (this.options.autoRefresh && this.data[key]['fn'] ? this.refresh(key) : false);
},
isFresh: function(key) {
return this.data[key]['expires'] > $time();
},
refresh: function(key) {
if(!this.data[key] || !this.data[key]['fn']) return false;
this.data[key]['value'] = this.data[key]['fn']();
this.data[key]['expires'] = this.calculateDuration(this.data[key]['duration']);
return this.data[key];
},
load: function(hash,duration) {
for (var key in hash){
if (hash.hasOwnProperty(key)) {
this.data[key] = {
value: hash[key],
expires: this.calculateDuration(duration || this.options.duration),
duration: (duration || this.options.duration)
};
}
}
},
loadCookie: function() {
var cookie = Cookie.read(this.options.cookieName);
if(cookie) {
var keys = cookie.split(this.options.sep);
keys.each(function(key) {
var val = Cookie.read(this.options.cookiePrefix + '' + key);
if($defined(val)) {
var split = val.split(this.options.sep);
this.data[key] = {
value: split[0],
expires: split[1],
duration: split[2]
};
if(split[3]) this.data[key]['fn'] = split[3];
}
},this);
}
},
save: function() {
//write individual items
var keys = [];
for (var key in this.data){
if (this.data.hasOwnProperty(key)) {
Cookie.write(this.options.cookiePrefix + key,this.data[key]['value'] + this.options.sep + this.data[key]['expires'] + this.options.sep + this.data[key]['duration'] + (this.data[key]['fn'] && $type(this.data[key]['fn']) == 'string' ? this.data[key]['fn'] : ''),this.options.cookieOptions);
keys.push(key);
}
}
cookies = keys.join(this.options.sep);
//write grouper
Cookie.write(this.options.cookieName,cookies);
return this;
},
clear: function(key) {
if(key) {
this.data[key] = '';
}
else {
this.data = {};
}
return this;
},
/** PRIVATE **/
calculateDuration: function(seconds) {
return seconds * 1000 + $time();
}
});
Options for Cache include:
缓存的选项包括:
autoRefresh: (defaults to true) Determines whether a value should auto-refresh if the value is stale.
autoRefresh :( 默认为true)确定值是否陈旧时是否应自动刷新。
cookieName: (defaults to 'cache-keys') The name of the "index" cookie that the class will use to store variable keys.
cookieName :( 默认为'cache-keys')类将用于存储变量键的“索引” cookie的名称。
cookieOptions: (defaults to {}) The options to be given to Cookies written by the class.
cookieOptions :( 默认为{})为类编写的Cookies提供的选项。
cookiePrefix: (defaults to 'cache-') The prefix given to cookie keys. Useful to prevent overwriting of other cookies.
cookiePrefix :( 默认为'cache-')给cookie键的前缀。 有助于防止覆盖其他cookie。
duration: (defaults to 60) The default number of seconds to cache a given value. The duration may also be set on a per-key basis.
duration :( 默认为60)缓存给定值的默认秒数。 持续时间也可以基于每个键设置。
sep: (defaults to ':://::') The separater to split the stored cookie information (value, expires, duration, fn).
sep :( 默认为':: // ::')分隔符,用于分隔存储的cookie信息(值,到期时间,持续时间,fn)。
Public methods for Cache include:
缓存的公共方法包括:
set(key, value, duration *optional*, fn *optional*): The key and value are simple. The duration argument overrides the default Class duration. The function argument allows you to assign a function to execute to "refresh" a value when it expires.
set(键,值,持续时间*可选*,fn *可选*) :键和值很简单。 duration参数将覆盖默认的Class持续时间。 函数参数允许您分配一个函数以在值过期时“刷新”值。
get(key): Returns the value for the given key. If autoRefresh is on and fn was passed in initialy, returns the new value.
get(key) :返回给定键的值。 如果autoRefresh处于打开状态,并且fn是在初始时传递的,则返回新值。
isFresh(key): Returns true or false based upon whether or not the value is fresh.
isFresh(key) :根据值是否新鲜返回真或假。
refresh(key): Refreshes the value of a key.
refresh(key) :刷新键的值。
load(hash,duration): Takes a provided Object, recurses through it and adds the key->values to the cache.
load(hash,duration) :获取提供的对象,通过它进行递归,然后将key-> values添加到缓存中。
loadCookie(): Loads the key->values from cookies.
loadCookie() :从cookie加载key-> values。
save(): Saves the cache to cookies.
save() :将缓存保存到cookie。
clear(key *optional*): Clears a given key from the cache or the entire cache.
clear(key * optional *) :从缓存或整个缓存中清除给定的键。
MooTools缓存使用示例 (Sample MooTools Cache Usage)
Creating an instance is easy:
创建实例很容易:
var cache = new Cache({
duration: 3600 //one hour
});
Now we can add and retrieve keys and values to the cache:
现在我们可以向缓存添加和检索键和值:
//setting
cache.set('name','David'); //explicit
cache.set(age,$('age').get('value')); //from form
cache.set('time',getAJAXInfo(),1200,'getAJAXInfo'); //refresh function
//getting
if(cache.get('name')) {
alert('Welcome back: ' + cache.get('name') + '!');
}
Before we leave the page, save cache to cookies:
在离开页面之前,将缓存保存到cookie:
cache.save();
...and load values when a page loads:
...并在页面加载时加载值:
cache.loadCookie();
Have any ideas for the class? Let me know!
对这个班有什么想法吗? 让我知道!
mootools