mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Finished more caching components.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* CMemCache class file
|
||||
* MemCache class file
|
||||
*
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright © 2008-2012 Yii Software LLC
|
||||
@ -10,23 +10,23 @@
|
||||
namespace yii\caching;
|
||||
|
||||
/**
|
||||
* CMemCache implements a cache application component based on {@link http://memcached.org/ memcached}.
|
||||
* MemCache implements a cache application component based on {@link http://memcached.org/ memcached}.
|
||||
*
|
||||
* CMemCache can be configured with a list of memcache servers by settings
|
||||
* its {@link setServers servers} property. By default, CMemCache assumes
|
||||
* MemCache can be configured with a list of memcache servers by settings
|
||||
* its {@link setServers servers} property. By default, MemCache assumes
|
||||
* there is a memcache server running on localhost at port 11211.
|
||||
*
|
||||
* See {@link CCache} manual for common cache operations that are supported by CMemCache.
|
||||
* See {@link CCache} manual for common cache operations that are supported by 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.
|
||||
*
|
||||
* To use CMemCache 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(
|
||||
* 'components'=>array(
|
||||
* 'cache'=>array(
|
||||
* 'class'=>'CMemCache',
|
||||
* 'class'=>'MemCache',
|
||||
* 'servers'=>array(
|
||||
* array(
|
||||
* 'host'=>'server1',
|
||||
@ -49,32 +49,32 @@ namespace yii\caching;
|
||||
* See {@link http://www.php.net/manual/en/function.memcache-addserver.php}
|
||||
* for more details.
|
||||
*
|
||||
* CMemCache can also be used with {@link http://pecl.php.net/package/memcached memcached}.
|
||||
* MemCache can also be used with {@link http://pecl.php.net/package/memcached memcached}.
|
||||
* To do so, set {@link useMemcached} to be true.
|
||||
*
|
||||
* @property mixed $memCache The memcache instance (or memcached if {@link useMemcached} is true) used by this component.
|
||||
* @property array $servers List of memcache server configurations. Each element is a {@link CMemCacheServerConfiguration}.
|
||||
* @property array $servers List of memcache server configurations. Each element is a {@link MemCacheServerConfiguration}.
|
||||
*
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class CMemCache extends CCache
|
||||
class MemCache extends Cache
|
||||
{
|
||||
/**
|
||||
* @var boolean whether to use memcached or memcache as the underlying caching extension.
|
||||
* If true {@link http://pecl.php.net/package/memcached memcached} will be used.
|
||||
* If false {@link http://pecl.php.net/package/memcache memcache}. will be used.
|
||||
* If true, [memcached](http://pecl.php.net/package/memcached) will be used.
|
||||
* If false, [memcache](http://pecl.php.net/package/memcache) will be used.
|
||||
* Defaults to false.
|
||||
*/
|
||||
public $useMemcached=false;
|
||||
public $useMemcached = false;
|
||||
/**
|
||||
* @var Memcache the Memcache instance
|
||||
*/
|
||||
private $_cache=null;
|
||||
private $_cache = null;
|
||||
/**
|
||||
* @var array list of memcache server configurations
|
||||
*/
|
||||
private $_servers=array();
|
||||
private $_servers = array();
|
||||
|
||||
/**
|
||||
* Initializes this application component.
|
||||
@ -85,20 +85,19 @@ class CMemCache extends CCache
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
$servers=$this->getServers();
|
||||
$cache=$this->getMemCache();
|
||||
if(count($servers))
|
||||
{
|
||||
foreach($servers as $server)
|
||||
{
|
||||
if($this->useMemcached)
|
||||
$cache->addServer($server->host,$server->port,$server->weight);
|
||||
else
|
||||
$cache->addServer($server->host,$server->port,$server->persistent,$server->weight,$server->timeout,$server->status);
|
||||
$servers = $this->getServers();
|
||||
$cache = $this->getMemCache();
|
||||
if (count($servers)) {
|
||||
foreach ($servers as $server) {
|
||||
if ($this->useMemcached) {
|
||||
$cache->addServer($server->host, $server->port, $server->weight);
|
||||
} else {
|
||||
$cache->addServer($server->host, $server->port, $server->persistent, $server->weight, $server->timeout, $server->status);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$cache->addServer('localhost', 11211);
|
||||
}
|
||||
else
|
||||
$cache->addServer('localhost',11211);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -107,19 +106,19 @@ class CMemCache extends CCache
|
||||
*/
|
||||
public function getMemCache()
|
||||
{
|
||||
if($this->_cache!==null)
|
||||
if ($this->_cache !== null) {
|
||||
return $this->_cache;
|
||||
else
|
||||
{
|
||||
$extension=$this->useMemcached ? 'memcached' : 'memcache';
|
||||
if(!extension_loaded($extension))
|
||||
throw new CException(Yii::t('yii',"CMemCache requires PHP $extension extension to be loaded."));
|
||||
return $this->_cache=$this->useMemcached ? new Memcached : new Memcache;
|
||||
} else {
|
||||
$extension = $this->useMemcached ? 'memcached' : 'memcache';
|
||||
if (!extension_loaded($extension)) {
|
||||
throw new CException(Yii::t('yii', "MemCache requires PHP $extension extension to be loaded."));
|
||||
}
|
||||
return $this->_cache = $this->useMemcached ? new Memcached : new Memcache;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array list of memcache server configurations. Each element is a {@link CMemCacheServerConfiguration}.
|
||||
* @return array list of memcache server configurations. Each element is a {@link MemCacheServerConfiguration}.
|
||||
*/
|
||||
public function getServers()
|
||||
{
|
||||
@ -133,8 +132,9 @@ class CMemCache extends CCache
|
||||
*/
|
||||
public function setServers($config)
|
||||
{
|
||||
foreach($config as $c)
|
||||
$this->_servers[]=new CMemCacheServerConfiguration($c);
|
||||
foreach ($config as $c) {
|
||||
$this->_servers[] = new MemCacheServerConfiguration($c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -167,14 +167,15 @@ class CMemCache extends CCache
|
||||
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
|
||||
* @return boolean true if the value is successfully stored into cache, false otherwise
|
||||
*/
|
||||
protected function setValue($key,$value,$expire)
|
||||
protected function setValue($key, $value, $expire)
|
||||
{
|
||||
if($expire>0)
|
||||
$expire+=time();
|
||||
else
|
||||
$expire=0;
|
||||
if ($expire > 0) {
|
||||
$expire += time();
|
||||
} else {
|
||||
$expire = 0;
|
||||
}
|
||||
|
||||
return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
|
||||
return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -186,14 +187,15 @@ class CMemCache extends CCache
|
||||
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
|
||||
* @return boolean true if the value is successfully stored into cache, false otherwise
|
||||
*/
|
||||
protected function addValue($key,$value,$expire)
|
||||
protected function addValue($key, $value, $expire)
|
||||
{
|
||||
if($expire>0)
|
||||
$expire+=time();
|
||||
else
|
||||
$expire=0;
|
||||
if ($expire > 0) {
|
||||
$expire += time();
|
||||
} else {
|
||||
$expire = 0;
|
||||
}
|
||||
|
||||
return $this->useMemcached ? $this->_cache->add($key,$value,$expire) : $this->_cache->add($key,$value,0,$expire);
|
||||
return $this->useMemcached ? $this->_cache->add($key, $value, $expire) : $this->_cache->add($key, $value, 0, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -220,7 +222,7 @@ class CMemCache extends CCache
|
||||
}
|
||||
|
||||
/**
|
||||
* CMemCacheServerConfiguration represents the configuration data for a single memcache server.
|
||||
* 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.
|
||||
@ -230,7 +232,7 @@ class CMemCache extends CCache
|
||||
* @package system.caching
|
||||
* @since 1.0
|
||||
*/
|
||||
class CMemCacheServerConfiguration extends CComponent
|
||||
class MemCacheServerConfiguration extends CComponent
|
||||
{
|
||||
/**
|
||||
* @var string memcache server hostname or IP address
|
||||
@ -239,27 +241,27 @@ class CMemCacheServerConfiguration extends CComponent
|
||||
/**
|
||||
* @var integer memcache server port
|
||||
*/
|
||||
public $port=11211;
|
||||
public $port = 11211;
|
||||
/**
|
||||
* @var boolean whether to use a persistent connection
|
||||
*/
|
||||
public $persistent=true;
|
||||
public $persistent = true;
|
||||
/**
|
||||
* @var integer probability of using this server among all servers.
|
||||
*/
|
||||
public $weight=1;
|
||||
public $weight = 1;
|
||||
/**
|
||||
* @var integer value in seconds which will be used for connecting to the server
|
||||
*/
|
||||
public $timeout=15;
|
||||
public $timeout = 15;
|
||||
/**
|
||||
* @var integer how often a failed server will be retried (in seconds)
|
||||
*/
|
||||
public $retryInterval=15;
|
||||
public $retryInterval = 15;
|
||||
/**
|
||||
* @var boolean if the server should be flagged as online upon a failure
|
||||
*/
|
||||
public $status=true;
|
||||
public $status = true;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -268,14 +270,15 @@ class CMemCacheServerConfiguration extends CComponent
|
||||
*/
|
||||
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','CMemCache server configuration must have "host" value.'));
|
||||
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.'));
|
||||
}
|
||||
else
|
||||
throw new CException(Yii::t('yii','CMemCache server configuration must be an array.'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user