blob: 13e2d1af67e9e541091f1176f76b640f8300b1c0 [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
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Memcached Caching Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Core
24 * @author ExpressionEngine Dev Team
25 * @link
26 */
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');
39
40 $path = $CI->config->item('cache_path');
41
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 }
59
60 $data = read_file($this->_cache_path.$id);
61 $data = unserialize($data);
62
63 if (time() > $data['time'] + $data['ttl'])
64 {
65 unlink($this->_cache_path.$id);
66 return FALSE;
67 }
68
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
79 * @param int length of time (in seconds) the cache is valid
80 * - Default is 60 seconds
81 * @return boolean true on success/false on failure
82 */
83 public function save($id, $data, $ttl = 60)
84 {
85 $contents = array(
86 'time' => time(),
87 'ttl' => $ttl,
88 'data' => $data
89 );
90
91 if (write_file($this->_cache_path.$id, serialize($contents)))
92 {
93 @chmod($this->_cache_path.$id, 0777);
94 return TRUE;
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 {
110 return unlink($this->_cache_path.$id);
111 }
112
113 // ------------------------------------------------------------------------
114
115 /**
116 * Clean the Cache
117 *
118 * @return boolean false on failure/true on success
119 */
120 public function clean()
121 {
122 return delete_files($this->_cache_path);
123 }
124
125 // ------------------------------------------------------------------------
126
127 /**
128 * Cache Info
129 *
130 * Not supported by file-based caching
131 *
132 * @param string user/filehits
133 * @return mixed FALSE
134 */
135 public function cache_info($type = NULL)
136 {
137 return get_dir_file_info($this->_cache_path);
138 }
139
140 // ------------------------------------------------------------------------
141
142 /**
143 * Get Cache Metadata
144 *
145 * @param mixed key to get cache metadata on
146 * @return mixed FALSE on failure, array on success.
147 */
148 public function get_metadata($id)
149 {
150 if ( ! file_exists($this->_cache_path.$id))
151 {
152 return FALSE;
153 }
154
155 $data = read_file($this->_cache_path.$id);
156 $data = unserialize($data);
157
158 if (is_array($data))
159 {
160 $data = $data['data'];
161 $mtime = filemtime($this->_cache_path.$id);
162
163 if ( ! isset($data['ttl']))
164 {
165 return FALSE;
166 }
167
168 return array(
169 'expire' => $mtime + $data['ttl'],
170 'mtime' => $mtime
171 );
172 }
173
174 return FALSE;
175 }
176
177 // ------------------------------------------------------------------------
178
179 /**
180 * Is supported
181 *
182 * In the file driver, check to see that the cache directory is indeed writable
183 *
184 * @return boolean
185 */
186 public function is_supported()
187 {
188 return is_really_writable($this->_cache_path);
189 }
190
191 // ------------------------------------------------------------------------
192}
193// End Class
194
195/* End of file Cache_file.php */
196/* Location: ./system/libraries/Cache/drivers/Cache_file.php */