Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 700205a | 2011-01-28 07:44:28 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 2.0 |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 13 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 19 | * CodeIgniter Memcached Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Libraries |
| 23 | * @category Core |
| 24 | * @author ExpressionEngine Dev Team |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 25 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 26 | */ |
| 27 | |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 28 | class CI_Cache_file extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 29 | |
| 30 | protected $_cache_path; |
| 31 | |
| 32 | /** |
| 33 | * Constructor |
| 34 | */ |
| 35 | public function __construct() |
| 36 | { |
| 37 | $CI =& get_instance(); |
| 38 | $CI->load->helper('file'); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 39 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 40 | $path = $CI->config->item('cache_path'); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 41 | |
Greg Aker | 23351dc | 2011-02-14 00:14:21 -0600 | [diff] [blame] | 42 | $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | |
| 47 | /** |
| 48 | * Fetch from cache |
| 49 | * |
| 50 | * @param mixed unique key id |
| 51 | * @return mixed data on success/false on failure |
| 52 | */ |
| 53 | public function get($id) |
| 54 | { |
| 55 | if ( ! file_exists($this->_cache_path.$id)) |
| 56 | { |
| 57 | return FALSE; |
| 58 | } |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 59 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 60 | $data = read_file($this->_cache_path.$id); |
| 61 | $data = unserialize($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 62 | |
| 63 | if (time() > $data['time'] + $data['ttl']) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 64 | { |
| 65 | unlink($this->_cache_path.$id); |
| 66 | return FALSE; |
| 67 | } |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 68 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 69 | return $data['data']; |
| 70 | } |
| 71 | |
| 72 | // ------------------------------------------------------------------------ |
| 73 | |
| 74 | /** |
| 75 | * Save into cache |
| 76 | * |
| 77 | * @param string unique key |
| 78 | * @param mixed data to store |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 79 | * @param int length of time (in seconds) the cache is valid |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 80 | * - Default is 60 seconds |
| 81 | * @return boolean true on success/false on failure |
| 82 | */ |
| 83 | public function save($id, $data, $ttl = 60) |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 84 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 85 | $contents = array( |
| 86 | 'time' => time(), |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 87 | 'ttl' => $ttl, |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 88 | 'data' => $data |
| 89 | ); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 90 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 91 | if (write_file($this->_cache_path.$id, serialize($contents))) |
| 92 | { |
| 93 | @chmod($this->_cache_path.$id, 0777); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 94 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return FALSE; |
| 98 | } |
| 99 | |
| 100 | // ------------------------------------------------------------------------ |
| 101 | |
| 102 | /** |
| 103 | * Delete from Cache |
| 104 | * |
| 105 | * @param mixed unique identifier of item in cache |
| 106 | * @return boolean true on success/false on failure |
| 107 | */ |
| 108 | public function delete($id) |
| 109 | { |
Jeroen van der Gulik | a3a8b61 | 2011-09-16 13:39:30 +0200 | [diff] [blame] | 110 | if (file_exists($this->_cache_path.$id)) |
| 111 | { |
| 112 | return unlink($this->_cache_path.$id); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | return FALSE; |
| 117 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // ------------------------------------------------------------------------ |
| 121 | |
| 122 | /** |
| 123 | * Clean the Cache |
| 124 | * |
| 125 | * @return boolean false on failure/true on success |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 126 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 127 | public function clean() |
| 128 | { |
| 129 | return delete_files($this->_cache_path); |
| 130 | } |
| 131 | |
| 132 | // ------------------------------------------------------------------------ |
| 133 | |
| 134 | /** |
| 135 | * Cache Info |
| 136 | * |
| 137 | * Not supported by file-based caching |
| 138 | * |
| 139 | * @param string user/filehits |
| 140 | * @return mixed FALSE |
| 141 | */ |
| 142 | public function cache_info($type = NULL) |
| 143 | { |
| 144 | return get_dir_file_info($this->_cache_path); |
| 145 | } |
| 146 | |
| 147 | // ------------------------------------------------------------------------ |
| 148 | |
| 149 | /** |
| 150 | * Get Cache Metadata |
| 151 | * |
| 152 | * @param mixed key to get cache metadata on |
| 153 | * @return mixed FALSE on failure, array on success. |
| 154 | */ |
| 155 | public function get_metadata($id) |
| 156 | { |
| 157 | if ( ! file_exists($this->_cache_path.$id)) |
| 158 | { |
| 159 | return FALSE; |
| 160 | } |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 161 | |
| 162 | $data = read_file($this->_cache_path.$id); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 163 | $data = unserialize($data); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 164 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 165 | if (is_array($data)) |
| 166 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 167 | $mtime = filemtime($this->_cache_path.$id); |
| 168 | |
Ben Edmunds | 80f4c34 | 2011-08-20 14:11:21 -0500 | [diff] [blame] | 169 | if ( ! isset($data['data']['ttl'])) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 170 | { |
| 171 | return FALSE; |
| 172 | } |
| 173 | |
| 174 | return array( |
Ben Edmunds | 3db2849 | 2011-08-20 14:12:12 -0500 | [diff] [blame] | 175 | 'expire' => $mtime + $data['data']['ttl'], |
| 176 | 'mtime' => $mtime |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 177 | ); |
| 178 | } |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 179 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 180 | return FALSE; |
| 181 | } |
| 182 | |
| 183 | // ------------------------------------------------------------------------ |
| 184 | |
| 185 | /** |
| 186 | * Is supported |
| 187 | * |
| 188 | * In the file driver, check to see that the cache directory is indeed writable |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 189 | * |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 190 | * @return boolean |
| 191 | */ |
| 192 | public function is_supported() |
| 193 | { |
| 194 | return is_really_writable($this->_cache_path); |
| 195 | } |
| 196 | |
| 197 | // ------------------------------------------------------------------------ |
| 198 | } |
| 199 | // End Class |
| 200 | |
| 201 | /* End of file Cache_file.php */ |
Jeroen van der Gulik | a3a8b61 | 2011-09-16 13:39:30 +0200 | [diff] [blame] | 202 | /* Location: ./system/libraries/Cache/drivers/Cache_file.php */ |