blob: 4f383032f51658f95a321857b96e79498adad279 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Jonesf4a4bd82011-10-20 12:18:42 -05006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev7d4ea072011-12-25 19:23:50 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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:
Greg Akerbde25d92010-12-21 09:31:21 -060017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 2.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020036 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060039
Greg Akerbde25d92010-12-21 09:31:21 -060040/**
Andrey Andreevc9195a72012-06-12 03:49:03 +030041 * CodeIgniter File Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060042 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreev7d4ea072011-12-25 19:23:50 +020047 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060048 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010049class CI_Cache_file extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060050
Timothy Warren0688ac92012-04-20 10:25:04 -040051 /**
52 * Directory in which to save cache files
53 *
54 * @var string
55 */
Greg Akerbde25d92010-12-21 09:31:21 -060056 protected $_cache_path;
57
Timothy Warren0688ac92012-04-20 10:25:04 -040058 /**
59 * Initialize file-based cache
Andrey Andreev56454792012-05-17 14:32:19 +030060 *
61 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040062 */
Greg Akerbde25d92010-12-21 09:31:21 -060063 public function __construct()
64 {
65 $CI =& get_instance();
66 $CI->load->helper('file');
Greg Akerbde25d92010-12-21 09:31:21 -060067 $path = $CI->config->item('cache_path');
Alex Bilbied261b1e2012-06-02 11:12:16 +010068 $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060069 }
70
71 // ------------------------------------------------------------------------
72
73 /**
74 * Fetch from cache
75 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020076 * @param string $id Cache ID
77 * @return mixed Data on success, FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060078 */
79 public function get($id)
80 {
Andrey Andreev43d7fa72014-01-09 17:29:45 +020081 $data = $this->_get($id);
82 return is_array($data) ? $data['data'] : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060083 }
84
85 // ------------------------------------------------------------------------
86
87 /**
88 * Save into cache
89 *
Andrey Andreev43d7fa72014-01-09 17:29:45 +020090 * @param string $id Cache ID
91 * @param mixed $data Data to store
92 * @param int $ttl Time to live in seconds
93 * @param bool $raw Whether to store the raw value (unused)
94 * @return bool TRUE on success, FALSE on failure
Greg Akerbde25d92010-12-21 09:31:21 -060095 */
Andrey Andreev43d7fa72014-01-09 17:29:45 +020096 public function save($id, $data, $ttl = 60, $raw = FALSE)
Andrey Andreev7d4ea072011-12-25 19:23:50 +020097 {
Greg Akerbde25d92010-12-21 09:31:21 -060098 $contents = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040099 'time' => time(),
100 'ttl' => $ttl,
101 'data' => $data
102 );
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200103
Greg Akerbde25d92010-12-21 09:31:21 -0600104 if (write_file($this->_cache_path.$id, serialize($contents)))
105 {
Andrey Andreev45965742014-08-27 20:40:11 +0300106 chmod($this->_cache_path.$id, 0640);
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200107 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600108 }
109
110 return FALSE;
111 }
112
113 // ------------------------------------------------------------------------
114
115 /**
116 * Delete from Cache
117 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300118 * @param mixed unique identifier of item in cache
119 * @return bool true on success/false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600120 */
121 public function delete($id)
122 {
Andrey Andreev04649d92016-08-11 14:13:11 +0300123 return is_file($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -0600124 }
125
126 // ------------------------------------------------------------------------
127
128 /**
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200129 * Increment a raw value
130 *
131 * @param string $id Cache ID
132 * @param int $offset Step/value to add
133 * @return New value on success, FALSE on failure
134 */
135 public function increment($id, $offset = 1)
136 {
137 $data = $this->_get($id);
138
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300139 if ($data === FALSE)
140 {
141 $data = array('data' => 0, 'ttl' => 60);
142 }
143 elseif ( ! is_int($data['data']))
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200144 {
145 return FALSE;
146 }
147
148 $new_value = $data['data'] + $offset;
149 return $this->save($id, $new_value, $data['ttl'])
150 ? $new_value
151 : FALSE;
152 }
153
154 // ------------------------------------------------------------------------
155
156 /**
157 * Decrement a raw value
158 *
159 * @param string $id Cache ID
160 * @param int $offset Step/value to reduce by
161 * @return New value on success, FALSE on failure
162 */
163 public function decrement($id, $offset = 1)
164 {
165 $data = $this->_get($id);
166
Andrey Andreev5f0799a2014-07-31 13:32:41 +0300167 if ($data === FALSE)
168 {
169 $data = array('data' => 0, 'ttl' => 60);
170 }
171 elseif ( ! is_int($data['data']))
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200172 {
173 return FALSE;
174 }
175
176 $new_value = $data['data'] - $offset;
177 return $this->save($id, $new_value, $data['ttl'])
178 ? $new_value
179 : FALSE;
180 }
181
182 // ------------------------------------------------------------------------
183
184 /**
Greg Akerbde25d92010-12-21 09:31:21 -0600185 * Clean the Cache
186 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300187 * @return bool false on failure/true on success
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200188 */
Greg Akerbde25d92010-12-21 09:31:21 -0600189 public function clean()
190 {
vlakoff83c344e2013-03-04 14:30:08 +0100191 return delete_files($this->_cache_path, FALSE, TRUE);
Greg Akerbde25d92010-12-21 09:31:21 -0600192 }
193
194 // ------------------------------------------------------------------------
195
196 /**
197 * Cache Info
198 *
199 * Not supported by file-based caching
200 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300201 * @param string user/filehits
202 * @return mixed FALSE
Greg Akerbde25d92010-12-21 09:31:21 -0600203 */
204 public function cache_info($type = NULL)
205 {
206 return get_dir_file_info($this->_cache_path);
207 }
208
209 // ------------------------------------------------------------------------
210
211 /**
212 * Get Cache Metadata
213 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300214 * @param mixed key to get cache metadata on
215 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600216 */
217 public function get_metadata($id)
218 {
Andrey Andreev04649d92016-08-11 14:13:11 +0300219 if ( ! is_file($this->_cache_path.$id))
Greg Akerbde25d92010-12-21 09:31:21 -0600220 {
221 return FALSE;
222 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200223
Andrey Andreev0f0b7692012-06-07 14:57:04 +0300224 $data = unserialize(file_get_contents($this->_cache_path.$id));
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200225
Greg Akerbde25d92010-12-21 09:31:21 -0600226 if (is_array($data))
227 {
Greg Akerbde25d92010-12-21 09:31:21 -0600228 $mtime = filemtime($this->_cache_path.$id);
229
Andrey Andreev9514dc32016-08-11 15:14:33 +0300230 if ( ! isset($data['ttl'], $data['time']))
Greg Akerbde25d92010-12-21 09:31:21 -0600231 {
232 return FALSE;
233 }
234
235 return array(
Andrey Andreev9514dc32016-08-11 15:14:33 +0300236 'expire' => $data['time'] + $data['ttl'],
Ben Edmunds3db28492011-08-20 14:12:12 -0500237 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600238 );
239 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200240
Greg Akerbde25d92010-12-21 09:31:21 -0600241 return FALSE;
242 }
243
244 // ------------------------------------------------------------------------
245
246 /**
247 * Is supported
248 *
249 * In the file driver, check to see that the cache directory is indeed writable
Andrey Andreev7d4ea072011-12-25 19:23:50 +0200250 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300251 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600252 */
253 public function is_supported()
254 {
255 return is_really_writable($this->_cache_path);
256 }
257
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200258 // ------------------------------------------------------------------------
259
260 /**
261 * Get all data
262 *
263 * Internal method to get all the relevant data about a cache item
264 *
265 * @param string $id Cache ID
266 * @return mixed Data array on success or FALSE on failure
267 */
268 protected function _get($id)
269 {
Andrey Andreev4dac6eb2015-08-31 17:52:27 +0300270 if ( ! is_file($this->_cache_path.$id))
Andrey Andreev43d7fa72014-01-09 17:29:45 +0200271 {
272 return FALSE;
273 }
274
275 $data = unserialize(file_get_contents($this->_cache_path.$id));
276
277 if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
278 {
279 unlink($this->_cache_path.$id);
280 return FALSE;
281 }
282
283 return $data;
284 }
285
Greg Akerbde25d92010-12-21 09:31:21 -0600286}