MemCache.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\caching;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * MemCache implements a cache application component based on [memcache](https://pecl.php.net/package/memcache)
  12. * and [memcached](https://pecl.php.net/package/memcached).
  13. *
  14. * MemCache supports both [memcache](https://pecl.php.net/package/memcache) and
  15. * [memcached](https://pecl.php.net/package/memcached). By setting [[useMemcached]] to be true or false,
  16. * one can let MemCache to use either memcached or memcache, respectively.
  17. *
  18. * MemCache can be configured with a list of memcache servers by settings its [[servers]] property.
  19. * By default, MemCache assumes there is a memcache server running on localhost at port 11211.
  20. *
  21. * See [[Cache]] for common cache operations that MemCache supports.
  22. *
  23. * Note, there is no security measure to protected data in memcache.
  24. * All data in memcache can be accessed by any process running in the system.
  25. *
  26. * To use MemCache as the cache application component, configure the application as follows,
  27. *
  28. * ```php
  29. * [
  30. * 'components' => [
  31. * 'cache' => [
  32. * 'class' => 'yii\caching\MemCache',
  33. * 'servers' => [
  34. * [
  35. * 'host' => 'server1',
  36. * 'port' => 11211,
  37. * 'weight' => 60,
  38. * ],
  39. * [
  40. * 'host' => 'server2',
  41. * 'port' => 11211,
  42. * 'weight' => 40,
  43. * ],
  44. * ],
  45. * ],
  46. * ],
  47. * ]
  48. * ```
  49. *
  50. * In the above, two memcache servers are used: server1 and server2. You can configure more properties of
  51. * each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options.
  52. *
  53. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  54. *
  55. * @property \Memcache|\Memcached $memcache The memcache (or memcached) object used by this cache component.
  56. * This property is read-only.
  57. * @property MemCacheServer[] $servers List of memcache server configurations. Note that the type of this
  58. * property differs in getter and setter. See [[getServers()]] and [[setServers()]] for details.
  59. *
  60. * @author Qiang Xue <qiang.xue@gmail.com>
  61. * @since 2.0
  62. */
  63. class MemCache extends Cache
  64. {
  65. /**
  66. * @var bool whether to use memcached or memcache as the underlying caching extension.
  67. * If true, [memcached](https://pecl.php.net/package/memcached) will be used.
  68. * If false, [memcache](https://pecl.php.net/package/memcache) will be used.
  69. * Defaults to false.
  70. */
  71. public $useMemcached = false;
  72. /**
  73. * @var string an ID that identifies a Memcached instance. This property is used only when [[useMemcached]] is true.
  74. * By default the Memcached instances are destroyed at the end of the request. To create an instance that
  75. * persists between requests, you may specify a unique ID for the instance. All instances created with the
  76. * same ID will share the same connection.
  77. * @see https://secure.php.net/manual/en/memcached.construct.php
  78. */
  79. public $persistentId;
  80. /**
  81. * @var array options for Memcached. This property is used only when [[useMemcached]] is true.
  82. * @see https://secure.php.net/manual/en/memcached.setoptions.php
  83. */
  84. public $options;
  85. /**
  86. * @var string memcached sasl username. This property is used only when [[useMemcached]] is true.
  87. * @see https://secure.php.net/manual/en/memcached.setsaslauthdata.php
  88. */
  89. public $username;
  90. /**
  91. * @var string memcached sasl password. This property is used only when [[useMemcached]] is true.
  92. * @see https://secure.php.net/manual/en/memcached.setsaslauthdata.php
  93. */
  94. public $password;
  95. /**
  96. * @var \Memcache|\Memcached the Memcache instance
  97. */
  98. private $_cache;
  99. /**
  100. * @var array list of memcache server configurations
  101. */
  102. private $_servers = [];
  103. /**
  104. * Initializes this application component.
  105. * It creates the memcache instance and adds memcache servers.
  106. */
  107. public function init()
  108. {
  109. parent::init();
  110. $this->addServers($this->getMemcache(), $this->getServers());
  111. }
  112. /**
  113. * Add servers to the server pool of the cache specified.
  114. *
  115. * @param \Memcache|\Memcached $cache
  116. * @param MemCacheServer[] $servers
  117. * @throws InvalidConfigException
  118. */
  119. protected function addServers($cache, $servers)
  120. {
  121. if (empty($servers)) {
  122. $servers = [new MemCacheServer([
  123. 'host' => '127.0.0.1',
  124. 'port' => 11211,
  125. ])];
  126. } else {
  127. foreach ($servers as $server) {
  128. if ($server->host === null) {
  129. throw new InvalidConfigException("The 'host' property must be specified for every memcache server.");
  130. }
  131. }
  132. }
  133. if ($this->useMemcached) {
  134. $this->addMemcachedServers($cache, $servers);
  135. } else {
  136. $this->addMemcacheServers($cache, $servers);
  137. }
  138. }
  139. /**
  140. * Add servers to the server pool of the cache specified
  141. * Used for memcached PECL extension.
  142. *
  143. * @param \Memcached $cache
  144. * @param MemCacheServer[] $servers
  145. */
  146. protected function addMemcachedServers($cache, $servers)
  147. {
  148. $existingServers = [];
  149. if ($this->persistentId !== null) {
  150. foreach ($cache->getServerList() as $s) {
  151. $existingServers[$s['host'] . ':' . $s['port']] = true;
  152. }
  153. }
  154. foreach ($servers as $server) {
  155. if (empty($existingServers) || !isset($existingServers[$server->host . ':' . $server->port])) {
  156. $cache->addServer($server->host, $server->port, $server->weight);
  157. }
  158. }
  159. }
  160. /**
  161. * Add servers to the server pool of the cache specified
  162. * Used for memcache PECL extension.
  163. *
  164. * @param \Memcache $cache
  165. * @param MemCacheServer[] $servers
  166. */
  167. protected function addMemcacheServers($cache, $servers)
  168. {
  169. $class = new \ReflectionClass($cache);
  170. $paramCount = $class->getMethod('addServer')->getNumberOfParameters();
  171. foreach ($servers as $server) {
  172. // $timeout is used for memcache versions that do not have $timeoutms parameter
  173. $timeout = (int) ($server->timeout / 1000) + (($server->timeout % 1000 > 0) ? 1 : 0);
  174. if ($paramCount === 9) {
  175. $cache->addserver(
  176. $server->host,
  177. $server->port,
  178. $server->persistent,
  179. $server->weight,
  180. $timeout,
  181. $server->retryInterval,
  182. $server->status,
  183. $server->failureCallback,
  184. $server->timeout
  185. );
  186. } else {
  187. $cache->addserver(
  188. $server->host,
  189. $server->port,
  190. $server->persistent,
  191. $server->weight,
  192. $timeout,
  193. $server->retryInterval,
  194. $server->status,
  195. $server->failureCallback
  196. );
  197. }
  198. }
  199. }
  200. /**
  201. * Returns the underlying memcache (or memcached) object.
  202. * @return \Memcache|\Memcached the memcache (or memcached) object used by this cache component.
  203. * @throws InvalidConfigException if memcache or memcached extension is not loaded
  204. */
  205. public function getMemcache()
  206. {
  207. if ($this->_cache === null) {
  208. $extension = $this->useMemcached ? 'memcached' : 'memcache';
  209. if (!extension_loaded($extension)) {
  210. throw new InvalidConfigException("MemCache requires PHP $extension extension to be loaded.");
  211. }
  212. if ($this->useMemcached) {
  213. $this->_cache = $this->persistentId !== null ? new \Memcached($this->persistentId) : new \Memcached();
  214. if ($this->username !== null || $this->password !== null) {
  215. $this->_cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
  216. $this->_cache->setSaslAuthData($this->username, $this->password);
  217. }
  218. if (!empty($this->options)) {
  219. $this->_cache->setOptions($this->options);
  220. }
  221. } else {
  222. $this->_cache = new \Memcache();
  223. }
  224. }
  225. return $this->_cache;
  226. }
  227. /**
  228. * Returns the memcache or memcached server configurations.
  229. * @return MemCacheServer[] list of memcache server configurations.
  230. */
  231. public function getServers()
  232. {
  233. return $this->_servers;
  234. }
  235. /**
  236. * @param array $config list of memcache or memcached server configurations. Each element must be an array
  237. * with the following keys: host, port, persistent, weight, timeout, retryInterval, status.
  238. * @see https://secure.php.net/manual/en/memcache.addserver.php
  239. * @see https://secure.php.net/manual/en/memcached.addserver.php
  240. */
  241. public function setServers($config)
  242. {
  243. foreach ($config as $c) {
  244. $this->_servers[] = new MemCacheServer($c);
  245. }
  246. }
  247. /**
  248. * Retrieves a value from cache with a specified key.
  249. * This is the implementation of the method declared in the parent class.
  250. * @param string $key a unique key identifying the cached value
  251. * @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
  252. */
  253. protected function getValue($key)
  254. {
  255. return $this->_cache->get($key);
  256. }
  257. /**
  258. * Retrieves multiple values from cache with the specified keys.
  259. * @param array $keys a list of keys identifying the cached values
  260. * @return array a list of cached values indexed by the keys
  261. */
  262. protected function getValues($keys)
  263. {
  264. return $this->useMemcached ? $this->_cache->getMulti($keys) : $this->_cache->get($keys);
  265. }
  266. /**
  267. * Stores a value identified by a key in cache.
  268. * This is the implementation of the method declared in the parent class.
  269. *
  270. * @param string $key the key identifying the value to be cached
  271. * @param mixed $value the value to be cached.
  272. * @see [Memcache::set()](https://secure.php.net/manual/en/memcache.set.php)
  273. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  274. * @return bool true if the value is successfully stored into cache, false otherwise
  275. */
  276. protected function setValue($key, $value, $duration)
  277. {
  278. // Use UNIX timestamp since it doesn't have any limitation
  279. // @see https://secure.php.net/manual/en/memcache.set.php
  280. // @see https://secure.php.net/manual/en/memcached.expiration.php
  281. $expire = $duration > 0 ? $duration + time() : 0;
  282. return $this->useMemcached ? $this->_cache->set($key, $value, $expire) : $this->_cache->set($key, $value, 0, $expire);
  283. }
  284. /**
  285. * Stores multiple key-value pairs in cache.
  286. * @param array $data array where key corresponds to cache key while value is the value stored
  287. * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
  288. * @return array array of failed keys.
  289. */
  290. protected function setValues($data, $duration)
  291. {
  292. if ($this->useMemcached) {
  293. // Use UNIX timestamp since it doesn't have any limitation
  294. // @see https://secure.php.net/manual/en/memcache.set.php
  295. // @see https://secure.php.net/manual/en/memcached.expiration.php
  296. $expire = $duration > 0 ? $duration + time() : 0;
  297. // Memcached::setMulti() returns boolean
  298. // @see https://secure.php.net/manual/en/memcached.setmulti.php
  299. return $this->_cache->setMulti($data, $expire) ? [] : array_keys($data);
  300. }
  301. return parent::setValues($data, $duration);
  302. }
  303. /**
  304. * Stores a value identified by a key into cache if the cache does not contain this key.
  305. * This is the implementation of the method declared in the parent class.
  306. *
  307. * @param string $key the key identifying the value to be cached
  308. * @param mixed $value the value to be cached
  309. * @see [Memcache::set()](https://secure.php.net/manual/en/memcache.set.php)
  310. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  311. * @return bool true if the value is successfully stored into cache, false otherwise
  312. */
  313. protected function addValue($key, $value, $duration)
  314. {
  315. // Use UNIX timestamp since it doesn't have any limitation
  316. // @see https://secure.php.net/manual/en/memcache.set.php
  317. // @see https://secure.php.net/manual/en/memcached.expiration.php
  318. $expire = $duration > 0 ? $duration + time() : 0;
  319. return $this->useMemcached ? $this->_cache->add($key, $value, $expire) : $this->_cache->add($key, $value, 0, $expire);
  320. }
  321. /**
  322. * Deletes a value with the specified key from cache
  323. * This is the implementation of the method declared in the parent class.
  324. * @param string $key the key of the value to be deleted
  325. * @return bool if no error happens during deletion
  326. */
  327. protected function deleteValue($key)
  328. {
  329. return $this->_cache->delete($key, 0);
  330. }
  331. /**
  332. * Deletes all values from cache.
  333. * This is the implementation of the method declared in the parent class.
  334. * @return bool whether the flush operation was successful.
  335. */
  336. protected function flushValues()
  337. {
  338. return $this->_cache->flush();
  339. }
  340. }