Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

138 lines
5.0KB

  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. /**
  9. * ApcCache provides APC caching in terms of an application component.
  10. *
  11. * To use this application component, the [APC PHP extension](http://www.php.net/apc) must be loaded.
  12. * In order to enable APC for CLI you should add "apc.enable_cli = 1" to your php.ini.
  13. *
  14. * See [[Cache]] for common cache operations that ApcCache supports.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class ApcCache extends Cache
  20. {
  21. /**
  22. * Checks whether a specified key exists in the cache.
  23. * This can be faster than getting the value from the cache if the data is big.
  24. * Note that this method does not check whether the dependency associated
  25. * with the cached data, if there is any, has changed. So a call to [[get]]
  26. * may return false while exists returns true.
  27. * @param mixed $key a key identifying the cached value. This can be a simple string or
  28. * a complex data structure consisting of factors representing the key.
  29. * @return boolean true if a value exists in cache, false if the value is not in the cache or expired.
  30. */
  31. public function exists($key)
  32. {
  33. $key = $this->buildKey($key);
  34. return apc_exists($key);
  35. }
  36. /**
  37. * Retrieves a value from cache with a specified key.
  38. * This is the implementation of the method declared in the parent class.
  39. * @param string $key a unique key identifying the cached value
  40. * @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
  41. */
  42. protected function getValue($key)
  43. {
  44. return apc_fetch($key);
  45. }
  46. /**
  47. * Retrieves multiple values from cache with the specified keys.
  48. * @param array $keys a list of keys identifying the cached values
  49. * @return array a list of cached values indexed by the keys
  50. */
  51. protected function getValues($keys)
  52. {
  53. $values = apc_fetch($keys);
  54. return is_array($values) ? $values : [];
  55. }
  56. /**
  57. * Stores a value identified by a key in cache.
  58. * This is the implementation of the method declared in the parent class.
  59. *
  60. * @param string $key the key identifying the value to be cached
  61. * @param string $value the value to be cached
  62. * @param integer $duration the number of seconds in which the cached value will expire. 0 means never expire.
  63. * @return boolean true if the value is successfully stored into cache, false otherwise
  64. */
  65. protected function setValue($key, $value, $duration)
  66. {
  67. return apc_store($key, $value, $duration);
  68. }
  69. /**
  70. * Stores multiple key-value pairs in cache.
  71. * @param array $data array where key corresponds to cache key while value
  72. * @param integer $duration the number of seconds in which the cached values will expire. 0 means never expire.
  73. * @return array array of failed keys
  74. */
  75. protected function setValues($data, $duration)
  76. {
  77. $result = apc_store($data, null, $duration);
  78. return is_array($result) ? array_keys($result) : [];
  79. }
  80. /**
  81. * Stores a value identified by a key into cache if the cache does not contain this key.
  82. * This is the implementation of the method declared in the parent class.
  83. * @param string $key the key identifying the value to be cached
  84. * @param string $value the value to be cached
  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 addValue($key, $value, $duration)
  89. {
  90. return apc_add($key, $value, $duration);
  91. }
  92. /**
  93. * Adds multiple key-value pairs to cache.
  94. * @param array $data array where key corresponds to cache key while value is the value stored
  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 addValues($data, $duration)
  99. {
  100. $result = apc_add($data, null, $duration);
  101. return is_array($result) ? array_keys($result) : [];
  102. }
  103. /**
  104. * Deletes a value with the specified key from cache
  105. * This is the implementation of the method declared in the parent class.
  106. * @param string $key the key of the value to be deleted
  107. * @return boolean if no error happens during deletion
  108. */
  109. protected function deleteValue($key)
  110. {
  111. return apc_delete($key);
  112. }
  113. /**
  114. * Deletes all values from cache.
  115. * This is the implementation of the method declared in the parent class.
  116. * @return boolean whether the flush operation was successful.
  117. */
  118. protected function flushValues()
  119. {
  120. if (extension_loaded('apcu')) {
  121. return apc_clear_cache();
  122. } else {
  123. return apc_clear_cache('user');
  124. }
  125. }
  126. }