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