Apcu.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * [Kohana Cache](api/Kohana_Cache) APCu data store driver for Kohana Cache
  4. * library.
  5. *
  6. * ### Configuration example
  7. *
  8. * Below is an example of an _apcu_ server configuration.
  9. *
  10. * return array(
  11. * 'apcu' => array( // Driver group
  12. * 'driver' => 'apcu', // using APCu driver
  13. * ),
  14. * )
  15. *
  16. * In cases where only one cache group is required, if the group is named `default` there is
  17. * no need to pass the group name when instantiating a cache instance.
  18. *
  19. * #### General cache group configuration settings
  20. *
  21. * Below are the settings available to all types of cache driver.
  22. *
  23. * Name | Required | Description
  24. * -------------- | -------- | ---------------------------------------------------------------
  25. * driver | __YES__ | (_string_) The driver type to use
  26. *
  27. * ### System requirements
  28. *
  29. * * Kohana 3.0.x
  30. * * PHP 5.2.4 or greater
  31. * * APCu PHP extension
  32. *
  33. * @package Kohana/Cache
  34. * @category Base
  35. * @author Kohana Team
  36. * @copyright (c) Kohana Team
  37. * @license https://koseven.ga/LICENSE.md
  38. */
  39. class Kohana_Cache_Apcu extends Cache implements Cache_Arithmetic {
  40. /**
  41. * Check for existence of the APCu extension This method cannot be invoked externally. The driver must
  42. * be instantiated using the `Cache::instance()` method.
  43. *
  44. * @param array $config configuration
  45. * @throws Cache_Exception
  46. */
  47. protected function __construct(array $config)
  48. {
  49. if ( ! extension_loaded('apcu'))
  50. {
  51. throw new Cache_Exception('PHP APCu extension is not available.');
  52. }
  53. parent::__construct($config);
  54. }
  55. /**
  56. * Retrieve a cached value entry by id.
  57. *
  58. * // Retrieve cache entry from apcu group
  59. * $data = Cache::instance('apcu')->get('foo');
  60. *
  61. * // Retrieve cache entry from apcu group and return 'bar' if miss
  62. * $data = Cache::instance('apcu')->get('foo', 'bar');
  63. *
  64. * @param string $id id of cache to entry
  65. * @param string $default default value to return if cache miss
  66. * @return mixed
  67. * @throws Cache_Exception
  68. */
  69. public function get($id, $default = NULL)
  70. {
  71. $data = apcu_fetch($this->_sanitize_id($id), $success);
  72. return $success ? $data : $default;
  73. }
  74. /**
  75. * Set a value to cache with id and lifetime
  76. *
  77. * $data = 'bar';
  78. *
  79. * // Set 'bar' to 'foo' in apcu group, using default expiry
  80. * Cache::instance('apcu')->set('foo', $data);
  81. *
  82. * // Set 'bar' to 'foo' in apcu group for 30 seconds
  83. * Cache::instance('apcu')->set('foo', $data, 30);
  84. *
  85. * @param string $id id of cache entry
  86. * @param string $data data to set to cache
  87. * @param ?integer $lifetime lifetime in seconds
  88. * @return boolean
  89. */
  90. public function set($id, $data, $lifetime = NULL)
  91. {
  92. if (is_null($lifetime))
  93. {
  94. $lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
  95. }
  96. return apcu_store($this->_sanitize_id($id), $data, $lifetime);
  97. }
  98. /**
  99. * Delete a cache entry based on id
  100. *
  101. * // Delete 'foo' entry from the apcu group
  102. * Cache::instance('apcu')->delete('foo');
  103. *
  104. * @param string $id id to remove from cache
  105. * @return boolean
  106. */
  107. public function delete($id)
  108. {
  109. return apcu_delete($this->_sanitize_id($id));
  110. }
  111. /**
  112. * Delete all cache entries.
  113. *
  114. * Beware of using this method when
  115. * using shared memory cache systems, as it will wipe every
  116. * entry within the system for all clients.
  117. *
  118. * // Delete all cache entries in the apcu group
  119. * Cache::instance('apcu')->delete_all();
  120. *
  121. * @return boolean
  122. */
  123. public function delete_all()
  124. {
  125. return apcu_clear_cache();
  126. }
  127. /**
  128. * Increments a given value by the step value supplied.
  129. * Useful for shared counters and other persistent integer based
  130. * tracking.
  131. *
  132. * @param string $id of cache entry to increment
  133. * @param int $step value to increment by
  134. * @return integer|boolean
  135. */
  136. public function increment($id, $step = 1)
  137. {
  138. if (apcu_exists($this->_sanitize_id($id))) {
  139. return apcu_inc($this->_sanitize_id($id), $step);
  140. } else {
  141. return FALSE;
  142. }
  143. }
  144. /**
  145. * Decrements a given value by the step value supplied.
  146. * Useful for shared counters and other persistent integer based
  147. * tracking.
  148. *
  149. * @param string $id of cache entry to decrement
  150. * @param int $step value to decrement by
  151. * @return integer
  152. * @return boolean
  153. */
  154. public function decrement($id, $step = 1)
  155. {
  156. if (apcu_exists($this->_sanitize_id($id))) {
  157. return apcu_dec($this->_sanitize_id($id), $step);
  158. } else {
  159. return FALSE;
  160. }
  161. }
  162. } // End Kohana_Cache_Apcu