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.

217 lines
5.7 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 3.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Wincache Caching Class
  41. *
  42. * Read more about Wincache functions here:
  43. * http://www.php.net/manual/en/ref.wincache.php
  44. *
  45. * @package CodeIgniter
  46. * @subpackage Libraries
  47. * @category Core
  48. * @author Mike Murkovic
  49. * @link
  50. */
  51. class CI_Cache_wincache extends CI_Driver {
  52. /**
  53. * Class constructor
  54. *
  55. * Only present so that an error message is logged
  56. * if APC is not available.
  57. *
  58. * @return void
  59. */
  60. public function __construct()
  61. {
  62. if ( ! $this->is_supported())
  63. {
  64. log_message('error', 'Cache: Failed to initialize Wincache; extension not loaded/enabled?');
  65. }
  66. }
  67. // ------------------------------------------------------------------------
  68. /**
  69. * Get
  70. *
  71. * Look for a value in the cache. If it exists, return the data,
  72. * if not, return FALSE
  73. *
  74. * @param string $id Cache Ide
  75. * @return mixed Value that is stored/FALSE on failure
  76. */
  77. public function get($id)
  78. {
  79. $success = FALSE;
  80. $data = wincache_ucache_get($id, $success);
  81. // Success returned by reference from wincache_ucache_get()
  82. return ($success) ? $data : FALSE;
  83. }
  84. // ------------------------------------------------------------------------
  85. /**
  86. * Cache Save
  87. *
  88. * @param string $id Cache ID
  89. * @param mixed $data Data to store
  90. * @param int $ttl Time to live (in seconds)
  91. * @param bool $raw Whether to store the raw value (unused)
  92. * @return bool true on success/false on failure
  93. */
  94. public function save($id, $data, $ttl = 60, $raw = FALSE)
  95. {
  96. return wincache_ucache_set($id, $data, $ttl);
  97. }
  98. // ------------------------------------------------------------------------
  99. /**
  100. * Delete from Cache
  101. *
  102. * @param mixed unique identifier of the item in the cache
  103. * @return bool true on success/false on failure
  104. */
  105. public function delete($id)
  106. {
  107. return wincache_ucache_delete($id);
  108. }
  109. // ------------------------------------------------------------------------
  110. /**
  111. * Increment a raw value
  112. *
  113. * @param string $id Cache ID
  114. * @param int $offset Step/value to add
  115. * @return mixed New value on success or FALSE on failure
  116. */
  117. public function increment($id, $offset = 1)
  118. {
  119. $success = FALSE;
  120. $value = wincache_ucache_inc($id, $offset, $success);
  121. return ($success === TRUE) ? $value : FALSE;
  122. }
  123. // ------------------------------------------------------------------------
  124. /**
  125. * Decrement a raw value
  126. *
  127. * @param string $id Cache ID
  128. * @param int $offset Step/value to reduce by
  129. * @return mixed New value on success or FALSE on failure
  130. */
  131. public function decrement($id, $offset = 1)
  132. {
  133. $success = FALSE;
  134. $value = wincache_ucache_dec($id, $offset, $success);
  135. return ($success === TRUE) ? $value : FALSE;
  136. }
  137. // ------------------------------------------------------------------------
  138. /**
  139. * Clean the cache
  140. *
  141. * @return bool false on failure/true on success
  142. */
  143. public function clean()
  144. {
  145. return wincache_ucache_clear();
  146. }
  147. // ------------------------------------------------------------------------
  148. /**
  149. * Cache Info
  150. *
  151. * @return mixed array on success, false on failure
  152. */
  153. public function cache_info()
  154. {
  155. return wincache_ucache_info(TRUE);
  156. }
  157. // ------------------------------------------------------------------------
  158. /**
  159. * Get Cache Metadata
  160. *
  161. * @param mixed key to get cache metadata on
  162. * @return mixed array on success/false on failure
  163. */
  164. public function get_metadata($id)
  165. {
  166. if ($stored = wincache_ucache_info(FALSE, $id))
  167. {
  168. $age = $stored['ucache_entries'][1]['age_seconds'];
  169. $ttl = $stored['ucache_entries'][1]['ttl_seconds'];
  170. $hitcount = $stored['ucache_entries'][1]['hitcount'];
  171. return array(
  172. 'expire' => $ttl - $age,
  173. 'hitcount' => $hitcount,
  174. 'age' => $age,
  175. 'ttl' => $ttl
  176. );
  177. }
  178. return FALSE;
  179. }
  180. // ------------------------------------------------------------------------
  181. /**
  182. * is_supported()
  183. *
  184. * Check to see if WinCache 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('wincache') && ini_get('wincache.ucenabled'));
  191. }
  192. }