Apcu.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * [KO7 Cache](api/KO7_Cache) APCu data store driver for KO7 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. * * KO7 3.0.x
  30. * * PHP 5.2.4 or greater
  31. * * APCu PHP extension
  32. *
  33. * @package KO7/Cache
  34. * @category Base
  35. *
  36. * @copyright (c) 2007-2016 Kohana Team
  37. * @copyright (c) since 2016 Koseven Team
  38. * @license https://koseven.dev/LICENSE
  39. */
  40. class KO7_Cache_Apcu extends Cache implements Cache_Arithmetic {
  41. /**
  42. * Check for existence of the APCu extension This method cannot be invoked externally. The driver must
  43. * be instantiated using the `Cache::instance()` method.
  44. *
  45. * @param array $config configuration
  46. * @throws Cache_Exception
  47. */
  48. protected function __construct(array $config)
  49. {
  50. if ( ! extension_loaded('apcu'))
  51. {
  52. throw new Cache_Exception('PHP APCu extension is not available.');
  53. }
  54. parent::__construct($config);
  55. }
  56. /**
  57. * Retrieve a cached value entry by id.
  58. *
  59. * // Retrieve cache entry from apcu group
  60. * $data = Cache::instance('apcu')->get('foo');
  61. *
  62. * // Retrieve cache entry from apcu group and return 'bar' if miss
  63. * $data = Cache::instance('apcu')->get('foo', 'bar');
  64. *
  65. * @param string $id id of cache to entry
  66. * @param string $default default value to return if cache miss
  67. * @return mixed
  68. * @throws Cache_Exception
  69. */
  70. public function get($id, $default = NULL)
  71. {
  72. $data = apcu_fetch($this->_sanitize_id($id), $success);
  73. return $success ? $data : $default;
  74. }
  75. /**
  76. * Set a value to cache with id and lifetime
  77. *
  78. * $data = 'bar';
  79. *
  80. * // Set 'bar' to 'foo' in apcu group, using default expiry
  81. * Cache::instance('apcu')->set('foo', $data);
  82. *
  83. * // Set 'bar' to 'foo' in apcu group for 30 seconds
  84. * Cache::instance('apcu')->set('foo', $data, 30);
  85. *
  86. * @param string $id id of cache entry
  87. * @param string $data data to set to cache
  88. * @param integer $lifetime lifetime in seconds
  89. * @return boolean
  90. */
  91. public function set($id, $data, $lifetime = NULL)
  92. {
  93. if ($lifetime === NULL)
  94. {
  95. $lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
  96. }
  97. return apcu_store($this->_sanitize_id($id), $data, $lifetime);
  98. }
  99. /**
  100. * Delete a cache entry based on id
  101. *
  102. * // Delete 'foo' entry from the apcu group
  103. * Cache::instance('apcu')->delete('foo');
  104. *
  105. * @param string $id id to remove from cache
  106. * @return boolean
  107. */
  108. public function delete($id)
  109. {
  110. return apcu_delete($this->_sanitize_id($id));
  111. }
  112. /**
  113. * Delete all cache entries.
  114. *
  115. * Beware of using this method when
  116. * using shared memory cache systems, as it will wipe every
  117. * entry within the system for all clients.
  118. *
  119. * // Delete all cache entries in the apcu group
  120. * Cache::instance('apcu')->delete_all();
  121. *
  122. * @return boolean
  123. */
  124. public function delete_all()
  125. {
  126. return apcu_clear_cache();
  127. }
  128. /**
  129. * Increments a given value by the step value supplied.
  130. * Useful for shared counters and other persistent integer based
  131. * tracking.
  132. *
  133. * @param string id of cache entry to increment
  134. * @param int step value to increment by
  135. * @return integer
  136. * @return boolean
  137. */
  138. public function increment($id, $step = 1)
  139. {
  140. $id = $this->_sanitize_id($id);
  141. if (apcu_exists($id)) {
  142. return apcu_inc($id, $step);
  143. }
  144. return FALSE;
  145. }
  146. /**
  147. * Decrements a given value by the step value supplied.
  148. * Useful for shared counters and other persistent integer based
  149. * tracking.
  150. *
  151. * @param string id of cache entry to decrement
  152. * @param int step value to decrement by
  153. * @return integer
  154. * @return boolean
  155. */
  156. public function decrement($id, $step = 1)
  157. {
  158. $id = $this->_sanitize_id($id);
  159. if (apcu_exists($id)) {
  160. return apcu_dec($id, $step);
  161. }
  162. return FALSE;
  163. }
  164. } // End KO7_Cache_Apcu