pax.cache

This class caches objects in the DOM

Author

Mikkel Bergmann, http://www.pointful.com

Summary
pax.cacheThis class caches objects in the DOM
Functions
pax.cache.isCachedReturns true if an object is cached, (false otherwise), and expires and removes obj as required.
pax.cache.setSet an object in the cache.
pax.cache.getRetreives an object from the cache, returns null if obj is not cached
pax.cache.removeDeletes an object from the cache, returns false if obj is not cached

Functions

pax.cache.isCached

pax.cache.isCached = function(key)

Returns true if an object is cached, (false otherwise), and expires and removes obj as required.

Parameters

keyThe key used when the object was cached.

Returns

Booleantrue if object is cached, (false otherwise), and expires and removes obj as required.

Example

<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.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.

Example

<div id='pax.cache.isCached.example2'></div>
[:.
    var myFunc = function() {
        $('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

pax.cache.set = function(key,
value,
expire)

Set an object in the cache.

Parameters

keyThe key to use for caching the object.
valueThe object or value to be cached.
expireTime in seconds for the value or object to be valid.  Set to 0 to never expire the object (or omit expire)

Example

[:_
    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

pax.cache.get = function(key)

Retreives an object from the cache, returns null if obj is not cached

Parameters

keyThe key to use for caching the object.

Example

[:_
    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

pax.cache.remove = function(key)

Deletes an object from the cache, returns false if obj is not cached

Parameters

keyThe key to use for caching the object.

Example

[:_
    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.

pax.cache.isCached = function(key)
Returns true if an object is cached, (false otherwise), and expires and removes obj as required.
pax.cache.set = function(key,
value,
expire)
Set an object in the cache.
pax.cache.get = function(key)
Retreives an object from the cache, returns null if obj is not cached
pax.cache.remove = function(key)
Deletes an object from the cache, returns false if obj is not cached
Close