Apc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * [Kohana Cache](api/Kohana_Cache) APC driver. Provides an opcode based
  4. * driver for the Kohana Cache library.
  5. *
  6. * ### Configuration example
  7. *
  8. * Below is an example of an _apc_ server configuration.
  9. *
  10. * return array(
  11. * 'apc' => array( // Driver group
  12. * 'driver' => 'apc', // using APC 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. * * APC 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_Apc extends Cache implements Cache_Arithmetic {
  40. /**
  41. * Check for existence of the APC 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('apc'))
  50. {
  51. throw new Cache_Exception('PHP APC 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 apc group
  59. * $data = Cache::instance('apc')->get('foo');
  60. *
  61. * // Retrieve cache entry from apc group and return 'bar' if miss
  62. * $data = Cache::instance('apc')->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 = apc_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 apc group, using default expiry
  80. * Cache::instance('apc')->set('foo', $data);
  81. *
  82. * // Set 'bar' to 'foo' in apc group for 30 seconds
  83. * Cache::instance('apc')->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 ($lifetime === NULL)
  93. {
  94. $lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
  95. }
  96. return apc_store($this->_sanitize_id($id), $data, $lifetime);
  97. }
  98. /**
  99. * Delete a cache entry based on id
  100. *
  101. * // Delete 'foo' entry from the apc group
  102. * Cache::instance('apc')->delete('foo');
  103. *
  104. * @param string $id id to remove from cache
  105. * @return boolean
  106. */
  107. public function delete($id)
  108. {
  109. return apc_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 apc group
  119. * Cache::instance('apc')->delete_all();
  120. *
  121. * @return boolean
  122. */
  123. public function delete_all()
  124. {
  125. return apc_clear_cache('user');
  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
  135. * @return boolean
  136. */
  137. public function increment($id, $step = 1)
  138. {
  139. return apc_inc($id, $step);
  140. }
  141. /**
  142. * Decrements a given value by the step value supplied.
  143. * Useful for shared counters and other persistent integer based
  144. * tracking.
  145. *
  146. * @param string id of cache entry to decrement
  147. * @param int step value to decrement by
  148. * @return integer
  149. * @return boolean
  150. */
  151. public function decrement($id, $step = 1)
  152. {
  153. return apc_dec($id, $step);
  154. }
  155. } // End Kohana_Cache_Apc