import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -40,26 +40,23 @@ require_once 'Zend/Cache/Backend.php';
|
||||
class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
|
||||
{
|
||||
/**
|
||||
* Default Host IP Address or DNS
|
||||
*/
|
||||
const DEFAULT_HOST = '127.0.0.1';
|
||||
|
||||
/**
|
||||
* Default port
|
||||
*/
|
||||
const DEFAULT_PORT = 11211;
|
||||
|
||||
/**
|
||||
* Persistent
|
||||
* Default Values
|
||||
*/
|
||||
const DEFAULT_HOST = '127.0.0.1';
|
||||
const DEFAULT_PORT = 11211;
|
||||
const DEFAULT_PERSISTENT = true;
|
||||
const DEFAULT_WEIGHT = 1;
|
||||
const DEFAULT_TIMEOUT = 1;
|
||||
const DEFAULT_RETRY_INTERVAL = 15;
|
||||
const DEFAULT_STATUS = true;
|
||||
const DEFAULT_FAILURE_CALLBACK = null;
|
||||
|
||||
/**
|
||||
* Log message
|
||||
*/
|
||||
const TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::clean() : tags are unsupported by the Memcached backend';
|
||||
const TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend';
|
||||
|
||||
|
||||
/**
|
||||
* Available options
|
||||
*
|
||||
@ -68,19 +65,41 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
* 'host' => (string) : the name of the memcached server
|
||||
* 'port' => (int) : the port of the memcached server
|
||||
* 'persistent' => (bool) : use or not persistent connections to this memcached server
|
||||
* 'weight' => (int) : number of buckets to create for this server which in turn control its
|
||||
* probability of it being selected. The probability is relative to the total
|
||||
* weight of all servers.
|
||||
* 'timeout' => (int) : value in seconds which will be used for connecting to the daemon. Think twice
|
||||
* before changing the default value of 1 second - you can lose all the
|
||||
* advantages of caching if your connection is too slow.
|
||||
* 'retry_interval' => (int) : controls how often a failed server will be retried, the default value
|
||||
* is 15 seconds. Setting this parameter to -1 disables automatic retry.
|
||||
* 'status' => (bool) : controls if the server should be flagged as online.
|
||||
* 'failure_callback' => (callback) : Allows the user to specify a callback function to run upon
|
||||
* encountering an error. The callback is run before failover
|
||||
* is attempted. The function takes two parameters, the hostname
|
||||
* and port of the failed server.
|
||||
*
|
||||
* =====> (boolean) compression :
|
||||
* true if you want to use on-the-fly compression
|
||||
*
|
||||
* =====> (boolean) compatibility :
|
||||
* true if you use old memcache server or extension
|
||||
*
|
||||
* @var array available options
|
||||
*/
|
||||
protected $_options = array(
|
||||
'servers' => array(array(
|
||||
'host' => Zend_Cache_Backend_Memcached::DEFAULT_HOST,
|
||||
'port' => Zend_Cache_Backend_Memcached::DEFAULT_PORT,
|
||||
'persistent' => Zend_Cache_Backend_Memcached::DEFAULT_PERSISTENT
|
||||
'host' => self::DEFAULT_HOST,
|
||||
'port' => self::DEFAULT_PORT,
|
||||
'persistent' => self::DEFAULT_PERSISTENT,
|
||||
'weight' => self::DEFAULT_WEIGHT,
|
||||
'timeout' => self::DEFAULT_TIMEOUT,
|
||||
'retry_interval' => self::DEFAULT_RETRY_INTERVAL,
|
||||
'status' => self::DEFAULT_STATUS,
|
||||
'failure_callback' => self::DEFAULT_FAILURE_CALLBACK
|
||||
)),
|
||||
'compression' => false
|
||||
'compression' => false,
|
||||
'compatibility' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
@ -113,13 +132,38 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
}
|
||||
$this->_memcache = new Memcache;
|
||||
foreach ($this->_options['servers'] as $server) {
|
||||
if (!array_key_exists('persistent', $server)) {
|
||||
$server['persistent'] = Zend_Cache_Backend_Memcached::DEFAULT_PERSISTENT;
|
||||
}
|
||||
if (!array_key_exists('port', $server)) {
|
||||
$server['port'] = Zend_Cache_Backend_Memcached::DEFAULT_PORT;
|
||||
$server['port'] = self::DEFAULT_PORT;
|
||||
}
|
||||
$this->_memcache->addServer($server['host'], $server['port'], $server['persistent']);
|
||||
if (!array_key_exists('persistent', $server)) {
|
||||
$server['persistent'] = self::DEFAULT_PERSISTENT;
|
||||
}
|
||||
if (!array_key_exists('weight', $server)) {
|
||||
$server['weight'] = self::DEFAULT_WEIGHT;
|
||||
}
|
||||
if (!array_key_exists('timeout', $server)) {
|
||||
$server['timeout'] = self::DEFAULT_TIMEOUT;
|
||||
}
|
||||
if (!array_key_exists('retry_interval', $server)) {
|
||||
$server['retry_interval'] = self::DEFAULT_RETRY_INTERVAL;
|
||||
}
|
||||
if (!array_key_exists('status', $server)) {
|
||||
$server['status'] = self::DEFAULT_STATUS;
|
||||
}
|
||||
if (!array_key_exists('failure_callback', $server)) {
|
||||
$server['failure_callback'] = self::DEFAULT_FAILURE_CALLBACK;
|
||||
}
|
||||
if ($this->_options['compatibility']) {
|
||||
// No status for compatibility mode (#ZF-5887)
|
||||
$this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
|
||||
$server['weight'], $server['timeout'],
|
||||
$server['retry_interval']);
|
||||
} else {
|
||||
$this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
|
||||
$server['weight'], $server['timeout'],
|
||||
$server['retry_interval'],
|
||||
$server['status'], $server['failure_callback']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -174,10 +218,8 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
} else {
|
||||
$flag = 0;
|
||||
}
|
||||
if ($this->test($id)) {
|
||||
// because set and replace seems to have different behaviour
|
||||
$result = $this->_memcache->replace($id, array($data, time(), $lifetime), $flag, $lifetime);
|
||||
} else {
|
||||
// #ZF-5702 : we try add() first becase set() seems to be slower
|
||||
if (!($result = $this->_memcache->add($id, array($data, time(), $lifetime), $flag, $lifetime))) {
|
||||
$result = $this->_memcache->set($id, array($data, time(), $lifetime), $flag, $lifetime);
|
||||
}
|
||||
if (count($tags) > 0) {
|
||||
@ -257,15 +299,15 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
// #ZF-3490 : For the memcached backend, there is a lifetime limit of 30 days (2592000 seconds)
|
||||
$this->_log('memcached backend has a limit of 30 days (2592000 seconds) for the lifetime');
|
||||
}
|
||||
if (is_null($lifetime)) {
|
||||
// #ZF-4614 : we tranform null to zero to get the maximal lifetime
|
||||
parent::setDirectives(array('lifetime' => 0));
|
||||
if ($lifetime === null) {
|
||||
// #ZF-4614 : we tranform null to zero to get the maximal lifetime
|
||||
parent::setDirectives(array('lifetime' => 0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return an array of stored cache ids
|
||||
*
|
||||
*
|
||||
* @return array array of stored cache ids (string)
|
||||
*/
|
||||
public function getIds()
|
||||
@ -273,7 +315,7 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
$this->_log("Zend_Cache_Backend_Memcached::save() : getting the list of cache ids is unsupported by the Memcache backend");
|
||||
return array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return an array of stored tags
|
||||
*
|
||||
@ -284,10 +326,10 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -301,21 +343,21 @@ class Zend_Cache_Backend_Memcached 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())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
|
||||
return array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -324,7 +366,7 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
public function getIdsMatchingAnyTags($tags = array())
|
||||
{
|
||||
$this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
|
||||
return array();
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -335,15 +377,29 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
*/
|
||||
public function getFillingPercentage()
|
||||
{
|
||||
$mem = $this->_memcache->getStats();
|
||||
$memSize = $mem['limit_maxbytes'];
|
||||
$memUsed= $mem['bytes'];
|
||||
if ($memSize == 0) {
|
||||
Zend_Cache::throwException('can\'t get memcache memory size');
|
||||
}
|
||||
if ($memUsed > $memSize) {
|
||||
return 100;
|
||||
$mems = $this->_memcache->getExtendedStats();
|
||||
|
||||
$memSize = 0;
|
||||
$memUsed = 0;
|
||||
foreach ($mems as $key => $mem) {
|
||||
if ($mem === false) {
|
||||
Zend_Cache::throwException('can\'t get stat from ' . $key);
|
||||
} else {
|
||||
$eachSize = $mem['limit_maxbytes'];
|
||||
if ($eachSize == 0) {
|
||||
Zend_Cache::throwException('can\'t get memory size from ' . $key);
|
||||
}
|
||||
|
||||
$eachUsed = $mem['bytes'];
|
||||
if ($eachUsed > $eachSize) {
|
||||
$eachUsed = $eachSize;
|
||||
}
|
||||
|
||||
$memSize += $eachSize;
|
||||
$memUsed += $eachUsed;
|
||||
}
|
||||
}
|
||||
|
||||
return ((int) (100. * ($memUsed / $memSize)));
|
||||
}
|
||||
|
||||
@ -354,7 +410,7 @@ class Zend_Cache_Backend_Memcached 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)
|
||||
*/
|
||||
@ -376,9 +432,9 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
'mtime' => $mtime
|
||||
);
|
||||
}
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Give (if possible) an extra lifetime to the given cache id
|
||||
*
|
||||
@ -405,22 +461,20 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
$lifetime = $tmp[2];
|
||||
$newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
|
||||
if ($newLifetime <=0) {
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
if ($this->test($id)) {
|
||||
// because set and replace seems to have different behaviour
|
||||
$result = $this->_memcache->replace($id, array($data, time(), $newLifetime), $flag, $newLifetime);
|
||||
} else {
|
||||
$result = $this->_memcache->set($id, array($data, time(), $newLifetime), $flag, $newLifetime);
|
||||
// #ZF-5702 : we try replace() first becase set() seems to be slower
|
||||
if (!($result = $this->_memcache->replace($id, array($data, time(), $newLifetime), $flag, $newLifetime))) {
|
||||
$result = $this->_memcache->set($id, array($data, time(), $newLifetime), $flag, $newLifetime);
|
||||
}
|
||||
return true;
|
||||
return $result;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
@ -429,7 +483,7 @@ class Zend_Cache_Backend_Memcached 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()
|
||||
@ -443,5 +497,5 @@ class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Ca
|
||||
'get_list' => false
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user