blob: 2a89faf0921d2d4d292acc129d610a0aa414307f [file] [log] [blame]
Greg Akerbde25d92010-12-21 09:31:21 -06001<?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 Jones700205a2011-01-28 07:44:28 -06009 * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
Greg Akerbde25d92010-12-21 09:31:21 -060010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 2.0
Derek Jones37f4b9c2011-07-01 17:56:50 -050013 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060014 */
15
16// ------------------------------------------------------------------------
17
18/**
Derek Jones37f4b9c2011-07-01 17:56:50 -050019 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060020 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Core
24 * @author ExpressionEngine Dev Team
Derek Jones37f4b9c2011-07-01 17:56:50 -050025 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
27
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010028class CI_Cache_file extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060029
30 protected $_cache_path;
31
32 /**
33 * Constructor
34 */
35 public function __construct()
36 {
37 $CI =& get_instance();
38 $CI->load->helper('file');
Derek Jones37f4b9c2011-07-01 17:56:50 -050039
Greg Akerbde25d92010-12-21 09:31:21 -060040 $path = $CI->config->item('cache_path');
Derek Jones37f4b9c2011-07-01 17:56:50 -050041
Greg Aker23351dc2011-02-14 00:14:21 -060042 $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
Greg Akerbde25d92010-12-21 09:31:21 -060043 }
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 Jones37f4b9c2011-07-01 17:56:50 -050059
Greg Akerbde25d92010-12-21 09:31:21 -060060 $data = read_file($this->_cache_path.$id);
61 $data = unserialize($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -050062
63 if (time() > $data['time'] + $data['ttl'])
Greg Akerbde25d92010-12-21 09:31:21 -060064 {
65 unlink($this->_cache_path.$id);
66 return FALSE;
67 }
Derek Jones37f4b9c2011-07-01 17:56:50 -050068
Greg Akerbde25d92010-12-21 09:31:21 -060069 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 Jones37f4b9c2011-07-01 17:56:50 -050079 * @param int length of time (in seconds) the cache is valid
Greg Akerbde25d92010-12-21 09:31:21 -060080 * - Default is 60 seconds
81 * @return boolean true on success/false on failure
82 */
83 public function save($id, $data, $ttl = 60)
Derek Jones37f4b9c2011-07-01 17:56:50 -050084 {
Greg Akerbde25d92010-12-21 09:31:21 -060085 $contents = array(
86 'time' => time(),
Derek Jones37f4b9c2011-07-01 17:56:50 -050087 'ttl' => $ttl,
Greg Akerbde25d92010-12-21 09:31:21 -060088 'data' => $data
89 );
Derek Jones37f4b9c2011-07-01 17:56:50 -050090
Greg Akerbde25d92010-12-21 09:31:21 -060091 if (write_file($this->_cache_path.$id, serialize($contents)))
92 {
93 @chmod($this->_cache_path.$id, 0777);
Derek Jones37f4b9c2011-07-01 17:56:50 -050094 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -060095 }
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 Gulika3a8b612011-09-16 13:39:30 +0200110 if (file_exists($this->_cache_path.$id))
111 {
112 return unlink($this->_cache_path.$id);
113 }
114 else
115 {
116 return FALSE;
117 }
Greg Akerbde25d92010-12-21 09:31:21 -0600118 }
119
120 // ------------------------------------------------------------------------
121
122 /**
123 * Clean the Cache
124 *
125 * @return boolean false on failure/true on success
Derek Jones37f4b9c2011-07-01 17:56:50 -0500126 */
Greg Akerbde25d92010-12-21 09:31:21 -0600127 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 Jones37f4b9c2011-07-01 17:56:50 -0500161
162 $data = read_file($this->_cache_path.$id);
Greg Akerbde25d92010-12-21 09:31:21 -0600163 $data = unserialize($data);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500164
Greg Akerbde25d92010-12-21 09:31:21 -0600165 if (is_array($data))
166 {
Greg Akerbde25d92010-12-21 09:31:21 -0600167 $mtime = filemtime($this->_cache_path.$id);
168
Ben Edmunds80f4c342011-08-20 14:11:21 -0500169 if ( ! isset($data['data']['ttl']))
Greg Akerbde25d92010-12-21 09:31:21 -0600170 {
171 return FALSE;
172 }
173
174 return array(
Ben Edmunds3db28492011-08-20 14:12:12 -0500175 'expire' => $mtime + $data['data']['ttl'],
176 'mtime' => $mtime
Greg Akerbde25d92010-12-21 09:31:21 -0600177 );
178 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500179
Greg Akerbde25d92010-12-21 09:31:21 -0600180 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 Jones37f4b9c2011-07-01 17:56:50 -0500189 *
Greg Akerbde25d92010-12-21 09:31:21 -0600190 * @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 Gulika3a8b612011-09-16 13:39:30 +0200202/* Location: ./system/libraries/Cache/drivers/Cache_file.php */