import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -44,12 +44,12 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
* Available options
|
||||
*
|
||||
* =====> (string) slow_backend :
|
||||
* - Slow backend name
|
||||
* - Slow backend name
|
||||
* - Must implement the Zend_Cache_Backend_ExtendedInterface
|
||||
* - Should provide a big storage
|
||||
*
|
||||
* =====> (string) fast_backend :
|
||||
* - Flow backend name
|
||||
* - Flow backend name
|
||||
* - Must implement the Zend_Cache_Backend_ExtendedInterface
|
||||
* - Must be much faster than slow_backend
|
||||
*
|
||||
@ -58,22 +58,22 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
*
|
||||
* =====> (array) fast_backend_options :
|
||||
* - Fast backend options (see corresponding backend)
|
||||
*
|
||||
*
|
||||
* =====> (int) stats_update_factor :
|
||||
* - Disable / Tune the computation of the fast backend filling percentage
|
||||
* - When saving a record into cache :
|
||||
* 1 => systematic computation of the fast backend filling percentage
|
||||
* x (integer) > 1 => computation of the fast backend filling percentage randomly 1 times on x cache write
|
||||
*
|
||||
*
|
||||
* =====> (boolean) slow_backend_custom_naming :
|
||||
* =====> (boolean) fast_backend_custom_naming :
|
||||
* =====> (boolean) slow_backend_autoload :
|
||||
* =====> (boolean) fast_backend_autoload :
|
||||
* - See Zend_Cache::factory() method
|
||||
*
|
||||
* - See Zend_Cache::factory() method
|
||||
*
|
||||
* =====> (boolean) auto_refresh_fast_cache
|
||||
* - If true, auto refresh the fast cache when a cache record is hit
|
||||
*
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
@ -88,28 +88,28 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
'fast_backend_autoload' => false,
|
||||
'auto_refresh_fast_cache' => true
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Slow Backend
|
||||
*
|
||||
* @var Zend_Cache_Backend
|
||||
*/
|
||||
private $_slowBackend;
|
||||
|
||||
|
||||
/**
|
||||
* Fast Backend
|
||||
*
|
||||
* @var Zend_Cache_Backend
|
||||
*/
|
||||
private $_fastBackend;
|
||||
|
||||
|
||||
/**
|
||||
* Cache for the fast backend filling percentage
|
||||
*
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_fastBackendFillingPercentage = null;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@ -120,24 +120,24 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
parent::__construct($options);
|
||||
if (is_null($this->_options['slow_backend'])) {
|
||||
if ($this->_options['slow_backend'] === null) {
|
||||
Zend_Cache::throwException('slow_backend option has to set');
|
||||
}
|
||||
if (is_null($this->_options['fast_backend'])) {
|
||||
if ($this->_options['fast_backend'] === null) {
|
||||
Zend_Cache::throwException('fast_backend option has to set');
|
||||
}
|
||||
}
|
||||
$this->_slowBackend = Zend_Cache::_makeBackend($this->_options['slow_backend'], $this->_options['slow_backend_options'], $this->_options['slow_backend_custom_naming'], $this->_options['slow_backend_autoload']);
|
||||
$this->_fastBackend = Zend_Cache::_makeBackend($this->_options['fast_backend'], $this->_options['fast_backend_options'], $this->_options['fast_backend_custom_naming'], $this->_options['fast_backend_autoload']);
|
||||
$this->_fastBackend = Zend_Cache::_makeBackend($this->_options['fast_backend'], $this->_options['fast_backend_options'], $this->_options['fast_backend_custom_naming'], $this->_options['fast_backend_autoload']);
|
||||
if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_slowBackend))) {
|
||||
Zend_Cache::throwException('slow_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
|
||||
}
|
||||
if (!in_array('Zend_Cache_Backend_ExtendedInterface', class_implements($this->_fastBackend))) {
|
||||
Zend_Cache::throwException('slow_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
|
||||
}
|
||||
Zend_Cache::throwException('fast_backend must implement the Zend_Cache_Backend_ExtendedInterface interface');
|
||||
}
|
||||
$this->_slowBackend->setDirectives($this->_directives);
|
||||
$this->_fastBackend->setDirectives($this->_directives);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if a cache is available or not (for the given id)
|
||||
*
|
||||
@ -153,7 +153,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
return $this->_slowBackend->test($id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save some string datas into a cache record
|
||||
*
|
||||
@ -164,7 +164,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
* @param string $id Cache id
|
||||
* @param array $tags Array of strings, the cache record will be tagged by each string entry
|
||||
* @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
|
||||
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
|
||||
* @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function save($data, $id, $tags = array(), $specificLifetime = false, $priority = 8)
|
||||
@ -180,7 +180,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
$boolSlow = $this->_slowBackend->save($preparedData, $id, $tags, $lifetime);
|
||||
return ($boolFast && $boolSlow);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test if a cache is available for the given id and (if yes) return it (false else)
|
||||
*
|
||||
@ -212,13 +212,13 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
$usage = $this->_getFastFillingPercentage('loading');
|
||||
if (($array['priority'] > 0) && (10 * $array['priority'] >= $usage)) {
|
||||
// we can refresh the fast cache
|
||||
$preparedData = $this->_prepareData($array['data'], $array['lifetime'], $array['priority']);
|
||||
$preparedData = $this->_prepareData($array['data'], $array['lifetime'], $array['priority']);
|
||||
$this->_fastBackend->save($preparedData, $id, array(), $newFastLifetime);
|
||||
}
|
||||
}
|
||||
return $array['data'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a cache record
|
||||
*
|
||||
@ -230,7 +230,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
$this->_fastBackend->remove($id);
|
||||
return $this->_slowBackend->remove($id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clean some cache records
|
||||
*
|
||||
@ -249,7 +249,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
* @throws Zend_Cache_Exception
|
||||
* @return boolean true if no problem
|
||||
*/
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
|
||||
{
|
||||
switch($mode) {
|
||||
case Zend_Cache::CLEANING_MODE_ALL:
|
||||
@ -288,17 +288,17 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
{
|
||||
return $this->_slowBackend->getIds();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
@ -308,10 +308,10 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
{
|
||||
return $this->_slowBackend->getTags();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match given tags
|
||||
*
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
@ -324,12 +324,12 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which don't match given tags
|
||||
*
|
||||
*
|
||||
* In case of multiple tags, a logical OR is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
* @return array array of not matching cache ids (string)
|
||||
*/
|
||||
*/
|
||||
public function getIdsNotMatchingTags($tags = array())
|
||||
{
|
||||
return $this->_slowBackend->getIdsNotMatchingTags($tags);
|
||||
@ -337,7 +337,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids which match any given tags
|
||||
*
|
||||
*
|
||||
* In case of multiple tags, a logical AND is made between tags
|
||||
*
|
||||
* @param array $tags array of tags
|
||||
@ -347,8 +347,8 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
{
|
||||
return $this->_slowBackend->getIdsMatchingAnyTags($tags);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return the filling percentage of the backend storage
|
||||
*
|
||||
@ -366,7 +366,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
* - expire : the expire timestamp
|
||||
* - tags : a string array of tags
|
||||
* - mtime : timestamp of last modification time
|
||||
*
|
||||
*
|
||||
* @param string $id cache id
|
||||
* @return array array of metadatas (false if the cache id is not found)
|
||||
*/
|
||||
@ -374,7 +374,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
{
|
||||
return $this->_slowBackend->getMetadatas($id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
@ -386,10 +386,10 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
{
|
||||
return $this->_slowBackend->touch($id, $extraLifetime);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return an associative array of capabilities (booleans) of the backend
|
||||
*
|
||||
*
|
||||
* The array must include these keys :
|
||||
* - automatic_cleaning (is automating cleaning necessary)
|
||||
* - tags (are tags supported)
|
||||
@ -398,7 +398,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
* - priority does the backend deal with priority when saving
|
||||
* - infinite_lifetime (is infinite lifetime can work with this backend)
|
||||
* - get_list (is it possible to get the list of cache ids and the complete list of tags)
|
||||
*
|
||||
*
|
||||
* @return array associative of with capabilities
|
||||
*/
|
||||
public function getCapabilities()
|
||||
@ -413,7 +413,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
'get_list' => $slowBackendCapabilities['get_list']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare a serialized array to store datas and metadatas informations
|
||||
*
|
||||
@ -425,7 +425,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
private function _prepareData($data, $lifetime, $priority)
|
||||
{
|
||||
$lt = $lifetime;
|
||||
if (is_null($lt)) {
|
||||
if ($lt === null) {
|
||||
$lt = 9999999999;
|
||||
}
|
||||
return serialize(array(
|
||||
@ -435,7 +435,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
'priority' => $priority
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compute and return the lifetime for the fast backend
|
||||
*
|
||||
@ -446,21 +446,21 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
*/
|
||||
private function _getFastLifetime($lifetime, $priority, $maxLifetime = null)
|
||||
{
|
||||
if (is_null($lifetime)) {
|
||||
if ($lifetime === null) {
|
||||
// if lifetime is null, we have an infinite lifetime
|
||||
// we need to use arbitrary lifetimes
|
||||
$fastLifetime = (int) (2592000 / (11 - $priority));
|
||||
} else {
|
||||
$fastLifetime = (int) ($lifetime / (11 - $priority));
|
||||
}
|
||||
if (!is_null($maxLifetime) && ($maxLifetime >= 0)) {
|
||||
if (($maxLifetime !== null) && ($maxLifetime >= 0)) {
|
||||
if ($fastLifetime > $maxLifetime) {
|
||||
return $maxLifetime;
|
||||
}
|
||||
}
|
||||
return $fastLifetime;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* PUBLIC METHOD FOR UNIT TESTING ONLY !
|
||||
*
|
||||
@ -471,15 +471,15 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
public function ___expire($id)
|
||||
{
|
||||
$this->_fastBackend->remove($id);
|
||||
$this->_slowBackend->___expire($id);
|
||||
}
|
||||
|
||||
$this->_slowBackend->___expire($id);
|
||||
}
|
||||
|
||||
private function _getFastFillingPercentage($mode)
|
||||
{
|
||||
|
||||
|
||||
if ($mode == 'saving') {
|
||||
// mode saving
|
||||
if (is_null($this->_fastBackendFillingPercentage)) {
|
||||
if ($this->_fastBackendFillingPercentage === null) {
|
||||
$this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
|
||||
} else {
|
||||
$rand = rand(1, $this->_options['stats_update_factor']);
|
||||
@ -491,11 +491,11 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
|
||||
} else {
|
||||
// mode loading
|
||||
// we compute the percentage only if it's not available in cache
|
||||
if (is_null($this->_fastBackendFillingPercentage)) {
|
||||
if ($this->_fastBackendFillingPercentage === null) {
|
||||
$this->_fastBackendFillingPercentage = $this->_fastBackend->getFillingPercentage();
|
||||
}
|
||||
}
|
||||
return $this->_fastBackendFillingPercentage;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user