From dcb531acdbb76e19190ae050925ea28258d89a17 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 28 May 2012 15:13:18 -0400 Subject: [PATCH] Finished more caching components. --- framework/caching/ApcCache.php | 34 +++----- framework/caching/DummyCache.php | 128 ++++++------------------------ framework/caching/EAcceleratorCache.php | 108 -------------------------- framework/caching/MemCache.php | 133 ++++++++++++++++---------------- framework/caching/WinCache.php | 38 +++------ framework/caching/XCache.php | 41 ++++------ framework/caching/ZendDataCache.php | 34 +++----- 7 files changed, 137 insertions(+), 379 deletions(-) delete mode 100644 framework/caching/EAcceleratorCache.php diff --git a/framework/caching/ApcCache.php b/framework/caching/ApcCache.php index 25d2f85..f92b02c 100644 --- a/framework/caching/ApcCache.php +++ b/framework/caching/ApcCache.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CApcCache extends CCache +class ApcCache extends Cache { /** - * Initializes this application component. - * This method is required by the {@link IApplicationComponent} interface. - * It checks the availability of memcache. - * @throws CException if APC cache extension is not loaded or is disabled. - */ - public function init() - { - parent::init(); - if(!extension_loaded('apc')) - throw new CException(Yii::t('yii','CApcCache requires PHP apc extension to be loaded.')); - } - - /** * Retrieves a value from cache with a specified key. * This is the implementation of the method declared in the parent class. * @param string $key a unique key identifying the cached value @@ -65,23 +51,22 @@ class CApcCache 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) { - return apc_store($key,$value,$expire); + return apc_store($key, $value, $expire); } /** * Stores a value identified by a key into cache if the cache does not contain this key. * This is the implementation of the method declared in the parent class. - * * @param string $key the key identifying the value to be cached * @param string $value the value to be cached * @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) { - return apc_add($key,$value,$expire); + return apc_add($key, $value, $expire); } /** @@ -99,7 +84,6 @@ class CApcCache extends CCache * Deletes all values from cache. * This is the implementation of the method declared in the parent class. * @return boolean whether the flush operation was successful. - * @since 1.1.5 */ protected function flushValues() { diff --git a/framework/caching/DummyCache.php b/framework/caching/DummyCache.php index 83a6b5f..9ef7950 100644 --- a/framework/caching/DummyCache.php +++ b/framework/caching/DummyCache.php @@ -1,6 +1,6 @@ cache is null or not. - * By replacing CDummyCache with some other cache component, one can quickly switch from + * DummyCache does not cache anything. It is provided so that one can always configure + * a 'cache' application component and save the check of existence of `\Yii::$application->cache`. + * By replacing DummyCache with some other cache component, one can quickly switch from * non-caching mode to caching mode. * * @author Qiang Xue * @since 2.0 */ -class CDummyCache extends CApplicationComponent implements ICache, ArrayAccess +class DummyCache extends Cache { /** - * @var string a string prefixed to every cache key so that it is unique. Defaults to {@link CApplication::getId() application ID}. - */ - public $keyPrefix; - - /** - * Initializes the application component. - * This method overrides the parent implementation by setting default cache key prefix. - */ - public function init() - { - parent::init(); - if($this->keyPrefix===null) - $this->keyPrefix=\Yii::$application->getId(); - } - - /** * Retrieves a value from cache with a specified key. - * @param string $id a key identifying the cached value - * @return mixed the value stored in cache, false if the value is not in the cache, expired or the dependency has changed. + * This is the implementation of the method declared in the parent class. + * @param string $key a unique key identifying the cached value + * @return string the value stored in cache, false if the value is not in the cache or expired. */ - public function get($id) + protected function getValue($key) { return false; } /** - * Retrieves multiple values from cache with the specified keys. - * Some caches (such as memcache, apc) allow retrieving multiple cached values at one time, - * which may improve the performance since it reduces the communication cost. - * In case a cache doesn't support this feature natively, it will be simulated by this method. - * @param array $ids list of keys identifying the cached values - * @return array list of cached values corresponding to the specified keys. The array - * is returned in terms of (key,value) pairs. - * If a value is not cached or expired, the corresponding array value will be false. - */ - public function mget($ids) - { - $results=array(); - foreach($ids as $id) - $results[$id]=false; - return $results; - } - - /** - * Stores a value identified by a key into cache. - * If the cache already contains such a key, the existing value and - * expiration time will be replaced with the new ones. + * Stores a value identified by a key in cache. + * This is the implementation of the method declared in the parent class. * - * @param string $id the key identifying the value to be cached - * @param mixed $value the value to be cached + * @param string $key the key identifying the value to be cached + * @param string $value the value to be cached * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. - * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid. * @return boolean true if the value is successfully stored into cache, false otherwise */ - public function set($id,$value,$expire=0,$dependency=null) + protected function setValue($key, $value, $expire) { return true; } /** * Stores a value identified by a key into cache if the cache does not contain this key. - * Nothing will be done if the cache already contains the key. - * @param string $id the key identifying the value to be cached - * @param mixed $value the value to be cached + * This is the implementation of the method declared in the parent class. + * @param string $key the key identifying the value to be cached + * @param string $value the value to be cached * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire. - * @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid. * @return boolean true if the value is successfully stored into cache, false otherwise */ - public function add($id,$value,$expire=0,$dependency=null) + protected function addValue($key, $value, $expire) { return true; } /** * Deletes a value with the specified key from cache - * @param string $id the key of the value to be deleted + * This is the implementation of the method declared in the parent class. + * @param string $key the key of the value to be deleted * @return boolean if no error happens during deletion */ - public function delete($id) + protected function deleteValue($key) { return true; } /** * Deletes all values from cache. - * Be careful of performing this operation if the cache is shared by multiple applications. + * This is the implementation of the method declared in the parent class. * @return boolean whether the flush operation was successful. - * @throws CException if this method is not overridden by child classes */ - public function flush() + protected function flushValues() { return true; } - - /** - * Returns whether there is a cache entry with a specified key. - * This method is required by the interface ArrayAccess. - * @param string $id a key identifying the cached value - * @return boolean - */ - public function offsetExists($id) - { - return false; - } - - /** - * Retrieves the value from cache with a specified key. - * This method is required by the interface ArrayAccess. - * @param string $id a key identifying the cached value - * @return mixed the value stored in cache, false if the value is not in the cache or expired. - */ - public function offsetGet($id) - { - return false; - } - - /** - * Stores the value identified by a key into cache. - * If the cache already contains such a key, the existing value will be - * replaced with the new ones. To add expiration and dependencies, use the set() method. - * This method is required by the interface ArrayAccess. - * @param string $id the key identifying the value to be cached - * @param mixed $value the value to be cached - */ - public function offsetSet($id, $value) - { - } - - /** - * Deletes the value with the specified key from cache - * This method is required by the interface ArrayAccess. - * @param string $id the key of the value to be deleted - * @return boolean if no error happens during deletion - */ - public function offsetUnset($id) - { - } } diff --git a/framework/caching/EAcceleratorCache.php b/framework/caching/EAcceleratorCache.php deleted file mode 100644 index ead2f04..0000000 --- a/framework/caching/EAcceleratorCache.php +++ /dev/null @@ -1,108 +0,0 @@ - - * @since 2.0 - */ -class CEAcceleratorCache extends CCache -{ - /** - * Initializes this application component. - * This method is required by the {@link IApplicationComponent} interface. - * It checks the availability of eAccelerator. - * @throws CException if eAccelerator extension is not loaded, is disabled or the cache functions are not compiled in. - */ - public function init() - { - parent::init(); - if(!function_exists('eaccelerator_get')) - throw new CException(Yii::t('yii','CEAcceleratorCache requires PHP eAccelerator extension to be loaded, enabled or compiled with the "--with-eaccelerator-shared-memory" option.')); - } - - /** - * Retrieves a value from cache with a specified key. - * This is the implementation of the method declared in the parent class. - * @param string $key a unique key identifying the cached value - * @return string the value stored in cache, false if the value is not in the cache or expired. - */ - protected function getValue($key) - { - $result = eaccelerator_get($key); - return $result !== NULL ? $result : false; - } - - /** - * Stores a value identified by a key in cache. - * This is the implementation of the method declared in the parent class. - * - * @param string $key the key identifying the value to be cached - * @param string $value the value to be cached - * @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) - { - return eaccelerator_put($key,$value,$expire); - } - - /** - * Stores a value identified by a key into cache if the cache does not contain this key. - * This is the implementation of the method declared in the parent class. - * - * @param string $key the key identifying the value to be cached - * @param string $value the value to be cached - * @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) - { - return (NULL === eaccelerator_get($key)) ? $this->setValue($key,$value,$expire) : false; - } - - /** - * Deletes a value with the specified key from cache - * This is the implementation of the method declared in the parent class. - * @param string $key the key of the value to be deleted - * @return boolean if no error happens during deletion - */ - protected function deleteValue($key) - { - return eaccelerator_rm($key); - } - - /** - * Deletes all values from cache. - * This is the implementation of the method declared in the parent class. - * @return boolean whether the flush operation was successful. - * @since 1.1.5 - */ - protected function flushValues() - { - // first, remove expired content from cache - eaccelerator_gc(); - // now, remove leftover cache-keys - $keys = eaccelerator_list_keys(); - foreach($keys as $key) - $this->deleteValue(substr($key['name'], 1)); - return true; - } -} diff --git a/framework/caching/MemCache.php b/framework/caching/MemCache.php index 18586a8..b026cac 100644 --- a/framework/caching/MemCache.php +++ b/framework/caching/MemCache.php @@ -1,6 +1,6 @@ * 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 * @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.')); } } \ No newline at end of file diff --git a/framework/caching/WinCache.php b/framework/caching/WinCache.php index f269c73..8e0b0fc 100644 --- a/framework/caching/WinCache.php +++ b/framework/caching/WinCache.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CWinCache extends CCache { - /** - * Initializes this application component. - * This method is required by the {@link IApplicationComponent} interface. - * It checks the availability of WinCache extension and WinCache user cache. - * @throws CException if WinCache extension is not loaded or user cache is disabled - */ - public function init() - { - parent::init(); - if(!extension_loaded('wincache')) - throw new CException(Yii::t('yii', 'CWinCache requires PHP wincache extension to be loaded.')); - if(!ini_get('wincache.ucenabled')) - throw new CException(Yii::t('yii', 'CWinCache user cache is disabled. Please set wincache.ucenabled to On in your php.ini.')); - } - +class WinCache extends Cache +{ /** * Retrieves a value from cache with a specified key. * This is the implementation of the method declared in the parent class. @@ -49,7 +36,7 @@ class CWinCache extends CCache { /** * Retrieves multiple values from cache with the specified keys. * @param array $keys a list of keys identifying the cached values - * @return array a list of cached values indexed by the keys + * @return array a list of cached values indexed by the keys */ protected function getValues($keys) { @@ -65,9 +52,9 @@ class CWinCache 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) { - return wincache_ucache_set($key,$value,$expire); + return wincache_ucache_set($key, $value, $expire); } /** @@ -79,9 +66,9 @@ class CWinCache 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) { - return wincache_ucache_add($key,$value,$expire); + return wincache_ucache_add($key, $value, $expire); } /** @@ -99,7 +86,6 @@ class CWinCache extends CCache { * Deletes all values from cache. * This is the implementation of the method declared in the parent class. * @return boolean whether the flush operation was successful. - * @since 1.1.5 */ protected function flushValues() { diff --git a/framework/caching/XCache.php b/framework/caching/XCache.php index 0814cb7..b911a01 100644 --- a/framework/caching/XCache.php +++ b/framework/caching/XCache.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CXCache extends CCache +class XCache extends Cache { /** - * Initializes this application component. - * This method is required by the {@link IApplicationComponent} interface. - * It checks the availability of memcache. - * @throws CException if memcache extension is not loaded or is disabled. - */ - public function init() - { - parent::init(); - if(!function_exists('xcache_isset')) - throw new CException(Yii::t('yii','CXCache requires PHP XCache extension to be loaded.')); - } - - /** * Retrieves a value from cache with a specified key. * This is the implementation of the method declared in the parent class. * @param string $key a unique key identifying the cached value @@ -55,9 +43,9 @@ class CXCache 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) { - return xcache_set($key,$value,$expire); + return xcache_set($key, $value, $expire); } /** @@ -69,9 +57,9 @@ class CXCache 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) { - return !xcache_isset($key) ? $this->setValue($key,$value,$expire) : false; + return !xcache_isset($key) ? $this->setValue($key, $value, $expire) : false; } /** @@ -89,14 +77,13 @@ class CXCache extends CCache * Deletes all values from cache. * This is the implementation of the method declared in the parent class. * @return boolean whether the flush operation was successful. - * @since 1.1.5 */ protected function flushValues() { - for($i=0, $max=xcache_count(XC_TYPE_VAR); $i<$max; $i++) - { - if(xcache_clear_cache(XC_TYPE_VAR, $i)===false) + for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) { + if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) { return false; + } } return true; } diff --git a/framework/caching/ZendDataCache.php b/framework/caching/ZendDataCache.php index 57402a2..bcaca56 100644 --- a/framework/caching/ZendDataCache.php +++ b/framework/caching/ZendDataCache.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class CZendDataCache extends CCache +class ZendDataCache extends Cache { /** - * Initializes this application component. - * This method is required by the {@link IApplicationComponent} interface. - * It checks the availability of Zend Data Cache. - * @throws CException if Zend Data Cache extension is not loaded. - */ - public function init() - { - parent::init(); - if(!function_exists('zend_shm_cache_store')) - throw new CException(Yii::t('yii','CZendDataCache requires PHP Zend Data Cache extension to be loaded.')); - } - - /** * Retrieves a value from cache with a specified key. * This is the implementation of the method declared in the parent class. * @param string $key a unique key identifying the cached value @@ -56,9 +43,9 @@ class CZendDataCache 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) { - return zend_shm_cache_store($key,$value,$expire); + return zend_shm_cache_store($key, $value, $expire); } /** @@ -70,9 +57,9 @@ class CZendDataCache 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) { - return (NULL === zend_shm_cache_fetch($key)) ? $this->setValue($key,$value,$expire) : false; + return zend_shm_cache_fetch($key) === null ? $this->setValue($key, $value, $expire) : false; } /** @@ -90,7 +77,6 @@ class CZendDataCache extends CCache * Deletes all values from cache. * This is the implementation of the method declared in the parent class. * @return boolean whether the flush operation was successful. - * @since 1.1.5 */ protected function flushValues() {