ApcCache.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\base\InvalidConfigException;
  9. /**
  10. * ApcCache provides APC caching in terms of an application component.
  11. *
  12. * To use this application component, the [APC PHP extension](https://secure.php.net/apc) must be loaded.
  13. * Alternatively [APCu PHP extension](https://secure.php.net/apcu) could be used via setting `useApcu` to `true`.
  14. * In order to enable APC or APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
  15. *
  16. * See [[Cache]] for common cache operations that ApcCache supports.
  17. *
  18. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @since 2.0
  22. */
  23. class ApcCache extends Cache
  24. {
  25. /**
  26. * @var bool whether to use apcu or apc as the underlying caching extension.
  27. * If true, [apcu](https://pecl.php.net/package/apcu) will be used.
  28. * If false, [apc](https://pecl.php.net/package/apc) will be used.
  29. * Defaults to false.
  30. * @since 2.0.7
  31. */
  32. public $useApcu = false;
  33. /**
  34. * Initializes this application component.
  35. * It checks if extension required is loaded.
  36. */
  37. public function init()
  38. {
  39. parent::init();
  40. $extension = $this->useApcu ? 'apcu' : 'apc';
  41. if (!extension_loaded($extension)) {
  42. throw new InvalidConfigException("ApcCache requires PHP $extension extension to be loaded.");
  43. }
  44. }
  45. /**
  46. * Checks whether a specified key exists in the cache.
  47. * This can be faster than getting the value from the cache if the data is big.
  48. * Note that this method does not check whether the dependency associated
  49. * with the cached data, if there is any, has changed. So a call to [[get]]
  50. * may return false while exists returns true.
  51. * @param mixed $key a key identifying the cached value. This can be a simple string or
  52. * a complex data structure consisting of factors representing the key.
  53. * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
  54. */
  55. public function exists($key)
  56. {
  57. $key = $this->buildKey($key);
  58. return $this->useApcu ? apcu_exists($key) : apc_exists($key);
  59. }
  60. /**
  61. * Retrieves a value from cache with a specified key.
  62. * This is the implementation of the method declared in the parent class.
  63. * @param string $key a unique key identifying the cached value
  64. * @return mixed|false the value stored in cache, false if the value is not in the cache or expired.
  65. */
  66. protected function getValue($key)
  67. {
  68. return $this->useApcu ? apcu_fetch($key) : apc_fetch($key);
  69. }
  70. /**
  71. * Retrieves multiple values from cache with the specified keys.
  72. * @param array $keys a list of keys identifying the cached values
  73. * @return array a list of cached values indexed by the keys
  74. */
  75. protected function getValues($keys)
  76. {
  77. $values = $this->useApcu ? apcu_fetch($keys) : apc_fetch($keys);
  78. return is_array($values) ? $values : [];
  79. }
  80. /**
  81. * Stores a value identified by a key in cache.
  82. * This is the implementation of the method declared in the parent class.
  83. *
  84. * @param string $key the key identifying the value to be cached
  85. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  86. * it could be something else.
  87. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  88. * @return bool true if the value is successfully stored into cache, false otherwise.
  89. */
  90. protected function setValue($key, $value, $duration)
  91. {
  92. return $this->useApcu ? apcu_store($key, $value, $duration) : apc_store($key, $value, $duration);
  93. }
  94. /**
  95. * Stores multiple key-value pairs in cache.
  96. * @param array $data array where key corresponds to cache key while value
  97. * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
  98. * @return array array of failed keys
  99. */
  100. protected function setValues($data, $duration)
  101. {
  102. $result = $this->useApcu ? apcu_store($data, null, $duration) : apc_store($data, null, $duration);
  103. return is_array($result) ? array_keys($result) : [];
  104. }
  105. /**
  106. * Stores a value identified by a key into cache if the cache does not contain this key.
  107. * This is the implementation of the method declared in the parent class.
  108. * @param string $key the key identifying the value to be cached
  109. * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]],
  110. * it could be something else.
  111. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  112. * @return bool true if the value is successfully stored into cache, false otherwise
  113. */
  114. protected function addValue($key, $value, $duration)
  115. {
  116. return $this->useApcu ? apcu_add($key, $value, $duration) : apc_add($key, $value, $duration);
  117. }
  118. /**
  119. * Adds multiple key-value pairs to cache.
  120. * @param array $data array where key corresponds to cache key while value is the value stored
  121. * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire.
  122. * @return array array of failed keys
  123. */
  124. protected function addValues($data, $duration)
  125. {
  126. $result = $this->useApcu ? apcu_add($data, null, $duration) : apc_add($data, null, $duration);
  127. return is_array($result) ? array_keys($result) : [];
  128. }
  129. /**
  130. * Deletes a value with the specified key from cache
  131. * This is the implementation of the method declared in the parent class.
  132. * @param string $key the key of the value to be deleted
  133. * @return bool if no error happens during deletion
  134. */
  135. protected function deleteValue($key)
  136. {
  137. return $this->useApcu ? apcu_delete($key) : apc_delete($key);
  138. }
  139. /**
  140. * Deletes all values from cache.
  141. * This is the implementation of the method declared in the parent class.
  142. * @return bool whether the flush operation was successful.
  143. */
  144. protected function flushValues()
  145. {
  146. return $this->useApcu ? apcu_clear_cache() : apc_clear_cache('user');
  147. }
  148. }