Cache for YUI
Note: this is not an official YUI project.
This is a local storage wrapper designed for use with YUI. It is designed to use several storage engines and will choose the best available depending on the browser being used. It differentiates itself from other local storage wrappers by offering TTL and cleanup for storage objects.
The following browsers are supported:
- Firefox 2&3 (globalStorage)
- Safari 3.2 (Flash SharedObject)
- Webkit nightly (localStorage)
- IE 6&7 (Flash SharedObject)
- IE 8 (localStorage) (Not Tested)
- Opera 9.6 (Flash SharedObject)
Download Cache for YUI version 0.1
Notes:
- All objects must be stored as strings and JSON stringify is called on anything you pass to set. You won’t be able to store instances of objects.
- Names must contain only alphanumeric characters, -, and _.
Instructions:
Construct the cache instance by passing in a settings object.
var cache = new YAHOO.extension.Cache({ prefix: 'yui-cache-',
flash_url: 'cache.swf',
auto_purge: true });
All calls made to the cache object should be wrapped in a run call. This is done to make sure calls are done after the Flash applet has loaded.
cache.run(function() {
cache.set( 'test', 'YUI cache test' );
});
var result = null;
cache.run(function() {
result = cache.get( 'test' );
});
You can set a TTL for the cache using a third parameter on the set call.
cache.run(function() {
cache.set( 'test', 'YUI cache test', new Date(2008,1,1) );
});
var result = null;
cache.run(function() {
//Should return null because key is expired
result = cache.get( 'test' );
});
By default, the script will automatically purge expired objects for the domain. This may make page load slower depending on how many stored objects you have. I haven’t done testing to see how large of an effect this might have. If you don’t want it to run automatically on load then pass auto_purge: false when creating the cache object. You can manually call cache.purgeExpired() when you want to run cleanup. Doing cleanup periodically is recommended because every local storage implementation has limited storage space per domain.