This class caches objects in the DOM
Mikkel Bergmann, http://www.pointful.com
| pax. | This class caches objects in the DOM |
| Functions | |
| pax. | Returns true if an object is cached, (false otherwise), and expires and removes obj as required. |
| pax. | Set an object in the cache. |
| pax. | Retreives an object from the cache, returns null if obj is not cached |
| pax. | Deletes an object from the cache, returns false if obj is not cached |
pax.cache.isCached = function( key )
Returns true if an object is cached, (false otherwise), and expires and removes obj as required.
| key | The key used when the object was cached. |
| Boolean | true if object is cached, (false otherwise), and expires and removes obj as required. |
<div id='pax.cache.isCached.example1'></div>
[:.
pax.cache.set( 'myObjKey', { box: 'Shoes' } );
if( pax.cache.isCached( 'myObjKey' ) ) {
var contents = "";
for( var item in pax.cache.get('myObjKey') ) {
contents += item + " with " + pax.cache.get('myObjKey')[item] + "\n";
}
pax.$('pax.cache.isCached.example1').innerHTML = "my object is a: " + contents;
}
:]isCached returns true if anything has been cached under that key, and it has not expired. If it has expired, the object is removed from the cache, and false will be returned.
<div id='pax.cache.isCached.example2'></div>
[:.
var myFunc = function() {
pax.$('pax.cache.isCached.example2').innerHTML = "This is from a cached function!";
};
pax.cache.set( 'myObjKey', myFunc );
if( pax.cache.isCached( 'myObjKey' ) ) {
pax.cache.get('myObjKey')();
};
:]This example populates the DIV, using a cached function.
pax.cache.set = function( key, value, expire )
Set an object in the cache.
| key | The key to use for caching the object. |
| value | The object or value to be cached. |
| expire | Time in seconds for the value or object to be valid. Set to 0 to never expire the object (or omit expire) |
[:_
var myFruits = {
'apples': [
'Granny Smith',
'Red delicious'
],
'oranges': [
'Valencia',
'Blood'
],
'pears': 'green'
};
pax.cache.set( 'myObjKey', myFruits, 0 );
var cacheObj = pax.util.toString( pax.cache.get('myObjKey') );
:]
The fruit object contains: [:= cacheObj :]This will cache the myFruits object “forever”.
pax.cache.get = function( key )
Retreives an object from the cache, returns null if obj is not cached
| key | The key to use for caching the object. |
[:_
var myFruits = {
'apples': [
'Granny Smith',
'Red delicious'
],
'oranges': [
'Valencia',
'Blood'
],
'pears': 'green'
};
pax.cache.set( 'myObjKey', myFruits, 0 );
var myObj = pax.cache.get('myObjKey');
var appleList = pax.util.toString( myObj.apples );
:]
The fruit object contains the following apples: [:= appleList :]This will populate myFruits with object cached under ‘myObjKey’, or null if the key was not found.
pax.cache.remove = function( key )
Deletes an object from the cache, returns false if obj is not cached
| key | The key to use for caching the object. |
[:_
var myFruits = {
'apples': [
'Granny Smith',
'Red delicious'
],
'oranges': [
'Valencia',
'Blood'
],
'pears': 'green'
};
pax.cache.set( 'myObjKey', myFruits, 0 );
var objContents = pax.util.toString( pax.cache.get('myObjKey') );
:]
The fruit object contains the following: [:= objContents :]
[:
pax.cache.remove( 'myObjKey' );
var objContents2 = pax.util.toString( pax.cache.get('myObjKey') );
:]
<hr>
After removal, the fruit object contains the following: [:= objContents2 :]This will remove the object cached under ‘myObjKey’, or run the error function otherwise.
Returns true if an object is cached, (false otherwise), and expires and removes obj as required.
pax.cache.isCached = function( key )
Set an object in the cache.
pax.cache.set = function( key, value, expire )
Retreives an object from the cache, returns null if obj is not cached
pax.cache.get = function( key )
Deletes an object from the cache, returns false if obj is not cached
pax.cache.remove = function( key )