You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.4 KiB

7 years ago
  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 - 2017, 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 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://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. if ($success === TRUE)
  79. {
  80. return is_array($data)
  81. ? unserialize($data[0])
  82. : $data;
  83. }
  84. return FALSE;
  85. }
  86. // ------------------------------------------------------------------------
  87. /**
  88. * Cache Save
  89. *
  90. * @param string $id Cache ID
  91. * @param mixed $data Data to store
  92. * @param int $ttl Length of time (in seconds) to cache the data
  93. * @param bool $raw Whether to store the raw value
  94. * @return bool TRUE on success, FALSE on failure
  95. */
  96. public function save($id, $data, $ttl = 60, $raw = FALSE)
  97. {
  98. $ttl = (int) $ttl;
  99. return apc_store(
  100. $id,
  101. ($raw === TRUE ? $data : array(serialize($data), time(), $ttl)),
  102. $ttl
  103. );
  104. }
  105. // ------------------------------------------------------------------------
  106. /**
  107. * Delete from Cache
  108. *
  109. * @param mixed unique identifier of the item in the cache
  110. * @return bool true on success/false on failure
  111. */
  112. public function delete($id)
  113. {
  114. return apc_delete($id);
  115. }
  116. // ------------------------------------------------------------------------
  117. /**
  118. * Increment a raw value
  119. *
  120. * @param string $id Cache ID
  121. * @param int $offset Step/value to add
  122. * @return mixed New value on success or FALSE on failure
  123. */
  124. public function increment($id, $offset = 1)
  125. {
  126. return apc_inc($id, $offset);
  127. }
  128. // ------------------------------------------------------------------------
  129. /**
  130. * Decrement a raw value
  131. *
  132. * @param string $id Cache ID
  133. * @param int $offset Step/value to reduce by
  134. * @return mixed New value on success or FALSE on failure
  135. */
  136. public function decrement($id, $offset = 1)
  137. {
  138. return apc_dec($id, $offset);
  139. }
  140. // ------------------------------------------------------------------------
  141. /**
  142. * Clean the cache
  143. *
  144. * @return bool false on failure/true on success
  145. */
  146. public function clean()
  147. {
  148. return apc_clear_cache('user');
  149. }
  150. // ------------------------------------------------------------------------
  151. /**
  152. * Cache Info
  153. *
  154. * @param string user/filehits
  155. * @return mixed array on success, false on failure
  156. */
  157. public function cache_info($type = NULL)
  158. {
  159. return apc_cache_info($type);
  160. }
  161. // ------------------------------------------------------------------------
  162. /**
  163. * Get Cache Metadata
  164. *
  165. * @param mixed key to get cache metadata on
  166. * @return mixed array on success/false on failure
  167. */
  168. public function get_metadata($id)
  169. {
  170. $success = FALSE;
  171. $stored = apc_fetch($id, $success);
  172. if ($success === FALSE OR count($stored) !== 3)
  173. {
  174. return FALSE;
  175. }
  176. list($data, $time, $ttl) = $stored;
  177. return array(
  178. 'expire' => $time + $ttl,
  179. 'mtime' => $time,
  180. 'data' => unserialize($data)
  181. );
  182. }
  183. // ------------------------------------------------------------------------
  184. /**
  185. * is_supported()
  186. *
  187. * Check to see if APC is available on this system, bail if it isn't.
  188. *
  189. * @return bool
  190. */
  191. public function is_supported()
  192. {
  193. return (extension_loaded('apc') && ini_get('apc.enabled'));
  194. }
  195. }