/library/Zend/Cache/Backend/Libmemcached.php
PHP | 484 lines | 237 code | 41 blank | 206 comment | 28 complexity | 502670a3545683081f97cc4f821f93ef MD5 | raw file
Possible License(s): AGPL-1.0
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Cache
- * @subpackage Zend_Cache_Backend
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id: Libmemcached.php 24594 2012-01-05 21:27:01Z matthew $
- */
- /**
- * @see Zend_Cache_Backend_Interface
- */
- require_once 'Zend/Cache/Backend/ExtendedInterface.php';
- /**
- * @see Zend_Cache_Backend
- */
- require_once 'Zend/Cache/Backend.php';
- /**
- * @package Zend_Cache
- * @subpackage Zend_Cache_Backend
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Cache_Backend_Libmemcached extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
- {
- /**
- * Default Server Values
- */
- const DEFAULT_HOST = '127.0.0.1';
- const DEFAULT_PORT = 11211;
- const DEFAULT_WEIGHT = 1;
- /**
- * Log message
- */
- const TAGS_UNSUPPORTED_BY_CLEAN_OF_LIBMEMCACHED_BACKEND = 'Zend_Cache_Backend_Libmemcached::clean() : tags are unsupported by the Libmemcached backend';
- const TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND = 'Zend_Cache_Backend_Libmemcached::save() : tags are unsupported by the Libmemcached backend';
- /**
- * Available options
- *
- * =====> (array) servers :
- * an array of memcached server ; each memcached server is described by an associative array :
- * 'host' => (string) : the name of the memcached server
- * 'port' => (int) : the port of the 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.
- * =====> (array) client :
- * an array of memcached client options ; the memcached client is described by an associative array :
- * @see http://php.net/manual/memcached.constants.php
- * - The option name can be the name of the constant without the prefix 'OPT_'
- * or the integer value of this option constant
- *
- * @var array available options
- */
- protected $_options = array(
- 'servers' => array(array(
- 'host' => self::DEFAULT_HOST,
- 'port' => self::DEFAULT_PORT,
- 'weight' => self::DEFAULT_WEIGHT,
- )),
- 'client' => array()
- );
- /**
- * Memcached object
- *
- * @var mixed memcached object
- */
- protected $_memcache = null;
- /**
- * Constructor
- *
- * @param array $options associative array of options
- * @throws Zend_Cache_Exception
- * @return void
- */
- public function __construct(array $options = array())
- {
- if (!extension_loaded('memcached')) {
- Zend_Cache::throwException('The memcached extension must be loaded for using this backend !');
- }
- // override default client options
- $this->_options['client'] = array(
- Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
- Memcached::OPT_HASH => Memcached::HASH_MD5,
- Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
- );
- parent::__construct($options);
- if (isset($this->_options['servers'])) {
- $value = $this->_options['servers'];
- if (isset($value['host'])) {
- // in this case, $value seems to be a simple associative array (one server only)
- $value = array(0 => $value); // let's transform it into a classical array of associative arrays
- }
- $this->setOption('servers', $value);
- }
- $this->_memcache = new Memcached;
- // setup memcached client options
- foreach ($this->_options['client'] as $name => $value) {
- $optId = null;
- if (is_int($name)) {
- $optId = $name;
- } else {
- $optConst = 'Memcached::OPT_' . strtoupper($name);
- if (defined($optConst)) {
- $optId = constant($optConst);
- } else {
- $this->_log("Unknown memcached client option '{$name}' ({$optConst})");
- }
- }
- if ($optId) {
- if (!$this->_memcache->setOption($optId, $value)) {
- $this->_log("Setting memcached client option '{$optId}' failed");
- }
- }
- }
- // setup memcached servers
- $servers = array();
- foreach ($this->_options['servers'] as $server) {
- if (!array_key_exists('port', $server)) {
- $server['port'] = self::DEFAULT_PORT;
- }
- if (!array_key_exists('weight', $server)) {
- $server['weight'] = self::DEFAULT_WEIGHT;
- }
- $servers[] = array($server['host'], $server['port'], $server['weight']);
- }
- $this->_memcache->addServers($servers);
- }
- /**
- * Test if a cache is available for the given id and (if yes) return it (false else)
- *
- * @param string $id Cache id
- * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
- * @return string|false cached datas
- */
- public function load($id, $doNotTestCacheValidity = false)
- {
- $tmp = $this->_memcache->get($id);
- if (isset($tmp[0])) {
- return $tmp[0];
- }
- return false;
- }
- /**
- * Test if a cache is available or not (for the given id)
- *
- * @param string $id Cache id
- * @return int|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
- */
- public function test($id)
- {
- $tmp = $this->_memcache->get($id);
- if (isset($tmp[0], $tmp[1])) {
- return (int)$tmp[1];
- }
- return false;
- }
- /**
- * Save some string datas into a cache record
- *
- * Note : $data is always "string" (serialization is done by the
- * core not by the backend)
- *
- * @param string $data Datas to cache
- * @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)
- * @return boolean True if no problem
- */
- public function save($data, $id, $tags = array(), $specificLifetime = false)
- {
- $lifetime = $this->getLifetime($specificLifetime);
- // ZF-8856: using set because add needs a second request if item already exists
- $result = @$this->_memcache->set($id, array($data, time(), $lifetime), $lifetime);
- if ($result === false) {
- $rsCode = $this->_memcache->getResultCode();
- $rsMsg = $this->_memcache->getResultMessage();
- $this->_log("Memcached::set() failed: [{$rsCode}] {$rsMsg}");
- }
- if (count($tags) > 0) {
- $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_LIBMEMCACHED_BACKEND);
- }
- return $result;
- }
- /**
- * Remove a cache record
- *
- * @param string $id Cache id
- * @return boolean True if no problem
- */
- public function remove($id)
- {
- return $this->_memcache->delete($id);
- }
- /**
- * Clean some cache records
- *
- * Available modes are :
- * 'all' (default) => remove all cache entries ($tags is not used)
- * 'old' => unsupported
- * 'matchingTag' => unsupported
- * 'notMatchingTag' => unsupported
- * 'matchingAnyTag' => unsupported
- *
- * @param string $mode Clean mode
- * @param array $tags Array of tags
- * @throws Zend_Cache_Exception
- * @return boolean True if no problem
- */
- public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
- {
- switch ($mode) {
-