Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Cache Class Reference

Provides static/global access to a zend cache manager instance as well as easy access to load/save/etc. More...

List of all members.

Static Public Member Functions

static canCache ($template= 'default')
 Check if the given template is registered with the cache manager.
static clean ($mode= 'all', $tags=array(), $template=null)
 Clean cache entries.
static getCache ($template= 'default')
 A convienence passthrough to the getCache method on the cache manager.
static getManager ()
 Get the configured cache manager instance.
static hasManager ()
 Check if a cache manager is configured.
static load ($id, $template= 'default')
 Fetch data from the cache by id.
static remove ($id, $template= 'default')
 Remove a entry from the cache.
static save ($data, $id, $tags=array(), $lifetime=false, $priority=8, $template= 'default')
 Save some data in the cache.
static setLoggingEnabled ($enabled=true)
 Enable/disable logging when unable to read/write the cache.
static setManager (Zend_Cache_Manager $manager=null)
 Set the cache manager to use.

Static Protected Attributes

static $_loggingEnabled = true
static $_manager = null

Detailed Description

Provides static/global access to a zend cache manager instance as well as easy access to load/save/etc.

against named cache manager templates.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

static P4Cms_Cache::canCache ( template = 'default') [static]

Check if the given template is registered with the cache manager.

Returns false template is invalid, or no manager is set.

Parameters:
string$templateoptional - the named cache configuration to use.
Returns:
bool true if template and manager are valid.
    {
        // false if no manager set.
        if (!static::hasManager()) {
            if (static::$_loggingEnabled) {
                P4Cms_Log::log(
                    "Cannot read/write cache. No cache manager has been set.",
                    P4Cms_Log::WARN
                );
            }
            return false;
        }

        // false if named template is invalid.
        $manager = static::getManager();
        if (!$manager->hasCache($template)) {
            if (static::$_loggingEnabled) {
                P4Cms_Log::log(
                    "Cannot read/write cache. Cache template $template is not configured.",
                    P4Cms_Log::WARN
                );
            }
            return false;
        }

        // good to go!
        return true;
    }
static P4Cms_Cache::clean ( mode = 'all',
tags = array(),
template = null 
) [static]

Clean cache entries.

If a template is specified only it will be affected otherwise all templates are cleared.

Available modes are : 'all' (default) => remove all cache entries ($tags is not used) 'old' => remove too old cache entries ($tags is not used) 'matchingTag' => remove cache entries matching all given tags ($tags can be an array of strings or a single string) 'notMatchingTag' => remove cache entries not matching one of the given tags ($tags can be an array of strings or a single string) 'matchingAnyTag' => remove cache entries matching any given tags ($tags can be an array of strings or a single string)

Parameters:
string$modeoptional - the clearing mode, see above
array | string$tagsoptional - tags to use as per clearing mode
string | null$templateoptional - template to limit to, all if null
Returns:
boolean True if ok
    {
        if (!static::hasManager()) {
            return false;
        }

        // normalize tags
        if (is_string($tags)) {
            $tags = array($tags);
        }

        // determine the templates we are cleaning
        $templates = static::getManager()->getCaches();
        if ($template != null) {
            if (!static::canCache($template)) {
                return false;
            }

            $templates = array(static::getCache($template));
        }

        // clean template(s) and track success
        $result = true;
        foreach ($templates as $template) {
            try {
                $result = $template->clean($mode, $tags) && $result;
            } catch (Zend_Cache_Exception $e) {
                $result = false;
            }
        }

        return $result;
    }
static P4Cms_Cache::getCache ( template = 'default') [static]

A convienence passthrough to the getCache method on the cache manager.

Parameters:
string$templateThe template name to pass to manager's getCache
Returns:
Zend_Cache_Core|false Returns the result of manager getCache or false if no manager
    {
        if (!static::canCache($template)) {
            return false;
        }

        return static::getManager()->getCache($template);
    }
static P4Cms_Cache::getManager ( ) [static]

Get the configured cache manager instance.

Returns:
Zend_Cache_Manager the configured zend cache manager instance.
Exceptions:
P4Cms_Cache_Exceptionif no cache manager has been set.
    {
        if (!static::hasManager()) {
            throw new P4Cms_Cache_Exception(
                "Cannot get cache manager. No manager is set."
            );
        }

        return static::$_manager;
    }
static P4Cms_Cache::hasManager ( ) [static]

Check if a cache manager is configured.

Returns:
bool true if a manager instance is set.
    {
        return static::$_manager instanceof Zend_Cache_Manager;
    }
static P4Cms_Cache::load ( id,
template = 'default' 
) [static]

Fetch data from the cache by id.

Returns false if no such id in the cache or if cache manager or named template are not configured.

Parameters:
string$idthe id of the cache entry.
string$templateoptional - the cache configuration to use defaults to 'default'.
Returns:
mixed|false the cached data or false if no such entry or cache manager/template not configured.
    {
        // ensure manager and template are good.
        if (!static::canCache($template)) {
            return false;
        }

        $cache = static::getManager()->getCache($template);
        return $cache->load($id);
    }
static P4Cms_Cache::remove ( id,
template = 'default' 
) [static]

Remove a entry from the cache.

Parameters:
string$idid of entry to remove.
string$templateoptional - the named cache configuration to use.
Returns:
bool true if entry removed; false otherwise.
    {
        // ensure manager and template are good.
        if (!static::canCache($template)) {
            return false;
        }

        $cache = static::getManager()->getCache($template);
        return $cache->remove($id);
    }
static P4Cms_Cache::save ( data,
id,
tags = array(),
lifetime = false,
priority = 8,
template = 'default' 
) [static]

Save some data in the cache.

Returns false if cache manager or template are not configured.

Parameters:
mixed$datadata to put in cache (must be string if automatic serialization is off)
string$idcache id (if not set, the last cache id will be used)
array$tagscache tags
int$lifetimea specific lifetime for this cache record (null => infinite lifetime)
int$priorityinteger between 0 (very low priority) and 10 (maximum priority) used by some particular backends
string$templateoptional - the named cache configuration to use.
Returns:
bool true if save successful, false otherwise.
    {
        // ensure manager and template are good.
        if (!static::canCache($template)) {
            return false;
        }

        $cache = static::getManager()->getCache($template);
        return $cache->save($data, $id, $tags, $lifetime, $priority);
    }
static P4Cms_Cache::setLoggingEnabled ( enabled = true) [static]

Enable/disable logging when unable to read/write the cache.

Parameters:
bool$enabledtrue to enable logging, false otherwise.
    {
        static::$_loggingEnabled = (bool) $enabled;
    }
static P4Cms_Cache::setManager ( Zend_Cache_Manager $  manager = null) [static]

Set the cache manager to use.

Parameters:
Zend_Cache_Manager$managerthe cache manager instance to use.
    {
        static::$_manager = $manager;
    }

Member Data Documentation

P4Cms_Cache::$_loggingEnabled = true [static, protected]
P4Cms_Cache::$_manager = null [static, protected]

The documentation for this class was generated from the following file: