Cache_apc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 2.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter APC Caching Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Core
  45. * @author EllisLab Dev Team
  46. * @link
  47. */
  48. class CI_Cache_apc extends CI_Driver {
  49. /**
  50. * Class constructor
  51. *
  52. * Only present so that an error message is logged
  53. * if APC is not available.
  54. *
  55. * @return void
  56. */
  57. public function __construct()
  58. {
  59. if ( ! $this->is_supported())
  60. {
  61. log_message('error', 'Cache: Failed to initialize APC; extension not loaded/enabled?');
  62. }
  63. }
  64. // ------------------------------------------------------------------------
  65. /**
  66. * Get
  67. *
  68. * Look for a value in the cache. If it exists, return the data
  69. * if not, return FALSE
  70. *
  71. * @param string
  72. * @return mixed value that is stored/FALSE on failure
  73. */
  74. public function get($id)
  75. {
  76. $success = FALSE;
  77. $data = apc_fetch($id, $success);
  78. return ($success === TRUE) ? $data : FALSE;
  79. }
  80. // ------------------------------------------------------------------------
  81. /**
  82. * Cache Save
  83. *
  84. * @param string $id Cache ID
  85. * @param mixed $data Data to store
  86. * @param int $ttl Length of time (in seconds) to cache the data
  87. * @param bool $raw Whether to store the raw value (unused)
  88. * @return bool TRUE on success, FALSE on failure
  89. */
  90. public function save($id, $data, $ttl = 60, $raw = FALSE)
  91. {
  92. return apc_store($id, $data, (int) $ttl);
  93. }
  94. // ------------------------------------------------------------------------
  95. /**
  96. * Delete from Cache
  97. *
  98. * @param mixed unique identifier of the item in the cache
  99. * @return bool true on success/false on failure
  100. */
  101. public function delete($id)
  102. {
  103. return apc_delete($id);
  104. }
  105. // ------------------------------------------------------------------------
  106. /**
  107. * Increment a raw value
  108. *
  109. * @param string $id Cache ID
  110. * @param int $offset Step/value to add
  111. * @return mixed New value on success or FALSE on failure
  112. */
  113. public function increment($id, $offset = 1)
  114. {
  115. return apc_inc($id, $offset);
  116. }
  117. // ------------------------------------------------------------------------
  118. /**
  119. * Decrement a raw value
  120. *
  121. * @param string $id Cache ID
  122. * @param int $offset Step/value to reduce by
  123. * @return mixed New value on success or FALSE on failure
  124. */
  125. public function decrement($id, $offset = 1)
  126. {
  127. return apc_dec($id, $offset);
  128. }
  129. // ------------------------------------------------------------------------
  130. /**
  131. * Clean the cache
  132. *
  133. * @return bool false on failure/true on success
  134. */
  135. public function clean()
  136. {
  137. return apc_clear_cache('user');
  138. }
  139. // ------------------------------------------------------------------------
  140. /**
  141. * Cache Info
  142. *
  143. * @param string user/filehits
  144. * @return mixed array on success, false on failure
  145. */
  146. public function cache_info($type = NULL)
  147. {
  148. return apc_cache_info($type);
  149. }
  150. // ------------------------------------------------------------------------
  151. /**
  152. * Get Cache Metadata
  153. *
  154. * @param mixed key to get cache metadata on
  155. * @return mixed array on success/false on failure
  156. */
  157. public function get_metadata($id)
  158. {
  159. $cache_info = apc_cache_info('user', FALSE);
  160. if (empty($cache_info) OR empty($cache_info['cache_list']))
  161. {
  162. return FALSE;
  163. }
  164. foreach ($cache_info['cache_list'] as &$entry)
  165. {
  166. if ($entry['info'] !== $id)
  167. {
  168. continue;
  169. }
  170. $success = FALSE;
  171. $metadata = array(
  172. 'expire' => ($entry['ttl'] ? $entry['mtime'] + $entry['ttl'] : 0),
  173. 'mtime' => $entry['ttl'],
  174. 'data' => apc_fetch($id, $success)
  175. );
  176. return ($success === TRUE) ? $metadata : FALSE;
  177. }
  178. return FALSE;
  179. }
  180. // ------------------------------------------------------------------------
  181. /**
  182. * is_supported()
  183. *
  184. * Check to see if APC is available on this system, bail if it isn't.
  185. *
  186. * @return bool
  187. */
  188. public function is_supported()
  189. {
  190. return (extension_loaded('apc') && ini_get('apc.enabled'));
  191. }
  192. }