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 |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, 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 | { |
Yorick Peterse | 6e09f23 | 2012-02-15 18:23:08 +0100 | [diff] [blame] | 95 | @chmod($this->_cache_path.$id, 0660); |
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 | |
| 128 | if ($data === FALSE OR ! is_int($data['data'])) |
| 129 | { |
| 130 | return FALSE; |
| 131 | } |
| 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 | |
| 141 | /** |
| 142 | * Decrement a raw value |
| 143 | * |
| 144 | * @param string $id Cache ID |
| 145 | * @param int $offset Step/value to reduce by |
| 146 | * @return New value on success, FALSE on failure |
| 147 | */ |
| 148 | public function decrement($id, $offset = 1) |
| 149 | { |
| 150 | $data = $this->_get($id); |
| 151 | |
| 152 | if ($data === FALSE OR ! is_int($data['data'])) |
| 153 | { |
| 154 | return FALSE; |
| 155 | } |
| 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 | |
| 165 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 166 | * Clean the Cache |
| 167 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 168 | * @return bool false on failure/true on success |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 169 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 170 | public function clean() |
| 171 | { |
vlakoff | 83c344e | 2013-03-04 14:30:08 +0100 | [diff] [blame] | 172 | return delete_files($this->_cache_path, FALSE, TRUE); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // ------------------------------------------------------------------------ |
| 176 | |
| 177 | /** |
| 178 | * Cache Info |
| 179 | * |
| 180 | * Not supported by file-based caching |
| 181 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 182 | * @param string user/filehits |
| 183 | * @return mixed FALSE |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 184 | */ |
| 185 | public function cache_info($type = NULL) |
| 186 | { |
| 187 | return get_dir_file_info($this->_cache_path); |
| 188 | } |
| 189 | |
| 190 | // ------------------------------------------------------------------------ |
| 191 | |
| 192 | /** |
| 193 | * Get Cache Metadata |
| 194 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 195 | * @param mixed key to get cache metadata on |
| 196 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 197 | */ |
| 198 | public function get_metadata($id) |
| 199 | { |
| 200 | if ( ! file_exists($this->_cache_path.$id)) |
| 201 | { |
| 202 | return FALSE; |
| 203 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 204 | |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 205 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 206 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 207 | if (is_array($data)) |
| 208 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 209 | $mtime = filemtime($this->_cache_path.$id); |
| 210 | |
Burak Erdem | 0b48dd4 | 2012-02-06 17:28:33 +0200 | [diff] [blame] | 211 | if ( ! isset($data['ttl'])) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 212 | { |
| 213 | return FALSE; |
| 214 | } |
| 215 | |
| 216 | return array( |
Burak Erdem | 0b48dd4 | 2012-02-06 17:28:33 +0200 | [diff] [blame] | 217 | 'expire' => $mtime + $data['ttl'], |
Ben Edmunds | 3db2849 | 2011-08-20 14:12:12 -0500 | [diff] [blame] | 218 | 'mtime' => $mtime |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 219 | ); |
| 220 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 221 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 222 | return FALSE; |
| 223 | } |
| 224 | |
| 225 | // ------------------------------------------------------------------------ |
| 226 | |
| 227 | /** |
| 228 | * Is supported |
| 229 | * |
| 230 | * 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] | 231 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 232 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 233 | */ |
| 234 | public function is_supported() |
| 235 | { |
| 236 | return is_really_writable($this->_cache_path); |
| 237 | } |
| 238 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame^] | 239 | // ------------------------------------------------------------------------ |
| 240 | |
| 241 | /** |
| 242 | * Get all data |
| 243 | * |
| 244 | * Internal method to get all the relevant data about a cache item |
| 245 | * |
| 246 | * @param string $id Cache ID |
| 247 | * @return mixed Data array on success or FALSE on failure |
| 248 | */ |
| 249 | protected function _get($id) |
| 250 | { |
| 251 | if ( ! file_exists($this->_cache_path.$id)) |
| 252 | { |
| 253 | return FALSE; |
| 254 | } |
| 255 | |
| 256 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
| 257 | |
| 258 | if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl']) |
| 259 | { |
| 260 | unlink($this->_cache_path.$id); |
| 261 | return FALSE; |
| 262 | } |
| 263 | |
| 264 | return $data; |
| 265 | } |
| 266 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 267 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 268 | |
| 269 | /* End of file Cache_file.php */ |
Andrey Andreev | c9195a7 | 2012-06-12 03:49:03 +0300 | [diff] [blame] | 270 | /* Location: ./system/libraries/Cache/drivers/Cache_file.php */ |