CMemCache.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * CMemCache class file
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CMemCache implements a cache application component based on {@link http://memcached.org/ memcached}.
  12. *
  13. * CMemCache can be configured with a list of memcache servers by settings
  14. * its {@link setServers servers} property. By default, CMemCache assumes
  15. * there is a memcache server running on localhost at port 11211.
  16. *
  17. * See {@link CCache} manual for common cache operations that are supported by CMemCache.
  18. *
  19. * Note, there is no security measure to protected data in memcache.
  20. * All data in memcache can be accessed by any process running in the system.
  21. *
  22. * To use CMemCache as the cache application component, configure the application as follows,
  23. * <pre>
  24. * array(
  25. * 'components'=>array(
  26. * 'cache'=>array(
  27. * 'class'=>'CMemCache',
  28. * 'servers'=>array(
  29. * array(
  30. * 'host'=>'server1',
  31. * 'port'=>11211,
  32. * 'weight'=>60,
  33. * ),
  34. * array(
  35. * 'host'=>'server2',
  36. * 'port'=>11211,
  37. * 'weight'=>40,
  38. * ),
  39. * ),
  40. * ),
  41. * ),
  42. * )
  43. * </pre>
  44. * In the above, two memcache servers are used: server1 and server2.
  45. * You can configure more properties of every server, including:
  46. * host, port, persistent, weight, timeout, retryInterval, status.
  47. * See {@link http://www.php.net/manual/en/function.memcache-addserver.php}
  48. * for more details.
  49. *
  50. * CMemCache can also be used with {@link http://pecl.php.net/package/memcached memcached}.
  51. * To do so, set {@link useMemcached} to be true.
  52. *
  53. * @property mixed $memCache The memcache instance (or memcached if {@link useMemcached} is true) used by this component.
  54. * @property array $servers List of memcache server configurations. Each element is a {@link CMemCacheServerConfiguration}.
  55. *
  56. * @author Qiang Xue <qiang.xue@gmail.com>
  57. * @package system.caching
  58. * @since 1.0
  59. */
  60. class CMemCache extends CCache
  61. {
  62. /**
  63. * @var boolean whether to use memcached or memcache as the underlying caching extension.
  64. * If true {@link http://pecl.php.net/package/memcached memcached} will be used.
  65. * If false {@link http://pecl.php.net/package/memcache memcache}. will be used.
  66. * Defaults to false.
  67. */
  68. public $useMemcached=false;
  69. /**
  70. * @var Memcache the Memcache instance
  71. */
  72. private $_cache=null;
  73. /**
  74. * @var array list of memcache server configurations
  75. */
  76. private $_servers=array();
  77. /**
  78. * Initializes this application component.
  79. * This method is required by the {@link IApplicationComponent} interface.
  80. * It creates the memcache instance and adds memcache servers.
  81. * @throws CException if memcache extension is not loaded
  82. */
  83. public function init()
  84. {
  85. parent::init();
  86. $servers=$this->getServers();
  87. $cache=$this->getMemCache();
  88. if(count($servers))
  89. {
  90. foreach($servers as $server)
  91. {
  92. if($this->useMemcached)
  93. $cache->addServer($server->host,$server->port,$server->weight);
  94. else
  95. $cache->addServer($server->host,$server->port,$server->persistent,$server->weight,$server->timeout,$server->retryInterval,$server->status);
  96. }
  97. }
  98. else
  99. $cache->addServer('localhost',11211);
  100. }
  101. /**
  102. * @throws CException if extension isn't loaded
  103. * @return Memcache|Memcached the memcache instance (or memcached if {@link useMemcached} is true) used by this component.
  104. */
  105. public function getMemCache()
  106. {
  107. if($this->_cache!==null)
  108. return $this->_cache;
  109. else
  110. {
  111. $extension=$this->useMemcached ? 'memcached' : 'memcache';
  112. if(!extension_loaded($extension))
  113. throw new CException(Yii::t('yii',"CMemCache requires PHP {extension} extension to be loaded.",
  114. array('{extension}'=>$extension)));
  115. return $this->_cache=$this->useMemcached ? new Memcached : new Memcache;
  116. }
  117. }
  118. /**
  119. * @return array list of memcache server configurations. Each element is a {@link CMemCacheServerConfiguration}.
  120. */
  121. public function getServers()
  122. {
  123. return $this->_servers;
  124. }
  125. /**
  126. * @param array $config list of memcache server configurations. Each element must be an array
  127. * with the following keys: host, port, persistent, weight, timeout, retryInterval, status.
  128. * @see http://www.php.net/manual/en/function.Memcache-addServer.php
  129. */
  130. public function setServers($config)
  131. {
  132. foreach($config as $c)
  133. $this->_servers[]=new CMemCacheServerConfiguration($c);
  134. }
  135. /**
  136. * Retrieves a value from cache with a specified key.
  137. * This is the implementation of the method declared in the parent class.
  138. * @param string $key a unique key identifying the cached value
  139. * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
  140. */
  141. protected function getValue($key)
  142. {
  143. return $this->_cache->get($key);
  144. }
  145. /**
  146. * Retrieves multiple values from cache with the specified keys.
  147. * @param array $keys a list of keys identifying the cached values
  148. * @return array a list of cached values indexed by the keys
  149. */
  150. protected function getValues($keys)
  151. {
  152. return $this->useMemcached ? $this->_cache->getMulti($keys) : $this->_cache->get($keys);
  153. }
  154. /**
  155. * Stores a value identified by a key in cache.
  156. * This is the implementation of the method declared in the parent class.
  157. *
  158. * @param string $key the key identifying the value to be cached
  159. * @param string $value the value to be cached
  160. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  161. * @return boolean true if the value is successfully stored into cache, false otherwise
  162. */
  163. protected function setValue($key,$value,$expire)
  164. {
  165. if($expire>0)
  166. $expire+=time();
  167. else
  168. $expire=0;
  169. return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
  170. }
  171. /**
  172. * Stores a value identified by a key into cache if the cache does not contain this key.
  173. * This is the implementation of the method declared in the parent class.
  174. *
  175. * @param string $key the key identifying the value to be cached
  176. * @param string $value the value to be cached
  177. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  178. * @return boolean true if the value is successfully stored into cache, false otherwise
  179. */
  180. protected function addValue($key,$value,$expire)
  181. {
  182. if($expire>0)
  183. $expire+=time();
  184. else
  185. $expire=0;
  186. return $this->useMemcached ? $this->_cache->add($key,$value,$expire) : $this->_cache->add($key,$value,0,$expire);
  187. }
  188. /**
  189. * Deletes a value with the specified key from cache
  190. * This is the implementation of the method declared in the parent class.
  191. * @param string $key the key of the value to be deleted
  192. * @return boolean if no error happens during deletion
  193. */
  194. protected function deleteValue($key)
  195. {
  196. return $this->_cache->delete($key, 0);
  197. }
  198. /**
  199. * Deletes all values from cache.
  200. * This is the implementation of the method declared in the parent class.
  201. * @return boolean whether the flush operation was successful.
  202. * @since 1.1.5
  203. */
  204. protected function flushValues()
  205. {
  206. return $this->_cache->flush();
  207. }
  208. }
  209. /**
  210. * CMemCacheServerConfiguration represents the configuration data for a single memcache server.
  211. *
  212. * See {@link http://www.php.net/manual/en/function.Memcache-addServer.php}
  213. * for detailed explanation of each configuration property.
  214. *
  215. * @author Qiang Xue <qiang.xue@gmail.com>
  216. * @package system.caching
  217. * @since 1.0
  218. */
  219. class CMemCacheServerConfiguration extends CComponent
  220. {
  221. /**
  222. * @var string memcache server hostname or IP address
  223. */
  224. public $host;
  225. /**
  226. * @var integer memcache server port
  227. */
  228. public $port=11211;
  229. /**
  230. * @var boolean whether to use a persistent connection
  231. */
  232. public $persistent=true;
  233. /**
  234. * @var integer probability of using this server among all servers.
  235. */
  236. public $weight=1;
  237. /**
  238. * @var integer value in seconds which will be used for connecting to the server
  239. */
  240. public $timeout=15;
  241. /**
  242. * @var integer how often a failed server will be retried (in seconds)
  243. */
  244. public $retryInterval=15;
  245. /**
  246. * @var boolean if the server should be flagged as online upon a failure
  247. */
  248. public $status=true;
  249. /**
  250. * Constructor.
  251. * @param array $config list of memcache server configurations.
  252. * @throws CException if the configuration is not an array
  253. */
  254. public function __construct($config)
  255. {
  256. if(is_array($config))
  257. {
  258. foreach($config as $key=>$value)
  259. $this->$key=$value;
  260. if($this->host===null)
  261. throw new CException(Yii::t('yii','CMemCache server configuration must have "host" value.'));
  262. }
  263. else
  264. throw new CException(Yii::t('yii','CMemCache server configuration must be an array.'));
  265. }
  266. }