Browse Source

Finished more caching components.

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
dcb531acdb
  1. 26
      framework/caching/ApcCache.php
  2. 128
      framework/caching/DummyCache.php
  3. 108
      framework/caching/EAcceleratorCache.php
  4. 83
      framework/caching/MemCache.php
  5. 26
      framework/caching/WinCache.php
  6. 33
      framework/caching/XCache.php
  7. 28
      framework/caching/ZendDataCache.php

26
framework/caching/ApcCache.php

@ -1,6 +1,6 @@
<?php
/**
* CApcCache class file
* ApcCache class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,32 +10,18 @@
namespace yii\caching;
/**
* CApcCache provides APC caching in terms of an application component.
* ApcCache provides APC caching in terms of an application component.
*
* The caching is based on {@link http://www.php.net/apc APC}.
* To use this application component, the APC PHP extension must be loaded.
* To use this application component, the [APC PHP extension](http://www.php.net/apc) must be loaded.
*
* See {@link CCache} manual for common cache operations that are supported by CApcCache.
* See [[Cache]] for common cache operations that ApcCache supports.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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
@ -73,7 +59,6 @@ class CApcCache extends CCache
/**
* 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.
@ -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()
{

128
framework/caching/DummyCache.php

@ -1,6 +1,6 @@
<?php
/**
* CDummyCache class file.
* DummyCache class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,154 +10,74 @@
namespace yii\caching;
/**
* CDummyCache is a placeholder cache component.
* DummyCache is a placeholder cache component.
*
* CDummyCache does not cache anything. It is provided so that one can always configure
* a 'cache' application component and he does not need to check if \Yii::$application->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 <qiang.xue@gmail.com>
* @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)
{
}
}

108
framework/caching/EAcceleratorCache.php

@ -1,108 +0,0 @@
<?php
/**
* CEAcceleratorCache class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* CEAcceleratorCache implements a cache application module based on {@link http://eaccelerator.net/ eaccelerator}.
*
* To use this application component, the eAccelerator PHP extension must be loaded.
*
* See {@link CCache} manual for common cache operations that are supported by CEAccelerator.
*
* Please note that as of v0.9.6, eAccelerator no longer supports data caching.
* This means if you still want to use this component, your eAccelerator should be of 0.9.5.x or lower version.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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;
}
}

83
framework/caching/MemCache.php

@ -1,6 +1,6 @@
<?php
/**
* CMemCache class file
* MemCache class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 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,21 +49,21 @@ 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;
@ -87,19 +87,18 @@ class CMemCache extends CCache
parent::init();
$servers = $this->getServers();
$cache = $this->getMemCache();
if(count($servers))
{
foreach($servers as $server)
{
if($this->useMemcached)
if (count($servers)) {
foreach ($servers as $server) {
if ($this->useMemcached) {
$cache->addServer($server->host, $server->port, $server->weight);
else
} else {
$cache->addServer($server->host, $server->port, $server->persistent, $server->weight, $server->timeout, $server->status);
}
}
else
} else {
$cache->addServer('localhost', 11211);
}
}
/**
* @throws CException if extension isn't loaded
@ -107,19 +106,19 @@ class CMemCache extends CCache
*/
public function getMemCache()
{
if($this->_cache!==null)
if ($this->_cache !== null) {
return $this->_cache;
else
{
} else {
$extension = $this->useMemcached ? 'memcached' : 'memcache';
if(!extension_loaded($extension))
throw new CException(Yii::t('yii',"CMemCache requires PHP $extension extension to be loaded."));
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);
}
}
/**
@ -169,10 +169,11 @@ class CMemCache extends CCache
*/
protected function setValue($key, $value, $expire)
{
if($expire>0)
if ($expire > 0) {
$expire += time();
else
} else {
$expire = 0;
}
return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $expire);
}
@ -188,10 +189,11 @@ class CMemCache extends CCache
*/
protected function addValue($key, $value, $expire)
{
if($expire>0)
if ($expire > 0) {
$expire += time();
else
} else {
$expire = 0;
}
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
@ -268,14 +270,15 @@ class CMemCacheServerConfiguration extends CComponent
*/
public function __construct($config)
{
if(is_array($config))
{
foreach($config as $key=>$value)
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.'));
}
else
throw new CException(Yii::t('yii','CMemCache server configuration must be an array.'));
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.'));
}
}
}

26
framework/caching/WinCache.php

@ -1,6 +1,6 @@
<?php
/**
* CWinCache class file
* WinCache class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,31 +10,18 @@
namespace yii\caching;
/**
* CWinCache implements a cache application component based on {@link http://www.iis.net/expand/wincacheforphp WinCache}.
* WinCache provides XCache caching in terms of an application component.
*
* To use this application component, the WinCache PHP extension must be loaded.
* To use this application component, the [WinCache PHP extension](http://www.iis.net/expand/wincacheforphp)
* must be loaded. Also note that "wincache.ucenabled" should be set to "On" in your php.ini file.
*
* See {@link CCache} manual for common cache operations that are supported by CWinCache.
* See {@link CCache} manual for common cache operations that are supported by WinCache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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()
class WinCache extends Cache
{
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.'));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
@ -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()
{

33
framework/caching/XCache.php

@ -1,6 +1,6 @@
<?php
/**
* CXCache class file
* XCache class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,32 +10,20 @@
namespace yii\caching;
/**
* CXCache implements a cache application module based on {@link http://xcache.lighttpd.net/ xcache}.
* XCache provides XCache caching in terms of an application component.
*
* To use this application component, the XCache PHP extension must be loaded.
* Flush functionality will only work correctly if "xcache.admin.enable_auth" is set to "Off" in php.ini.
* To use this application component, the [XCache PHP extension](http://xcache.lighttpd.net/) must be loaded.
* Also note that the [[flush()]] functionality will work correctly only if "xcache.admin.enable_auth"
* is set to "Off" in php.ini.
*
* See {@link CCache} manual for common cache operations that are supported by CXCache.
* See [[Cache]] for common cache operations that XCache supports.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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
@ -89,15 +77,14 @@ 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;
}
}

28
framework/caching/ZendDataCache.php

@ -1,6 +1,6 @@
<?php
/**
* CZendDataCache class file
* ZendDataCache class file
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -10,32 +10,19 @@
namespace yii\caching;
/**
* CZendDataCache implements a cache application module based on the Zend Data Cache
* delivered with {@link http://www.zend.com/en/products/server/ ZendServer}.
* ZendDataCache provides Zend data caching in terms of an application component.
*
* To use this application component, the Zend Data Cache PHP extension must be loaded.
* To use this application component, the [Zend Data Cache PHP extensionn](http://www.zend.com/en/products/server/)
* must be loaded.
*
* See {@link CCache} manual for common cache operations that are supported by CZendDataCache.
* See [[Cache]] for common cache operations that ZendDataCache supports.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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
@ -72,7 +59,7 @@ class CZendDataCache extends CCache
*/
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()
{

Loading…
Cancel
Save