Commit 23be1a7c by Qiang Xue

...

parent dcb531ac
<?php
/**
* CacheDependency class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* CacheDependency is the base class for cache dependency classes.
*
* CacheDependency implements the {@link ICacheDependency} interface.
* Child classes should override its {@link generateDependentData} for
* actual dependency checking.
*
* @property boolean $hasChanged Whether the dependency has changed.
* @property mixed $dependentData The data used to determine if dependency has been changed.
* This data is available after {@link evaluateDependency} is called.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CacheDependency extends \yii\base\Object
{
/**
* @var boolean Whether this dependency is reusable or not.
* If set to true, dependent data for this cache dependency will only be generated once per request.
* You can then use the same cache dependency for multiple separate cache calls on the same page
* without the overhead of re-evaluating the dependency each time.
* Defaults to false;
* @since 1.1.11
*/
public $reuseDependentData = false;
/**
* @var array cached data for reusable dependencies.
* @since 1.1.11
*/
private static $_reusableData = array();
private $_hash;
private $_data;
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* This method is invoked by cache before writing data into it.
*/
public function evaluateDependency()
{
if ($this->reuseDependentData) {
$hash = $this->getHash();
if (!isset(self::$_reusableData[$hash]['dependentData'])) {
self::$_reusableData[$hash]['dependentData'] = $this->generateDependentData();
}
$this->_data = self::$_reusableData[$hash]['dependentData'];
} else {
$this->_data = $this->generateDependentData();
}
}
/**
* @return boolean whether the dependency has changed.
*/
public function getHasChanged()
{
if ($this->reuseDependentData) {
$hash = $this->getHash();
if (!isset(self::$_reusableData[$hash]['hasChanged'])) {
if (!isset(self::$_reusableData[$hash]['dependentData'])) {
self::$_reusableData[$hash]['dependentData'] = $this->generateDependentData();
}
self::$_reusableData[$hash]['hasChanged'] = self::$_reusableData[$hash]['dependentData'] != $this->_data;
}
return self::$_reusableData[$hash]['hasChanged'];
} else {
return $this->generateDependentData() != $this->_data;
}
}
/**
* @return mixed the data used to determine if dependency has been changed.
* This data is available after {@link evaluateDependency} is called.
*/
public function getDependentData()
{
return $this->_data;
}
/**
* Generates the data needed to determine if dependency has been changed.
* Derived classes should override this method to generate actual dependent data.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
return null;
}
/**
* Generates a unique hash that identifies this cache dependency.
* @return string the hash for this cache dependency
*/
private function getHash()
{
if ($this->_hash === null) {
$this->_hash = sha1(serialize($this));
}
return $this->_hash;
}
}
\ No newline at end of file
<?php <?php
/** /**
* CCacheDependency class file. * Dependency class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
namespace yii\caching; namespace yii\caching;
/** /**
* CCacheDependency is the base class for cache dependency classes. * Dependency is the base class for cache dependency classes.
* *
* CCacheDependency implements the {@link ICacheDependency} interface. * Dependency implements the {@link ICacheDependency} interface.
* Child classes should override its {@link generateDependentData} for * Child classes should override its {@link generateDependentData} for
* actual dependency checking. * actual dependency checking.
* *
...@@ -23,7 +23,7 @@ namespace yii\caching; ...@@ -23,7 +23,7 @@ namespace yii\caching;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class CCacheDependency extends CComponent implements ICacheDependency class Dependency extends \yii\base\Object
{ {
/** /**
* @var boolean Whether this dependency is reusable or not. * @var boolean Whether this dependency is reusable or not.
......
...@@ -9,20 +9,27 @@ ...@@ -9,20 +9,27 @@
namespace yii\caching; namespace yii\caching;
use yii\base\Exception;
/** /**
* MemCache implements a cache application component based on {@link http://memcached.org/ memcached}. * MemCache implements a cache application component based on [memcache](http://pecl.php.net/package/memcache)
* and [memcached](http://pecl.php.net/package/memcached).
*
* MemCache supports both [memcache](http://pecl.php.net/package/memcache) and
* [memcached](http://pecl.php.net/package/memcached). By setting [[useMemcached]] to be true or false,
* one can let MemCache to use either memcached or memcache, respectively.
* *
* MemCache can be configured with a list of memcache servers by settings * MemCache can be configured with a list of memcache servers by settings its [[servers]] property.
* its {@link setServers servers} property. By default, MemCache assumes * By default, MemCache assumes there is a memcache server running on localhost at port 11211.
* there is a memcache server running on localhost at port 11211.
* *
* See {@link CCache} manual for common cache operations that are supported by MemCache. * See [[Cache]] for common cache operations that ApcCache supports.
* *
* Note, there is no security measure to protected data in memcache. * Note, there is no security measure to protected data in memcache.
* All data in memcache can be accessed by any process running in the system. * All data in memcache can be accessed by any process running in the system.
* *
* To use MemCache as the cache application component, configure the application as follows, * To use MemCache as the cache application component, configure the application as follows,
* <pre> *
* ~~~
* array( * array(
* 'components'=>array( * 'components'=>array(
* 'cache'=>array( * 'cache'=>array(
...@@ -42,18 +49,13 @@ namespace yii\caching; ...@@ -42,18 +49,13 @@ namespace yii\caching;
* ), * ),
* ), * ),
* ) * )
* </pre> * ~~~
* In the above, two memcache servers are used: server1 and server2.
* You can configure more properties of every server, including:
* host, port, persistent, weight, timeout, retryInterval, status.
* See {@link http://www.php.net/manual/en/function.memcache-addserver.php}
* for more details.
* *
* MemCache can also be used with {@link http://pecl.php.net/package/memcached memcached}. * In the above, two memcache servers are used: server1 and server2. You can configure more properties of
* To do so, set {@link useMemcached} to be true. * each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options.
* *
* @property mixed $memCache The memcache instance (or memcached if {@link useMemcached} is true) used by this component. * @property mixed $memCache The memcache instance (or memcached if [[useMemcached]] is true) used by this component.
* @property array $servers List of memcache server configurations. Each element is a {@link MemCacheServerConfiguration}. * @property MemCacheServer[] $servers List of memcache server configurations.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
...@@ -68,7 +70,7 @@ class MemCache extends Cache ...@@ -68,7 +70,7 @@ class MemCache extends Cache
*/ */
public $useMemcached = false; public $useMemcached = false;
/** /**
* @var Memcache the Memcache instance * @var \Memcache|\Memcached the Memcache instance
*/ */
private $_cache = null; private $_cache = null;
/** /**
...@@ -78,9 +80,7 @@ class MemCache extends Cache ...@@ -78,9 +80,7 @@ class MemCache extends Cache
/** /**
* Initializes this application component. * Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It creates the memcache instance and adds memcache servers. * It creates the memcache instance and adds memcache servers.
* @throws CException if memcache extension is not loaded
*/ */
public function init() public function init()
{ {
...@@ -89,36 +89,41 @@ class MemCache extends Cache ...@@ -89,36 +89,41 @@ class MemCache extends Cache
$cache = $this->getMemCache(); $cache = $this->getMemCache();
if (count($servers)) { if (count($servers)) {
foreach ($servers as $server) { foreach ($servers as $server) {
if ($server->host === null) {
throw new Exception("The 'host' property must be specified for every memcache server.");
}
if ($this->useMemcached) { if ($this->useMemcached) {
$cache->addServer($server->host, $server->port, $server->weight); $cache->addServer($server->host, $server->port, $server->weight);
} else { } else {
$cache->addServer($server->host, $server->port, $server->persistent, $server->weight, $server->timeout, $server->status); $cache->addServer($server->host, $server->port, $server->persistent,
$server->weight, $server->timeout, $server->retryInterval, $server->status);
} }
} }
} else { } else {
$cache->addServer('localhost', 11211); $cache->addServer('127.0.0.1', 11211);
} }
} }
/** /**
* @throws CException if extension isn't loaded * Returns the underlying memcache (or memcached) object.
* @return Memcache|Memcached the memcache instance (or memcached if {@link useMemcached} is true) used by this component. * @return \Memcache|\Memcached the memcache (or memcached) object used by this cache component.
* @throws Exception if memcache or memcached extension is not loaded
*/ */
public function getMemCache() public function getMemCache()
{ {
if ($this->_cache !== null) { if ($this->_cache === null) {
return $this->_cache;
} else {
$extension = $this->useMemcached ? 'memcached' : 'memcache'; $extension = $this->useMemcached ? 'memcached' : 'memcache';
if (!extension_loaded($extension)) { if (!extension_loaded($extension)) {
throw new CException(Yii::t('yii', "MemCache requires PHP $extension extension to be loaded.")); throw new Exception("MemCache requires PHP $extension extension to be loaded.");
} }
return $this->_cache = $this->useMemcached ? new Memcached : new Memcache; $this->_cache = $this->useMemcached ? new \Memcached : new \Memcache;
} }
return $this->_cache;
} }
/** /**
* @return array list of memcache server configurations. Each element is a {@link MemCacheServerConfiguration}. * Returns the memcache server configurations.
* @return MemCacheServer[] list of memcache server configurations.
*/ */
public function getServers() public function getServers()
{ {
...@@ -133,7 +138,7 @@ class MemCache extends Cache ...@@ -133,7 +138,7 @@ class MemCache extends Cache
public function setServers($config) public function setServers($config)
{ {
foreach ($config as $c) { foreach ($config as $c) {
$this->_servers[] = new MemCacheServerConfiguration($c); $this->_servers[] = MemCacheServer::newInstance($c);
} }
} }
...@@ -213,72 +218,9 @@ class MemCache extends Cache ...@@ -213,72 +218,9 @@ class MemCache extends Cache
* Deletes all values from cache. * Deletes all values from cache.
* This is the implementation of the method declared in the parent class. * This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful. * @return boolean whether the flush operation was successful.
* @since 1.1.5
*/ */
protected function flushValues() protected function flushValues()
{ {
return $this->_cache->flush(); return $this->_cache->flush();
} }
} }
/**
* MemCacheServerConfiguration represents the configuration data for a single memcache server.
*
* See {@link http://www.php.net/manual/en/function.Memcache-addServer.php}
* for detailed explanation of each configuration property.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching
* @since 1.0
*/
class MemCacheServerConfiguration extends CComponent
{
/**
* @var string memcache server hostname or IP address
*/
public $host;
/**
* @var integer memcache server port
*/
public $port = 11211;
/**
* @var boolean whether to use a persistent connection
*/
public $persistent = true;
/**
* @var integer probability of using this server among all servers.
*/
public $weight = 1;
/**
* @var integer value in seconds which will be used for connecting to the server
*/
public $timeout = 15;
/**
* @var integer how often a failed server will be retried (in seconds)
*/
public $retryInterval = 15;
/**
* @var boolean if the server should be flagged as online upon a failure
*/
public $status = true;
/**
* Constructor.
* @param array $config list of memcache server configurations.
* @throws CException if the configuration is not an array
*/
public function __construct($config)
{
if (is_array($config)) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
if ($this->host === null) {
throw new CException(Yii::t('yii', 'MemCache server configuration must have "host" value.'));
}
} else {
throw new CException(Yii::t('yii', 'MemCache server configuration must be an array.'));
}
}
}
\ No newline at end of file
<?php
/**
* MemCacheServer class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* MemCacheServer represents the configuration data for a single memcache or memcached server.
*
* See [PHP manual](http://www.php.net/manual/en/function.Memcache-addServer.php) for detailed explanation
* of each configuration property.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class MemCacheServer extends \yii\base\Object
{
/**
* @var string memcache server hostname or IP address
*/
public $host;
/**
* @var integer memcache server port
*/
public $port = 11211;
/**
* @var integer probability of using this server among all servers.
*/
public $weight = 1;
/**
* @var boolean whether to use a persistent connection. This is used by memcache only.
*/
public $persistent = true;
/**
* @var integer value in seconds which will be used for connecting to the server. This is used by memcache only.
*/
public $timeout = 15;
/**
* @var integer how often a failed server will be retried (in seconds). This is used by memcache only.
*/
public $retryInterval = 15;
/**
* @var boolean if the server should be flagged as online upon a failure. This is used by memcache only.
*/
public $status = true;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment