Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 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
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 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/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 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
/**
41
 * CodeIgniter File Caching Class
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Libraries
45
 * @category	Core
46
 * @author		EllisLab Dev Team
47
 * @link
48
 */
49
class CI_Cache_file extends CI_Driver {
50
 
51
	/**
52
	 * Directory in which to save cache files
53
	 *
54
	 * @var string
55
	 */
56
	protected $_cache_path;
57
 
58
	/**
59
	 * Initialize file-based cache
60
	 *
61
	 * @return	void
62
	 */
63
	public function __construct()
64
	{
65
		$CI =& get_instance();
66
		$CI->load->helper('file');
67
		$path = $CI->config->item('cache_path');
68
		$this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path;
69
	}
70
 
71
	// ------------------------------------------------------------------------
72
 
73
	/**
74
	 * Fetch from cache
75
	 *
76
	 * @param	string	$id	Cache ID
77
	 * @return	mixed	Data on success, FALSE on failure
78
	 */
79
	public function get($id)
80
	{
81
		$data = $this->_get($id);
82
		return is_array($data) ? $data['data'] : FALSE;
83
	}
84
 
85
	// ------------------------------------------------------------------------
86
 
87
	/**
88
	 * Save into cache
89
	 *
90
	 * @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
95
	 */
96
	public function save($id, $data, $ttl = 60, $raw = FALSE)
97
	{
98
		$contents = array(
99
			'time'		=> time(),
100
			'ttl'		=> $ttl,
101
			'data'		=> $data
102
		);
103
 
104
		if (write_file($this->_cache_path.$id, serialize($contents)))
105
		{
106
			chmod($this->_cache_path.$id, 0640);
107
			return TRUE;
108
		}
109
 
110
		return FALSE;
111
	}
112
 
113
	// ------------------------------------------------------------------------
114
 
115
	/**
116
	 * Delete from Cache
117
	 *
118
	 * @param	mixed	unique identifier of item in cache
119
	 * @return	bool	true on success/false on failure
120
	 */
121
	public function delete($id)
122
	{
1257 lars 123
		return is_file($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE;
68 lars 124
	}
125
 
126
	// ------------------------------------------------------------------------
127
 
128
	/**
129
	 * 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
 
139
		if ($data === FALSE)
140
		{
141
			$data = array('data' => 0, 'ttl' => 60);
142
		}
143
		elseif ( ! is_int($data['data']))
144
		{
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
 
167
		if ($data === FALSE)
168
		{
169
			$data = array('data' => 0, 'ttl' => 60);
170
		}
171
		elseif ( ! is_int($data['data']))
172
		{
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
	/**
185
	 * Clean the Cache
186
	 *
187
	 * @return	bool	false on failure/true on success
188
	 */
189
	public function clean()
190
	{
191
		return delete_files($this->_cache_path, FALSE, TRUE);
192
	}
193
 
194
	// ------------------------------------------------------------------------
195
 
196
	/**
197
	 * Cache Info
198
	 *
199
	 * Not supported by file-based caching
200
	 *
201
	 * @param	string	user/filehits
202
	 * @return	mixed	FALSE
203
	 */
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
	 *
214
	 * @param	mixed	key to get cache metadata on
215
	 * @return	mixed	FALSE on failure, array on success.
216
	 */
217
	public function get_metadata($id)
218
	{
1257 lars 219
		if ( ! is_file($this->_cache_path.$id))
68 lars 220
		{
221
			return FALSE;
222
		}
223
 
224
		$data = unserialize(file_get_contents($this->_cache_path.$id));
225
 
226
		if (is_array($data))
227
		{
228
			$mtime = filemtime($this->_cache_path.$id);
229
 
1257 lars 230
			if ( ! isset($data['ttl'], $data['time']))
68 lars 231
			{
232
				return FALSE;
233
			}
234
 
235
			return array(
1257 lars 236
				'expire' => $data['time'] + $data['ttl'],
68 lars 237
				'mtime'	 => $mtime
238
			);
239
		}
240
 
241
		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
250
	 *
251
	 * @return	bool
252
	 */
253
	public function is_supported()
254
	{
255
		return is_really_writable($this->_cache_path);
256
	}
257
 
258
	// ------------------------------------------------------------------------
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
	{
270
		if ( ! is_file($this->_cache_path.$id))
271
		{
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
		{
2414 lars 279
			file_exists($this->_cache_path.$id) && unlink($this->_cache_path.$id);
68 lars 280
			return FALSE;
281
		}
282
 
283
		return $data;
284
	}
285
 
286
}