Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 6 | * |
| 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 18 | * |
| 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 2.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 25 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 28 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 29 | /** |
Andrey Andreev | c9195a7 | 2012-06-12 03:49:03 +0300 | [diff] [blame] | 30 | * CodeIgniter File Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 36 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 37 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 38 | class CI_Cache_file extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 39 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 40 | /** |
| 41 | * Directory in which to save cache files |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 45 | protected $_cache_path; |
| 46 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 47 | /** |
| 48 | * Initialize file-based cache |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 49 | * |
| 50 | * @return void |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 51 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 52 | public function __construct() |
| 53 | { |
| 54 | $CI =& get_instance(); |
| 55 | $CI->load->helper('file'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 56 | $path = $CI->config->item('cache_path'); |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 57 | $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // ------------------------------------------------------------------------ |
| 61 | |
| 62 | /** |
| 63 | * Fetch from cache |
| 64 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 65 | * @param string $id Cache ID |
| 66 | * @return mixed Data on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 67 | */ |
| 68 | public function get($id) |
| 69 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 70 | $data = $this->_get($id); |
| 71 | return is_array($data) ? $data['data'] : FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // ------------------------------------------------------------------------ |
| 75 | |
| 76 | /** |
| 77 | * Save into cache |
| 78 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 79 | * @param string $id Cache ID |
| 80 | * @param mixed $data Data to store |
| 81 | * @param int $ttl Time to live in seconds |
| 82 | * @param bool $raw Whether to store the raw value (unused) |
| 83 | * @return bool TRUE on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 84 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 85 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 86 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 87 | $contents = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 88 | 'time' => time(), |
| 89 | 'ttl' => $ttl, |
| 90 | 'data' => $data |
| 91 | ); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 92 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 93 | if (write_file($this->_cache_path.$id, serialize($contents))) |
| 94 | { |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame^] | 95 | chmod($this->_cache_path.$id, 0640); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 96 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return FALSE; |
| 100 | } |
| 101 | |
| 102 | // ------------------------------------------------------------------------ |
| 103 | |
| 104 | /** |
| 105 | * Delete from Cache |
| 106 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 107 | * @param mixed unique identifier of item in cache |
| 108 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 109 | */ |
| 110 | public function delete($id) |
| 111 | { |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 112 | return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // ------------------------------------------------------------------------ |
| 116 | |
| 117 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 118 | * Increment a raw value |
| 119 | * |
| 120 | * @param string $id Cache ID |
| 121 | * @param int $offset Step/value to add |
| 122 | * @return New value on success, FALSE on failure |
| 123 | */ |
| 124 | public function increment($id, $offset = 1) |
| 125 | { |
| 126 | $data = $this->_get($id); |
| 127 | |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 128 | if ($data === FALSE) |
| 129 | { |
| 130 | $data = array('data' => 0, 'ttl' => 60); |
| 131 | } |
| 132 | elseif ( ! is_int($data['data'])) |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 133 | { |
| 134 | return FALSE; |
| 135 | } |
| 136 | |
| 137 | $new_value = $data['data'] + $offset; |
| 138 | return $this->save($id, $new_value, $data['ttl']) |
| 139 | ? $new_value |
| 140 | : FALSE; |
| 141 | } |
| 142 | |
| 143 | // ------------------------------------------------------------------------ |
| 144 | |
| 145 | /** |
| 146 | * Decrement a raw value |
| 147 | * |
| 148 | * @param string $id Cache ID |
| 149 | * @param int $offset Step/value to reduce by |
| 150 | * @return New value on success, FALSE on failure |
| 151 | */ |
| 152 | public function decrement($id, $offset = 1) |
| 153 | { |
| 154 | $data = $this->_get($id); |
| 155 | |
Andrey Andreev | 5f0799a | 2014-07-31 13:32:41 +0300 | [diff] [blame] | 156 | if ($data === FALSE) |
| 157 | { |
| 158 | $data = array('data' => 0, 'ttl' => 60); |
| 159 | } |
| 160 | elseif ( ! is_int($data['data'])) |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 161 | { |
| 162 | return FALSE; |
| 163 | } |
| 164 | |
| 165 | $new_value = $data['data'] - $offset; |
| 166 | return $this->save($id, $new_value, $data['ttl']) |
| 167 | ? $new_value |
| 168 | : FALSE; |
| 169 | } |
| 170 | |
| 171 | // ------------------------------------------------------------------------ |
| 172 | |
| 173 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 174 | * Clean the Cache |
| 175 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 176 | * @return bool false on failure/true on success |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 177 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 178 | public function clean() |
| 179 | { |
vlakoff | 83c344e | 2013-03-04 14:30:08 +0100 | [diff] [blame] | 180 | return delete_files($this->_cache_path, FALSE, TRUE); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // ------------------------------------------------------------------------ |
| 184 | |
| 185 | /** |
| 186 | * Cache Info |
| 187 | * |
| 188 | * Not supported by file-based caching |
| 189 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 190 | * @param string user/filehits |
| 191 | * @return mixed FALSE |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 192 | */ |
| 193 | public function cache_info($type = NULL) |
| 194 | { |
| 195 | return get_dir_file_info($this->_cache_path); |
| 196 | } |
| 197 | |
| 198 | // ------------------------------------------------------------------------ |
| 199 | |
| 200 | /** |
| 201 | * Get Cache Metadata |
| 202 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 203 | * @param mixed key to get cache metadata on |
| 204 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 205 | */ |
| 206 | public function get_metadata($id) |
| 207 | { |
| 208 | if ( ! file_exists($this->_cache_path.$id)) |
| 209 | { |
| 210 | return FALSE; |
| 211 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 212 | |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 213 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 214 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 215 | if (is_array($data)) |
| 216 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 217 | $mtime = filemtime($this->_cache_path.$id); |
| 218 | |
Burak Erdem | 0b48dd4 | 2012-02-06 17:28:33 +0200 | [diff] [blame] | 219 | if ( ! isset($data['ttl'])) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 220 | { |
| 221 | return FALSE; |
| 222 | } |
| 223 | |
| 224 | return array( |
Burak Erdem | 0b48dd4 | 2012-02-06 17:28:33 +0200 | [diff] [blame] | 225 | 'expire' => $mtime + $data['ttl'], |
Ben Edmunds | 3db2849 | 2011-08-20 14:12:12 -0500 | [diff] [blame] | 226 | 'mtime' => $mtime |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 227 | ); |
| 228 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 229 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 230 | return FALSE; |
| 231 | } |
| 232 | |
| 233 | // ------------------------------------------------------------------------ |
| 234 | |
| 235 | /** |
| 236 | * Is supported |
| 237 | * |
| 238 | * In the file driver, check to see that the cache directory is indeed writable |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 239 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 240 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 241 | */ |
| 242 | public function is_supported() |
| 243 | { |
| 244 | return is_really_writable($this->_cache_path); |
| 245 | } |
| 246 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 247 | // ------------------------------------------------------------------------ |
| 248 | |
| 249 | /** |
| 250 | * Get all data |
| 251 | * |
| 252 | * Internal method to get all the relevant data about a cache item |
| 253 | * |
| 254 | * @param string $id Cache ID |
| 255 | * @return mixed Data array on success or FALSE on failure |
| 256 | */ |
| 257 | protected function _get($id) |
| 258 | { |
| 259 | if ( ! file_exists($this->_cache_path.$id)) |
| 260 | { |
| 261 | return FALSE; |
| 262 | } |
| 263 | |
| 264 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
| 265 | |
| 266 | if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl']) |
| 267 | { |
| 268 | unlink($this->_cache_path.$id); |
| 269 | return FALSE; |
| 270 | } |
| 271 | |
| 272 | return $data; |
| 273 | } |
| 274 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 275 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 276 | |
| 277 | /* End of file Cache_file.php */ |
Andrey Andreev | c9195a7 | 2012-06-12 03:49:03 +0300 | [diff] [blame] | 278 | /* Location: ./system/libraries/Cache/drivers/Cache_file.php */ |