You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					225 lines
				
				7.3 KiB
			
		
		
			
		
	
	
					225 lines
				
				7.3 KiB
			| 
								 
											14 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								namespace yii\caching;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use yii\base\InvalidConfigException;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * 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.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * MemCache can be configured with a list of memcache servers by settings its [[servers]] property.
							 | 
						||
| 
								 | 
							
								 * By default, MemCache assumes there is a memcache server running on localhost at port 11211.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * See [[Cache]] for common cache operations that MemCache supports.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * 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.
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * To use MemCache as the cache application component, configure the application as follows,
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * ~~~
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * array(
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *     'components' => array(
							 | 
						||
| 
								 | 
							
								 *         'cache' => array(
							 | 
						||
| 
								 | 
							
								 *             'class' => 'MemCache',
							 | 
						||
| 
								 | 
							
								 *             'servers' => array(
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *                 array(
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *                     'host' => 'server1',
							 | 
						||
| 
								 | 
							
								 *                     'port' => 11211,
							 | 
						||
| 
								 | 
							
								 *                     'weight' => 60,
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *                 ),
							 | 
						||
| 
								 | 
							
								 *                 array(
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *                     'host' => 'server2',
							 | 
						||
| 
								 | 
							
								 *                     'port' => 11211,
							 | 
						||
| 
								 | 
							
								 *                     'weight' => 40,
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *                 ),
							 | 
						||
| 
								 | 
							
								 *             ),
							 | 
						||
| 
								 | 
							
								 *         ),
							 | 
						||
| 
								 | 
							
								 *     ),
							 | 
						||
| 
								 | 
							
								 * )
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * ~~~
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * In the above, two memcache servers are used: server1 and server2. You can configure more properties of
							 | 
						||
| 
								 | 
							
								 * each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * @property \Memcache|\Memcached $memCache The memcache instance (or memcached if [[useMemcached]] is true) used by this component.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * @property MemCacheServer[] $servers List of memcache server configurations.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @author Qiang Xue <qiang.xue@gmail.com>
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 * @since 2.0
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								class MemCache extends Cache
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @var boolean whether to use memcached or memcache as the underlying caching extension.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 * If true, [memcached](http://pecl.php.net/package/memcached) will be used.
							 | 
						||
| 
								 | 
							
									 * If false, [memcache](http://pecl.php.net/package/memcache) will be used.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 * Defaults to false.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									public $useMemcached = false;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 * @var \Memcache|\Memcached the Memcache instance
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									private $_cache = null;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @var array list of memcache server configurations
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									private $_servers = array();
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Initializes this application component.
							 | 
						||
| 
								 | 
							
									 * It creates the memcache instance and adds memcache servers.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function init()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										parent::init();
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										$servers = $this->getServers();
							 | 
						||
| 
								 | 
							
										$cache = $this->getMemCache();
							 | 
						||
| 
								 | 
							
										if (count($servers)) {
							 | 
						||
| 
								 | 
							
											foreach ($servers as $server) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												if ($server->host === null) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													throw new InvalidConfigException("The 'host' property must be specified for every memcache server.");
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												if ($this->useMemcached) {
							 | 
						||
| 
								 | 
							
													$cache->addServer($server->host, $server->port, $server->weight);
							 | 
						||
| 
								 | 
							
												} else {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
													$cache->addServer($server->host, $server->port, $server->persistent,
							 | 
						||
| 
								 | 
							
														$server->weight, $server->timeout, $server->retryInterval, $server->status);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
												}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										} else {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											$cache->addServer('127.0.0.1', 11211);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 * Returns the underlying memcache (or memcached) object.
							 | 
						||
| 
								 | 
							
									 * @return \Memcache|\Memcached the memcache (or memcached) object used by this cache component.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @throws InvalidConfigException if memcache or memcached extension is not loaded
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function getMemcache()
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										if ($this->_cache === null) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											$extension = $this->useMemcached ? 'memcached' : 'memcache';
							 | 
						||
| 
								 | 
							
											if (!extension_loaded($extension)) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												throw new InvalidConfigException("MemCache requires PHP $extension extension to be loaded.");
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											$this->_cache = $this->useMemcached ? new \Memcached : new \Memcache;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										return $this->_cache;
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 * Returns the memcache server configurations.
							 | 
						||
| 
								 | 
							
									 * @return MemCacheServer[] list of memcache server configurations.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getServers()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return $this->_servers;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @param array $config list of memcache server configurations. Each element must be an array
							 | 
						||
| 
								 | 
							
									 * with the following keys: host, port, persistent, weight, timeout, retryInterval, status.
							 | 
						||
| 
								 | 
							
									 * @see http://www.php.net/manual/en/function.Memcache-addServer.php
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function setServers($config)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										foreach ($config as $c) {
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
											$this->_servers[] = new MemCacheServer($c);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * 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
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function getValue($key)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return $this->_cache->get($key);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * 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
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function getValues($keys)
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return $this->useMemcached ? $this->_cache->getMulti($keys) : $this->_cache->get($keys);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * 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
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									protected function setValue($key, $value, $expire)
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										if ($expire > 0) {
							 | 
						||
| 
								 | 
							
											$expire += time();
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											$expire = 0;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $expire);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * 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
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									protected function addValue($key, $value, $expire)
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										if ($expire > 0) {
							 | 
						||
| 
								 | 
							
											$expire += time();
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											$expire = 0;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
										return $this->useMemcached ? $this->_cache->add($key, $value, $expire) : $this->_cache->add($key, $value, 0, $expire);
							 | 
						||
| 
								 
											14 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * 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 $this->_cache->delete($key, 0);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * 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.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									protected function flushValues()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return $this->_cache->flush();
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |