ApcCache.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <[email protected]>
  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. return apc_fetch($keys);
  54. }
  55. /**
  56. * Stores a value identified by a key in cache.
  57. * This is the implementation of the method declared in the parent class.
  58. *
  59. * @param string $key the key identifying the value to be cached
  60. * @param string $value the value to be cached
  61. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  62. * @return boolean true if the value is successfully stored into cache, false otherwise
  63. */
  64. protected function setValue($key, $value, $expire)
  65. {
  66. return apc_store($key, $value, $expire);
  67. }
  68. /**
  69. * Stores multiple key-value pairs in cache.
  70. * @param array $data array where key corresponds to cache key while value
  71. * @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
  72. * @return array array of failed keys
  73. */
  74. protected function setValues($data, $expire)
  75. {
  76. return array_keys(apc_store($data, null, $expire));
  77. }
  78. /**
  79. * Stores a value identified by a key into cache if the cache does not contain this key.
  80. * This is the implementation of the method declared in the parent class.
  81. * @param string $key the key identifying the value to be cached
  82. * @param string $value the value to be cached
  83. * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  84. * @return boolean true if the value is successfully stored into cache, false otherwise
  85. */
  86. protected function addValue($key, $value, $expire)
  87. {
  88. return apc_add($key, $value, $expire);
  89. }
  90. /**
  91. * Adds multiple key-value pairs to cache.
  92. * @param array $data array where key corresponds to cache key while value is the value stored
  93. * @param integer $expire the number of seconds in which the cached values will expire. 0 means never expire.
  94. * @return array array of failed keys
  95. */
  96. protected function addValues($data, $expire)
  97. {
  98. return array_keys(apc_add($data, null, $expire));
  99. }
  100. /**
  101. * Deletes a value with the specified key from cache
  102. * This is the implementation of the method declared in the parent class.
  103. * @param string $key the key of the value to be deleted
  104. * @return boolean if no error happens during deletion
  105. */
  106. protected function deleteValue($key)
  107. {
  108. return apc_delete($key);
  109. }
  110. /**
  111. * Deletes all values from cache.
  112. * This is the implementation of the method declared in the parent class.
  113. * @return boolean whether the flush operation was successful.
  114. */
  115. protected function flushValues()
  116. {
  117. if (extension_loaded('apcu')) {
  118. return apc_clear_cache();
  119. } else {
  120. return apc_clear_cache('user');
  121. }
  122. }
  123. }