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 | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 21 | * @copyright Copyright (c) 2008 - 2012, 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 | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 65 | * @param mixed unique key 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 | { |
| 70 | if ( ! file_exists($this->_cache_path.$id)) |
| 71 | { |
| 72 | return FALSE; |
| 73 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 74 | |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 75 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 76 | |
Andrey Andreev | 0159ae7 | 2012-06-17 02:33:43 +0300 | [diff] [blame] | 77 | if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl']) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 78 | { |
| 79 | unlink($this->_cache_path.$id); |
| 80 | return FALSE; |
| 81 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 82 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 83 | return $data['data']; |
| 84 | } |
| 85 | |
| 86 | // ------------------------------------------------------------------------ |
| 87 | |
| 88 | /** |
| 89 | * Save into cache |
| 90 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 91 | * @param string unique key |
| 92 | * @param mixed data to store |
| 93 | * @param int length of time (in seconds) the cache is valid |
| 94 | * - Default is 60 seconds |
| 95 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 96 | */ |
| 97 | public function save($id, $data, $ttl = 60) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 98 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 99 | $contents = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 100 | 'time' => time(), |
| 101 | 'ttl' => $ttl, |
| 102 | 'data' => $data |
| 103 | ); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 104 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 105 | if (write_file($this->_cache_path.$id, serialize($contents))) |
| 106 | { |
Yorick Peterse | 6e09f23 | 2012-02-15 18:23:08 +0100 | [diff] [blame] | 107 | @chmod($this->_cache_path.$id, 0660); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 108 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return FALSE; |
| 112 | } |
| 113 | |
| 114 | // ------------------------------------------------------------------------ |
| 115 | |
| 116 | /** |
| 117 | * Delete from Cache |
| 118 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 119 | * @param mixed unique identifier of item in cache |
| 120 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 121 | */ |
| 122 | public function delete($id) |
| 123 | { |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 124 | 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] | 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------ |
| 128 | |
| 129 | /** |
| 130 | * Clean the Cache |
| 131 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 132 | * @return bool false on failure/true on success |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 133 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 134 | public function clean() |
| 135 | { |
| 136 | return delete_files($this->_cache_path); |
| 137 | } |
| 138 | |
| 139 | // ------------------------------------------------------------------------ |
| 140 | |
| 141 | /** |
| 142 | * Cache Info |
| 143 | * |
| 144 | * Not supported by file-based caching |
| 145 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 146 | * @param string user/filehits |
| 147 | * @return mixed FALSE |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 148 | */ |
| 149 | public function cache_info($type = NULL) |
| 150 | { |
| 151 | return get_dir_file_info($this->_cache_path); |
| 152 | } |
| 153 | |
| 154 | // ------------------------------------------------------------------------ |
| 155 | |
| 156 | /** |
| 157 | * Get Cache Metadata |
| 158 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 159 | * @param mixed key to get cache metadata on |
| 160 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 161 | */ |
| 162 | public function get_metadata($id) |
| 163 | { |
| 164 | if ( ! file_exists($this->_cache_path.$id)) |
| 165 | { |
| 166 | return FALSE; |
| 167 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 168 | |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame] | 169 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 170 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 171 | if (is_array($data)) |
| 172 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 173 | $mtime = filemtime($this->_cache_path.$id); |
| 174 | |
Burak Erdem | 0b48dd4 | 2012-02-06 17:28:33 +0200 | [diff] [blame] | 175 | if ( ! isset($data['ttl'])) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 176 | { |
| 177 | return FALSE; |
| 178 | } |
| 179 | |
| 180 | return array( |
Burak Erdem | 0b48dd4 | 2012-02-06 17:28:33 +0200 | [diff] [blame] | 181 | 'expire' => $mtime + $data['ttl'], |
Ben Edmunds | 3db2849 | 2011-08-20 14:12:12 -0500 | [diff] [blame] | 182 | 'mtime' => $mtime |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 183 | ); |
| 184 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 185 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 186 | return FALSE; |
| 187 | } |
| 188 | |
| 189 | // ------------------------------------------------------------------------ |
| 190 | |
| 191 | /** |
| 192 | * Is supported |
| 193 | * |
| 194 | * 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] | 195 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 196 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 197 | */ |
| 198 | public function is_supported() |
| 199 | { |
| 200 | return is_really_writable($this->_cache_path); |
| 201 | } |
| 202 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 203 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 204 | |
| 205 | /* End of file Cache_file.php */ |
Andrey Andreev | c9195a7 | 2012-06-12 03:49:03 +0300 | [diff] [blame] | 206 | /* Location: ./system/libraries/Cache/drivers/Cache_file.php */ |