2014-08-15 08:53:05 +02:00
|
|
|
<?php
|
2013-05-12 02:39:37 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* The `[api:SS_Cache]` class provides a bunch of static functions wrapping the Zend_Cache system
|
2013-05-12 02:39:37 +02:00
|
|
|
* in something a little more easy to use with the SilverStripe config system.
|
|
|
|
*
|
2013-11-21 14:13:51 +01:00
|
|
|
* @see http://doc.silverstripe.org/framework/en/topics/caching
|
2013-05-12 02:39:37 +02:00
|
|
|
*
|
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 {
|
2014-08-15 08:53: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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
/**
|
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(
|
2014-08-15 08:53:05 +02:00
|
|
|
'File',
|
2013-05-12 02:39:37 +02:00
|
|
|
array(
|
|
|
|
'cache_dir' => $cachedir
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
self::$cache_lifetime['default'] = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'lifetime' => 600,
|
2013-05-12 02:39:37 +02:00
|
|
|
'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.
|
2014-08-15 08:53: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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
/**
|
2013-05-12 02:39:37 +02:00
|
|
|
* Pick a named cache backend for a particular named cache.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* 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.
|
2013-05-12 02:39:37 +02:00
|
|
|
*
|
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)
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param integer $priority The priority of this pick
|
2013-05-12 02:39:37 +02:00
|
|
|
*
|
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(
|
2014-08-15 08:53:05 +02:00
|
|
|
'name' => $name,
|
2013-05-12 02:39:37 +02:00
|
|
|
'priority' => $priority
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
2013-05-12 02:39:37 +02:00
|
|
|
}
|
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
|
2014-08-15 08:53:05 +02:00
|
|
|
* 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$current = -1;
|
2013-05-12 02:39:37 +02:00
|
|
|
|
|
|
|
if (isset(self::$cache_lifetime[$for])) {
|
|
|
|
$current = self::$cache_lifetime[$for]['priority'];
|
|
|
|
}
|
2014-08-15 08:53: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(
|
2014-08-15 08:53:05 +02:00
|
|
|
'lifetime' => $lifetime,
|
2013-05-12 02:39:37 +02:00
|
|
|
'priority' => $priority
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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])) {
|
2013-08-21 08:54:05 +02:00
|
|
|
if(self::$backend_picks[$name]['priority'] > $backend_priority) {
|
2013-05-12 02:39:37 +02:00
|
|
|
$backend_name = self::$backend_picks[$name]['name'];
|
|
|
|
$backend_priority = self::$backend_picks[$name]['priority'];
|
|
|
|
}
|
2010-04-12 07:04:05 +02:00
|
|
|
}
|
2014-08-15 08:53: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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
$backend = self::$backends[$backend_name];
|
|
|
|
|
2017-07-18 17:51:03 +02:00
|
|
|
$basicOptions = array(
|
2017-07-19 12:29:01 +02:00
|
|
|
'cache_id_prefix' => md5(BASE_PATH) . '_' . $for . '_',
|
2017-07-18 17:51:03 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:04:05 +02:00
|
|
|
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
|
|
|
}
|