Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [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 | * |
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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2006 - 2012 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 |
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 | */ |
| 27 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 28 | /** |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 29 | * CodeIgniter Memcached Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 35 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 36 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 37 | class CI_Cache_file extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 38 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 39 | /** |
| 40 | * Directory in which to save cache files |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 44 | protected $_cache_path; |
| 45 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 46 | /** |
| 47 | * Initialize file-based cache |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 48 | * |
| 49 | * @return void |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 50 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 51 | public function __construct() |
| 52 | { |
| 53 | $CI =& get_instance(); |
| 54 | $CI->load->helper('file'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 55 | $path = $CI->config->item('cache_path'); |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 56 | $this->_cache_path = ($path === '') ? APPPATH.'cache/' : $path; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | |
| 61 | /** |
| 62 | * Fetch from cache |
| 63 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 64 | * @param mixed unique key id |
| 65 | * @return mixed data on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 66 | */ |
| 67 | public function get($id) |
| 68 | { |
| 69 | if ( ! file_exists($this->_cache_path.$id)) |
| 70 | { |
| 71 | return FALSE; |
| 72 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 73 | |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame^] | 74 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 75 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 76 | if (time() > $data['time'] + $data['ttl']) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 77 | { |
| 78 | unlink($this->_cache_path.$id); |
| 79 | return FALSE; |
| 80 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 81 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 82 | return $data['data']; |
| 83 | } |
| 84 | |
| 85 | // ------------------------------------------------------------------------ |
| 86 | |
| 87 | /** |
| 88 | * Save into cache |
| 89 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 90 | * @param string unique key |
| 91 | * @param mixed data to store |
| 92 | * @param int length of time (in seconds) the cache is valid |
| 93 | * - Default is 60 seconds |
| 94 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 95 | */ |
| 96 | public function save($id, $data, $ttl = 60) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 97 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 98 | $contents = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 99 | 'time' => time(), |
| 100 | 'ttl' => $ttl, |
| 101 | 'data' => $data |
| 102 | ); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 103 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 104 | if (write_file($this->_cache_path.$id, serialize($contents))) |
| 105 | { |
Yorick Peterse | 6e09f23 | 2012-02-15 18:23:08 +0100 | [diff] [blame] | 106 | @chmod($this->_cache_path.$id, 0660); |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 107 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return FALSE; |
| 111 | } |
| 112 | |
| 113 | // ------------------------------------------------------------------------ |
| 114 | |
| 115 | /** |
| 116 | * Delete from Cache |
| 117 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 118 | * @param mixed unique identifier of item in cache |
| 119 | * @return bool true on success/false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 120 | */ |
| 121 | public function delete($id) |
| 122 | { |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 123 | 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] | 124 | } |
| 125 | |
| 126 | // ------------------------------------------------------------------------ |
| 127 | |
| 128 | /** |
| 129 | * Clean the Cache |
| 130 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 131 | * @return bool false on failure/true on success |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 132 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 133 | public function clean() |
| 134 | { |
| 135 | return delete_files($this->_cache_path); |
| 136 | } |
| 137 | |
| 138 | // ------------------------------------------------------------------------ |
| 139 | |
| 140 | /** |
| 141 | * Cache Info |
| 142 | * |
| 143 | * Not supported by file-based caching |
| 144 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 145 | * @param string user/filehits |
| 146 | * @return mixed FALSE |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 147 | */ |
| 148 | public function cache_info($type = NULL) |
| 149 | { |
| 150 | return get_dir_file_info($this->_cache_path); |
| 151 | } |
| 152 | |
| 153 | // ------------------------------------------------------------------------ |
| 154 | |
| 155 | /** |
| 156 | * Get Cache Metadata |
| 157 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 158 | * @param mixed key to get cache metadata on |
| 159 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 160 | */ |
| 161 | public function get_metadata($id) |
| 162 | { |
| 163 | if ( ! file_exists($this->_cache_path.$id)) |
| 164 | { |
| 165 | return FALSE; |
| 166 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 167 | |
Andrey Andreev | 0f0b769 | 2012-06-07 14:57:04 +0300 | [diff] [blame^] | 168 | $data = unserialize(file_get_contents($this->_cache_path.$id)); |
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 | if (is_array($data)) |
| 171 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 172 | $mtime = filemtime($this->_cache_path.$id); |
| 173 | |
Ben Edmunds | 80f4c34 | 2011-08-20 14:11:21 -0500 | [diff] [blame] | 174 | if ( ! isset($data['data']['ttl'])) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 175 | { |
| 176 | return FALSE; |
| 177 | } |
| 178 | |
| 179 | return array( |
Ben Edmunds | 3db2849 | 2011-08-20 14:12:12 -0500 | [diff] [blame] | 180 | 'expire' => $mtime + $data['data']['ttl'], |
| 181 | 'mtime' => $mtime |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 182 | ); |
| 183 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 184 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 185 | return FALSE; |
| 186 | } |
| 187 | |
| 188 | // ------------------------------------------------------------------------ |
| 189 | |
| 190 | /** |
| 191 | * Is supported |
| 192 | * |
| 193 | * 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] | 194 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 195 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 196 | */ |
| 197 | public function is_supported() |
| 198 | { |
| 199 | return is_really_writable($this->_cache_path); |
| 200 | } |
| 201 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 202 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 203 | |
| 204 | /* End of file Cache_file.php */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 205 | /* Location: ./system/libraries/Cache/drivers/Cache_file.php */ |