blob: be392d3d2d4d062fe0cb7c92ba8ed2107f7b8561 [file] [log] [blame]
Derek Jonesf4a4bd82011-10-20 12:18:42 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05005 * 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 Akerbde25d92010-12-21 09:31:21 -060018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Derek Jones700205a2011-01-28 07:44:28 -060021 * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Greg Akerbde25d92010-12-21 09:31:21 -060023 * @link http://codeigniter.com
24 * @since Version 2.0
Derek Jones37f4b9c2011-07-01 17:56:50 -050025 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
27
28// ------------------------------------------------------------------------
29
30/**
Derek Jones37f4b9c2011-07-01 17:56:50 -050031 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Jones37f4b9c2011-07-01 17:56:50 -050037 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060038 */
39
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010040class CI_Cache_file extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060041
42 protected $_cache_path;
43
44 /**
45 * Constructor
46 */
47 public function __construct()
48 {
49 $CI =& get_instance();
50 $CI->load->helper('file');
Derek Jones37f4b9c2011-07-01 17:56:50 -050051
Greg Akerbde25d92010-12-21 09:31:21 -060052 $path = $CI->config->item('cache_path');
Derek Jones37f4b9c2011-07-01 17:56:50 -050053
Greg Aker23351dc2011-02-14 00:14:21 -060054 $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060055 }
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 Jones37f4b9c2011-07-01 17:56:50 -050071
Greg Akerbde25d92010-12-21 09:31:21 -060072 $data = read_file($this->_cache_path.$id);
73 $data = unserialize($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -050074
75 if (time() > $data['time'] + $data['ttl'])
Greg Akerbde25d92010-12-21 09:31:21 -060076 {
77 unlink($this->_cache_path.$id);
78 return FALSE;
79 }
Derek Jones37f4b9c2011-07-01 17:56:50 -050080
Greg Akerbde25d92010-12-21 09:31:21 -060081 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 Jones37f4b9c2011-07-01 17:56:50 -050091 * @param int length of time (in seconds) the cache is valid
Greg Akerbde25d92010-12-21 09:31:21 -060092 * - Default is 60 seconds
93 * @return boolean true on success/false on failure
94 */
95 public function save($id, $data, $ttl = 60)
Derek Jones37f4b9c2011-07-01 17:56:50 -050096 {
Greg Akerbde25d92010-12-21 09:31:21 -060097 $contents = array(
98 'time' => time(),
Derek Jones37f4b9c2011-07-01 17:56:50 -050099 'ttl' => $ttl,
Greg Akerbde25d92010-12-21 09:31:21 -0600100 'data' => $data
101 );
Derek Jones37f4b9c2011-07-01 17:56:50 -0500102
Greg Akerbde25d92010-12-21 09:31:21 -0600103 if (write_file($this->_cache_path.$id, serialize($contents)))
104 {
105 @chmod($this->_cache_path.$id, 0777);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500106 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600107 }
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 Gulika3a8b612011-09-16 13:39:30 +0200122 if (file_exists($this->_cache_path.$id))
123 {
124 return unlink($this->_cache_path.$id);
125 }
126 else
127 {
128 return FALSE;
129 }
Greg Akerbde25d92010-12-21 09:31:21 -0600130 }
131
132 // ------------------------------------------------------------------------
133
134 /**
135 * Clean the Cache
136 *
137 * @return boolean false on failure/true on success
Derek Jones37f4b9c2011-07-01 17:56:50 -0500138 */
Greg Akerbde25d92010-12-21 09:31:21 -0600139 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 Jones37f4b9c2011-07-01 17:56:50 -0500173
174 $data = read_file($this->_cache_path.$id);
Greg Akerbde25d92010-12-21 09:31:21 -0600175 $data = unserialize($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500176
Greg Akerbde25d92010-12-21 09:31:21 -0600177 if (is_array($data))
178 {
Greg Akerbde25d92010-12-21 09:31:21 -0600179 $mtime = filemtime($this->_cache_path.$id);
180
Ben Edmunds80f4c342011-08-20 14:11:21 -0500181 if ( ! isset($data['data']['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600182 {
183 return FALSE;
184 }
185
186 return array(
Ben Edmunds3db28492011-08-20 14:12:12 -0500187 'expire' => $mtime + $data['data']['ttl'],
188 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600189 );
190 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500191
Greg Akerbde25d92010-12-21 09:31:21 -0600192 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 Jones37f4b9c2011-07-01 17:56:50 -0500201 *
Greg Akerbde25d92010-12-21 09:31:21 -0600202 * @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 Gulika3a8b612011-09-16 13:39:30 +0200214/* Location: ./system/libraries/Cache/drivers/Cache_file.php */