您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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