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.

286 lines
6.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 2.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter File Caching Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Core
  45. * @author EllisLab Dev Team
  46. * @link
  47. */
  48. class CI_Cache_file extends CI_Driver {
  49. /**
  50. * Directory in which to save cache files
  51. *
  52. * @var string
  53. */
  54. protected $_cache_path;
  55. /**
  56. * Initialize file-based cache
  57. *
  58. * @return void
  59. */
  60. public function __construct()
  61. {
  62. $CI =& get_instance();
  63. $CI->load->helper('file');
  64. $path = $CI->config->item('cache_path');
  65. $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path;
  66. }
  67. // ------------------------------------------------------------------------
  68. /**
  69. * Fetch from cache
  70. *
  71. * @param string $id Cache ID
  72. * @return mixed Data on success, FALSE on failure
  73. */
  74. public function get($id)
  75. {
  76. $data = $this->_get($id);
  77. return is_array($data) ? $data['data'] : FALSE;
  78. }
  79. // ------------------------------------------------------------------------
  80. /**
  81. * Save into cache
  82. *
  83. * @param string $id Cache ID
  84. * @param mixed $data Data to store
  85. * @param int $ttl Time to live in seconds
  86. * @param bool $raw Whether to store the raw value (unused)
  87. * @return bool TRUE on success, FALSE on failure
  88. */
  89. public function save($id, $data, $ttl = 60, $raw = FALSE)
  90. {
  91. $contents = array(
  92. 'time' => time(),
  93. 'ttl' => $ttl,
  94. 'data' => $data
  95. );
  96. if (write_file($this->_cache_path.$id, serialize($contents)))
  97. {
  98. chmod($this->_cache_path.$id, 0640);
  99. return TRUE;
  100. }
  101. return FALSE;
  102. }
  103. // ------------------------------------------------------------------------
  104. /**
  105. * Delete from Cache
  106. *
  107. * @param mixed unique identifier of item in cache
  108. * @return bool true on success/false on failure
  109. */
  110. public function delete($id)
  111. {
  112. return is_file($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
  113. }
  114. // ------------------------------------------------------------------------
  115. /**
  116. * Increment a raw value
  117. *
  118. * @param string $id Cache ID
  119. * @param int $offset Step/value to add
  120. * @return New value on success, FALSE on failure
  121. */
  122. public function increment($id, $offset = 1)
  123. {
  124. $data = $this->_get($id);
  125. if ($data === FALSE)
  126. {
  127. $data = array('data' => 0, 'ttl' => 60);
  128. }
  129. elseif ( ! is_int($data['data']))
  130. {
  131. return FALSE;
  132. }
  133. $new_value = $data['data'] + $offset;
  134. return $this->save($id, $new_value, $data['ttl'])
  135. ? $new_value
  136. : FALSE;
  137. }
  138. // ------------------------------------------------------------------------
  139. /**
  140. * Decrement a raw value
  141. *
  142. * @param string $id Cache ID
  143. * @param int $offset Step/value to reduce by
  144. * @return New value on success, FALSE on failure
  145. */
  146. public function decrement($id, $offset = 1)
  147. {
  148. $data = $this->_get($id);
  149. if ($data === FALSE)
  150. {
  151. $data = array('data' => 0, 'ttl' => 60);
  152. }
  153. elseif ( ! is_int($data['data']))
  154. {
  155. return FALSE;
  156. }
  157. $new_value = $data['data'] - $offset;
  158. return $this->save($id, $new_value, $data['ttl'])
  159. ? $new_value
  160. : FALSE;
  161. }
  162. // ------------------------------------------------------------------------
  163. /**
  164. * Clean the Cache
  165. *
  166. * @return bool false on failure/true on success
  167. */
  168. public function clean()
  169. {
  170. return delete_files($this->_cache_path, FALSE, TRUE);
  171. }
  172. // ------------------------------------------------------------------------
  173. /**
  174. * Cache Info
  175. *
  176. * Not supported by file-based caching
  177. *
  178. * @param string user/filehits
  179. * @return mixed FALSE
  180. */
  181. public function cache_info($type = NULL)
  182. {
  183. return get_dir_file_info($this->_cache_path);
  184. }
  185. // ------------------------------------------------------------------------
  186. /**
  187. * Get Cache Metadata
  188. *
  189. * @param mixed key to get cache metadata on
  190. * @return mixed FALSE on failure, array on success.
  191. */
  192. public function get_metadata($id)
  193. {
  194. if ( ! is_file($this->_cache_path.$id))
  195. {
  196. return FALSE;
  197. }
  198. $data = unserialize(file_get_contents($this->_cache_path.$id));
  199. if (is_array($data))
  200. {
  201. $mtime = filemtime($this->_cache_path.$id);
  202. if ( ! isset($data['ttl'], $data['time']))
  203. {
  204. return FALSE;
  205. }
  206. return array(
  207. 'expire' => $data['time'] + $data['ttl'],
  208. 'mtime' => $mtime
  209. );
  210. }
  211. return FALSE;
  212. }
  213. // ------------------------------------------------------------------------
  214. /**
  215. * Is supported
  216. *
  217. * In the file driver, check to see that the cache directory is indeed writable
  218. *
  219. * @return bool
  220. */
  221. public function is_supported()
  222. {
  223. return is_really_writable($this->_cache_path);
  224. }
  225. // ------------------------------------------------------------------------
  226. /**
  227. * Get all data
  228. *
  229. * Internal method to get all the relevant data about a cache item
  230. *
  231. * @param string $id Cache ID
  232. * @return mixed Data array on success or FALSE on failure
  233. */
  234. protected function _get($id)
  235. {
  236. if ( ! is_file($this->_cache_path.$id))
  237. {
  238. return FALSE;
  239. }
  240. $data = unserialize(file_get_contents($this->_cache_path.$id));
  241. if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
  242. {
  243. unlink($this->_cache_path.$id);
  244. return FALSE;
  245. }
  246. return $data;
  247. }
  248. }