YUI3:cache

Cache 工具为存储名值对到本地JavaScript内存提供了一个基本的缓存管理工具。作为Plugin的一个子类,它无缝地与其他组件结合(例如DataSource)。

升级说明

3.1.1以及之前的版本,使用cache时,要有Y.Cache变为Y.Plugin.Cache.

使用Cache工具

基本的caching

基本的caching允许你存储在本地JavaScript内存中频繁使用的数据。这种情况下允许你取得从服务器传送过来的响应数据,存储到本地,以消除下一次的重复请求,这样能获得很好的性能,减轻服务器负载。

使用以下属性来配置你的Cache实例。

属性默认值描述
max0cache能存储的最大数据条数。默认情况下,cache是关闭的,设置大于0的值来打开cache
sizen/a只读。返回当前存储在cache中的条数。
entriesn/a只读。返回当前存储在cache中的条目数组。
expires0默认下,截止时间是关闭的。为了是截至时间有效,可以设置相对值也可以设置绝对值。
uniqueKeysfalse

When calling add() with an entry, checks to see if the key is already stored in the cache. Enforcing unique keys requires iterating through all stored entries, so setting this attribute to false is more performant. Note: If expiration is enabled, you should probably set uniqueKeys to false to avoid problems if data is cached multiple times with conflicting expirations.

个人觉得这里可能有错误:最后一句话Note: If expiration is enabled, you should probably set uniqueKeys to false to avoid problems if data is cached multiple times with conflicting expirations.

false 应该是true吧。。。。

// 在构造函数中配置cache的最大值
var myCache = new Y.Cache({max:5});

// 在运行时设置最大值
myCache.set("max", 10);

 用add()方法Cache名值对:

// Add entries to the Cache
myCache.add("key1", "value1");

  用retrieve()方法获得缓存的条目。如果没有与给定的key对应的条目,将返回null。存储的条目包含的属性有:request,response,cached,expires.

 

// Retrieve a cached entry
var cachedEntry = cache.retrieve("key1");

 默认情况下,存储的条目包含复制的keys:如果你增加一个"foo"请求,值为"bar",然后增加另一个"foo"请求,值为"bat"。cache将包含两个条目。用"foo"请求获得一个条目将仅仅取得最新的值。然而,设置uniqueKeys为true将验证存储在cache中的key是不是唯一的。

// Enforce unique keys in the constructor
var myCache = new Y.Cache({max:5, uniqueKeys:true});

// Enforce unique keys at runtime
myCache.set("uniqueKeys", true);

 一个cache可以用flush()方法来清空所有内容。

// Flush the cache
myCache.flush();

 

Offline caching

这个CacheOffline cache通过在HTML5 localStorage object支持的浏览器上存储数据,扩展了基本的caching功能,所以即使在浏览器离线情况下,通过存储到浏览器会话上,数据依然有效。这这种情况下啊,如果HTML5 localStorage无效的(例如IE6 和IE7),将同基本的caching一样。

var myCache = new Y.CacheOffline();

 

ATTRIBUTEDEFAULTDESCRIPTION
sandbox"default"A unique identifier used to sandbox each cache instance's entries from other entries. This string persists across browser sessions so take care that it is not dynamically generated.
expires86400000By default, data will expire one day after it is cached. This Attribute accepts a Data value, or a number of milliseconds.
maxnullRead-only. This Attribute is disabled for CacheOffline — there is no notion of capping the number of entries in a cache. Each browser implements a maximum localStorage byte size.
uniqueKeystrueRead-only. This Attribute is disabled for CacheOffline. All stored keys are unique.

作为一个plugin的cache

在一个宿主对象上使用plug(Y.Plugin.Cache)方法来打开caching功能。Y.Plugin.Cache类在"cache-plugin"子模块下是有效的。

// Define a max value to enable plugging.
myWidget.plug(Y.Plugin.Cache, {max:3});
myWidget.cache.add("key", "value");

 Cache plugin也可以接受一个cache值,来是你能够指明任何一个Cache 工具。

// Define a max value to enable plugging.
myWidget.plug(Y.Plugin.Cache, {cache:Y.CacheOffline});

 The Y.Plugin.DataSourceCache plugin enables seamless caching of DataSource responses.

// Use a basic cache
myDataSource.plug(Y.Plugin.DataSourceCache, {
    cache: Y.Cache, // this is the default, this line is not needed
    max: 100
});

// Use the "cache" configuration property to enable offline caching
myDataSource.plug(Y.Plugin.DataSourceCache, {
    cache: Y.CacheOffline,
    sandbox: "my3HrCache",
    expires: 10800000 // 3 hours
});

 一旦DataSourceCache plugin是有效的,它处理caching和获取值,不需要额外的代码。而且,所有的Cache实例的方法和属性,在主机的cache命名空间中都是有效的。

// Flush myDataSource's cache.
myDataSource.cache.flush();

 

事件

事件 什么时候 PROPERTIES AVAILABLE ON THE EVENT FACADE PASSED TO HANDLER add Entry is added to the cache. entry The cached entry. request Entry is requested from the cache. request The request value. retrieve Entry is retrieved from the cache. entry The retrieved entry. flush Cache is flushed. none error CacheOffline only. Fired when an entry could not be added, most likely due to exceeding the browser quota for the localStorage object. error The error object.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值