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.

255 lines
6.6 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 Caching Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Core
  45. * @author EllisLab Dev Team
  46. * @link
  47. */
  48. class CI_Cache extends CI_Driver_Library {
  49. /**
  50. * Valid cache drivers
  51. *
  52. * @var array
  53. */
  54. protected $valid_drivers = array(
  55. 'apc',
  56. 'dummy',
  57. 'file',
  58. 'memcached',
  59. 'redis',
  60. 'wincache'
  61. );
  62. /**
  63. * Path of cache files (if file-based cache)
  64. *
  65. * @var string
  66. */
  67. protected $_cache_path = NULL;
  68. /**
  69. * Reference to the driver
  70. *
  71. * @var mixed
  72. */
  73. protected $_adapter = 'dummy';
  74. /**
  75. * Fallback driver
  76. *
  77. * @var string
  78. */
  79. protected $_backup_driver = 'dummy';
  80. /**
  81. * Cache key prefix
  82. *
  83. * @var string
  84. */
  85. public $key_prefix = '';
  86. /**
  87. * Constructor
  88. *
  89. * Initialize class properties based on the configuration array.
  90. *
  91. * @param array $config = array()
  92. * @return void
  93. */
  94. public function __construct($config = array())
  95. {
  96. isset($config['adapter']) && $this->_adapter = $config['adapter'];
  97. isset($config['backup']) && $this->_backup_driver = $config['backup'];
  98. isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix'];
  99. // If the specified adapter isn't available, check the backup.
  100. if ( ! $this->is_supported($this->_adapter))
  101. {
  102. if ( ! $this->is_supported($this->_backup_driver))
  103. {
  104. // Backup isn't supported either. Default to 'Dummy' driver.
  105. log_message('error', 'Cache adapter "'.$this->_adapter.'" and backup "'.$this->_backup_driver.'" are both unavailable. Cache is now using "Dummy" adapter.');
  106. $this->_adapter = 'dummy';
  107. }
  108. else
  109. {
  110. // Backup is supported. Set it to primary.
  111. log_message('debug', 'Cache adapter "'.$this->_adapter.'" is unavailable. Falling back to "'.$this->_backup_driver.'" backup adapter.');
  112. $this->_adapter = $this->_backup_driver;
  113. }
  114. }
  115. }
  116. // ------------------------------------------------------------------------
  117. /**
  118. * Get
  119. *
  120. * Look for a value in the cache. If it exists, return the data
  121. * if not, return FALSE
  122. *
  123. * @param string $id
  124. * @return mixed value matching $id or FALSE on failure
  125. */
  126. public function get($id)
  127. {
  128. return $this->{$this->_adapter}->get($this->key_prefix.$id);
  129. }
  130. // ------------------------------------------------------------------------
  131. /**
  132. * Cache Save
  133. *
  134. * @param string $id Cache ID
  135. * @param mixed $data Data to store
  136. * @param int $ttl Cache TTL (in seconds)
  137. * @param bool $raw Whether to store the raw value
  138. * @return bool TRUE on success, FALSE on failure
  139. */
  140. public function save($id, $data, $ttl = 60, $raw = FALSE)
  141. {
  142. return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw);
  143. }
  144. // ------------------------------------------------------------------------
  145. /**
  146. * Delete from Cache
  147. *
  148. * @param string $id Cache ID
  149. * @return bool TRUE on success, FALSE on failure
  150. */
  151. public function delete($id)
  152. {
  153. return $this->{$this->_adapter}->delete($this->key_prefix.$id);
  154. }
  155. // ------------------------------------------------------------------------
  156. /**
  157. * Increment a raw value
  158. *
  159. * @param string $id Cache ID
  160. * @param int $offset Step/value to add
  161. * @return mixed New value on success or FALSE on failure
  162. */
  163. public function increment($id, $offset = 1)
  164. {
  165. return $this->{$this->_adapter}->increment($this->key_prefix.$id, $offset);
  166. }
  167. // ------------------------------------------------------------------------
  168. /**
  169. * Decrement a raw value
  170. *
  171. * @param string $id Cache ID
  172. * @param int $offset Step/value to reduce by
  173. * @return mixed New value on success or FALSE on failure
  174. */
  175. public function decrement($id, $offset = 1)
  176. {
  177. return $this->{$this->_adapter}->decrement($this->key_prefix.$id, $offset);
  178. }
  179. // ------------------------------------------------------------------------
  180. /**
  181. * Clean the cache
  182. *
  183. * @return bool TRUE on success, FALSE on failure
  184. */
  185. public function clean()
  186. {
  187. return $this->{$this->_adapter}->clean();
  188. }
  189. // ------------------------------------------------------------------------
  190. /**
  191. * Cache Info
  192. *
  193. * @param string $type = 'user' user/filehits
  194. * @return mixed array containing cache info on success OR FALSE on failure
  195. */
  196. public function cache_info($type = 'user')
  197. {
  198. return $this->{$this->_adapter}->cache_info($type);
  199. }
  200. // ------------------------------------------------------------------------
  201. /**
  202. * Get Cache Metadata
  203. *
  204. * @param string $id key to get cache metadata on
  205. * @return mixed cache item metadata
  206. */
  207. public function get_metadata($id)
  208. {
  209. return $this->{$this->_adapter}->get_metadata($this->key_prefix.$id);
  210. }
  211. // ------------------------------------------------------------------------
  212. /**
  213. * Is the requested driver supported in this environment?
  214. *
  215. * @param string $driver The driver to test
  216. * @return array
  217. */
  218. public function is_supported($driver)
  219. {
  220. static $support;
  221. if ( ! isset($support, $support[$driver]))
  222. {
  223. $support[$driver] = $this->{$driver}->is_supported();
  224. }
  225. return $support[$driver];
  226. }
  227. }