2010-04-12 07:04:05 +02:00
|
|
|
<?php
|
2013-05-12 02:39:37 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
/**
|
2013-05-12 02:39:37 +02:00
|
|
|
* SS_Cache provides a bunch of static functions wrapping the Zend_Cache system
|
|
|
|
* in something a little more easy to use with the SilverStripe config system.
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* A Zend_Cache has both a frontend (determines how to get the value to cache,
|
|
|
|
* and how to serialize it for storage) and a backend (handles the actual
|
|
|
|
* storage).
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* Rather than require library code to specify the backend directly, cache
|
|
|
|
* consumers provide a name for the cache backend they want. The end developer
|
|
|
|
* can then specify which backend to use for each name in their project's
|
|
|
|
* _config.php. They can also use 'all' to provide a backend for all named
|
|
|
|
* caches.
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* End developers provide a set of named backends, then pick the specific
|
|
|
|
* backend for each named cache. There is a default File cache set up as the
|
|
|
|
* 'default' named backend, which is assigned to 'all' named caches.
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* <h2>Using a cache</h2>
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* <code>
|
|
|
|
* // foo is any name (try to be specific), and is used to get configuration
|
|
|
|
* // & storage info
|
|
|
|
* $cache = SS_Cache::factory('foo');
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
|
|
|
* if (!($result = $cache->load($cachekey))) {
|
|
|
|
* $result = caluate some how;
|
|
|
|
* $cache->save($result);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* return $result;
|
2013-05-12 02:39:37 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Normally there's no need to remove things from the cache - the cache
|
|
|
|
* backends clear out entries based on age & maximum allocated storage. If you
|
|
|
|
* include the version of the object in the cache key, even object changes
|
|
|
|
* don't need any invalidation.
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* <h2>Disabling cache in dev mode</h2>
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* <code>
|
|
|
|
* // _config.php
|
|
|
|
* if (Director::isDev()) {
|
|
|
|
* SS_Cache::set_cache_lifetime('any', -1, 100);
|
|
|
|
* //
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* <h2>Using memcached as a store</h2>
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* // _config.php
|
|
|
|
* SS_Cache::add_backend(
|
|
|
|
* 'primary_memcached',
|
|
|
|
* 'Memcached',
|
|
|
|
* array(
|
|
|
|
* 'host' => 'localhost',
|
|
|
|
* 'port' => 11211,
|
|
|
|
* 'persistent' => true,
|
|
|
|
* 'weight' => 1,
|
|
|
|
* 'timeout' => 5,
|
|
|
|
* 'retry_interval' => 15,
|
|
|
|
* 'status' => true,
|
|
|
|
* 'failure_callback' => ''
|
|
|
|
* )
|
2010-04-12 07:04:05 +02:00
|
|
|
* );
|
|
|
|
*
|
2010-04-13 03:48:06 +02:00
|
|
|
* SS_Cache::pick_backend('primary_memcached', 'any', 10);
|
2013-05-12 02:39:37 +02:00
|
|
|
*
|
|
|
|
* // Aggregate needs a backend with tag support, which memcached doesn't
|
|
|
|
* // provide
|
2012-09-26 23:34:00 +02:00
|
|
|
* SS_Cache::pick_backend('default', 'aggregate', 20);
|
2010-12-05 20:46:21 +01:00
|
|
|
* </code>
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* <h2>Using APC as a store</h2>
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* <code>
|
2010-12-05 09:16:59 +01:00
|
|
|
* SS_Cache::add_backend('two-level', 'TwoLevels', array(
|
2010-04-12 07:04:05 +02:00
|
|
|
* 'slow_backend' => 'File',
|
|
|
|
* 'fast_backend' => 'Apc',
|
2013-05-12 02:39:37 +02:00
|
|
|
* 'slow_backend_options' => array(
|
|
|
|
* 'cache_dir' => TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cache'
|
|
|
|
* )
|
2010-04-12 07:04:05 +02:00
|
|
|
* ));
|
2012-09-26 23:34:00 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* // No need for special backend for aggregate - TwoLevels with a File slow
|
|
|
|
* // backend supports tags
|
2012-09-26 23:34:00 +02:00
|
|
|
* SS_Cache::pick_backend('two-level', 'any', 10);
|
2013-05-12 02:39:37 +02:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* <h2>Invalidate an element</h2>
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $cache = SS_Cache::factory('foo');
|
|
|
|
* $cache->remove($cachekey);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* <h2>Clear the cache</h2>
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* This clears the entire backend, not just this named cache partition.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $cache = SS_Cache::factory('foo');
|
|
|
|
* $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
|
|
|
|
* </code>
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-04-23 02:11:41 +02:00
|
|
|
* @subpackage core
|
2010-04-12 07:04:05 +02:00
|
|
|
*/
|
2010-04-13 03:48:06 +02:00
|
|
|
class SS_Cache {
|
2010-04-12 07:04:05 +02:00
|
|
|
|
2013-05-12 02:39:37 +02:00
|
|
|
/**
|
|
|
|
* @var array $backends
|
|
|
|
*/
|
2010-04-12 07:04:05 +02:00
|
|
|
protected static $backends = array();
|
2013-05-12 02:39:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $backend_picks
|
|
|
|
*/
|
2010-04-12 07:04:05 +02:00
|
|
|
protected static $backend_picks = array();
|
2012-09-03 02:55:19 +02:00
|
|
|
|
2013-05-12 02:39:37 +02:00
|
|
|
/**
|
|
|
|
* @var array $cache_lifetime
|
|
|
|
*/
|
2010-04-12 07:04:05 +02:00
|
|
|
protected static $cache_lifetime = array();
|
|
|
|
|
|
|
|
/**
|
2013-05-12 02:39:37 +02:00
|
|
|
* Initialize the 'default' named cache backend.
|
2010-04-12 07:04:05 +02:00
|
|
|
*/
|
|
|
|
protected static function init(){
|
|
|
|
if (!isset(self::$backends['default'])) {
|
|
|
|
$cachedir = TEMP_FOLDER . DIRECTORY_SEPARATOR . 'cache';
|
2013-05-12 02:39:37 +02:00
|
|
|
|
|
|
|
if (!is_dir($cachedir)) {
|
|
|
|
mkdir($cachedir);
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
self::$backends['default'] = array(
|
|
|
|
'File',
|
2013-05-12 02:39:37 +02:00
|
|
|
array(
|
|
|
|
'cache_dir' => $cachedir
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
self::$cache_lifetime['default'] = array(
|
|
|
|
'lifetime' => 600,
|
|
|
|
'priority' => 1
|
2012-09-26 23:34:00 +02:00
|
|
|
);
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-12 02:39:37 +02:00
|
|
|
* Add a new named cache backend.
|
2010-04-12 07:04:05 +02:00
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* @see http://framework.zend.com/manual/en/zend.cache.html
|
|
|
|
*
|
2010-04-12 07:04:05 +02:00
|
|
|
* @param string $name The name of this backend as a freeform string
|
|
|
|
* @param string $type The Zend_Cache backend ('File' or 'Sqlite' or ...)
|
2013-05-12 02:39:37 +02:00
|
|
|
* @param array $options The Zend_Cache backend options
|
|
|
|
*
|
2010-04-12 07:04:05 +02:00
|
|
|
* @return none
|
|
|
|
*/
|
2013-05-12 02:39:37 +02:00
|
|
|
public static function add_backend($name, $type, $options = array()) {
|
2010-04-12 07:04:05 +02:00
|
|
|
self::init();
|
|
|
|
self::$backends[$name] = array($type, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-12 02:39:37 +02:00
|
|
|
* Pick a named cache backend for a particular named cache.
|
|
|
|
*
|
|
|
|
* The priority call with the highest number will be the actual backend
|
|
|
|
* picked. A backend picked for a specific cache name will always be used
|
|
|
|
* instead of 'any' if it exists, no matter the priority.
|
|
|
|
*
|
2010-04-12 07:04:05 +02:00
|
|
|
* @param string $name The name of the backend, as passed as the first argument to add_backend
|
|
|
|
* @param string $for The name of the cache to pick this backend for (or 'any' for any backend)
|
2013-05-12 02:39:37 +02:00
|
|
|
* @param integer $priority The priority of this pick
|
|
|
|
*
|
2010-04-12 07:04:05 +02:00
|
|
|
* @return none
|
|
|
|
*/
|
2013-05-12 02:39:37 +02:00
|
|
|
public static function pick_backend($name, $for, $priority = 1) {
|
2010-04-12 07:04:05 +02:00
|
|
|
self::init();
|
|
|
|
|
|
|
|
$current = -1;
|
|
|
|
|
2013-05-12 02:39:37 +02:00
|
|
|
if (isset(self::$backend_picks[$for])) {
|
|
|
|
$current = self::$backend_picks[$for]['priority'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($priority >= $current) {
|
|
|
|
self::$backend_picks[$for] = array(
|
|
|
|
'name' => $name,
|
|
|
|
'priority' => $priority
|
|
|
|
);
|
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
2012-09-03 02:55:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the cache lifetime for a particular named cache.
|
2013-05-12 02:39:37 +02:00
|
|
|
*
|
|
|
|
* @param string $for
|
|
|
|
*
|
|
|
|
* @return string
|
2012-09-03 02:55:19 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_cache_lifetime($for) {
|
2013-05-12 02:39:37 +02:00
|
|
|
if(isset(self::$cache_lifetime[$for])) {
|
|
|
|
return self::$cache_lifetime[$for];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2012-09-03 02:55:19 +02:00
|
|
|
}
|
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
/**
|
|
|
|
* Set the cache lifetime for a particular named cache
|
2012-09-03 02:55:19 +02:00
|
|
|
*
|
2010-04-12 07:04:05 +02:00
|
|
|
* @param string $for The name of the cache to set this lifetime for (or 'any' for all backends)
|
|
|
|
* @param integer $lifetime The lifetime of an item of the cache, in seconds, or -1 to disable caching
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param integer $priority The priority. The highest priority setting is used. Unlike backends, 'any' is not
|
|
|
|
* special in terms of priority.
|
2010-04-12 07:04:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_cache_lifetime($for, $lifetime=600, $priority=1) {
|
2010-04-12 07:04:05 +02:00
|
|
|
self::init();
|
|
|
|
|
|
|
|
$current = -1;
|
2013-05-12 02:39:37 +02:00
|
|
|
|
|
|
|
if (isset(self::$cache_lifetime[$for])) {
|
|
|
|
$current = self::$cache_lifetime[$for]['priority'];
|
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if ($priority >= $current) {
|
2013-05-12 02:39:37 +02:00
|
|
|
self::$cache_lifetime[$for] = array(
|
|
|
|
'lifetime' => $lifetime,
|
|
|
|
'priority' => $priority
|
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-12 02:39:37 +02:00
|
|
|
* Build a cache object.
|
|
|
|
*
|
|
|
|
* @see http://framework.zend.com/manual/en/zend.cache.html
|
|
|
|
*
|
|
|
|
* @param string $for The name of the cache to build
|
|
|
|
* @param string $frontend (optional) The type of Zend_Cache frontend
|
2010-04-12 07:04:05 +02:00
|
|
|
* @param array $frontendOptions (optional) Any frontend options to use.
|
|
|
|
*
|
2013-05-12 02:39:37 +02:00
|
|
|
* @return Zend_Cache_Frontend The cache object
|
2010-04-12 07:04:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function factory($for, $frontend='Output', $frontendOptions=null) {
|
2010-04-12 07:04:05 +02:00
|
|
|
self::init();
|
|
|
|
|
2012-09-03 02:55:19 +02:00
|
|
|
$backend_name = 'default';
|
|
|
|
$backend_priority = -1;
|
|
|
|
$cache_lifetime = self::$cache_lifetime['default']['lifetime'];
|
|
|
|
$lifetime_priority = -1;
|
|
|
|
|
2013-05-12 02:39:37 +02:00
|
|
|
foreach(array('any', $for) as $name) {
|
|
|
|
if(isset(self::$backend_picks[$name])) {
|
|
|
|
if(self::$backend_picks[$name]['priority'] > $backend_priority) {
|
|
|
|
$backend_name = self::$backend_picks[$name]['name'];
|
|
|
|
$backend_priority = self::$backend_picks[$name]['priority'];
|
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
|
|
|
|
2013-05-12 02:39:37 +02:00
|
|
|
if (isset(self::$cache_lifetime[$name])) {
|
|
|
|
if(self::$cache_lifetime[$name]['priority'] > $lifetime_priority) {
|
|
|
|
$cache_lifetime = self::$cache_lifetime[$name]['lifetime'];
|
|
|
|
$lifetime_priority = self::$cache_lifetime[$name]['priority'];
|
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
2012-09-03 02:55:19 +02:00
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
|
|
|
|
$backend = self::$backends[$backend_name];
|
|
|
|
|
|
|
|
$basicOptions = array('cache_id_prefix' => $for);
|
|
|
|
|
2013-05-12 02:39:37 +02:00
|
|
|
if ($cache_lifetime >= 0) {
|
|
|
|
$basicOptions['lifetime'] = $cache_lifetime;
|
|
|
|
} else {
|
|
|
|
$basicOptions['caching'] = false;
|
|
|
|
}
|
2012-09-03 02:55:19 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$frontendOptions = $frontendOptions ? array_merge($basicOptions, $frontendOptions) : $basicOptions;
|
|
|
|
|
|
|
|
require_once 'Zend/Cache.php';
|
2013-05-12 02:39:37 +02:00
|
|
|
|
|
|
|
return Zend_Cache::factory(
|
|
|
|
$frontend, $backend[0], $frontendOptions, $backend[1]
|
|
|
|
);
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|