| 12694 |
anikendra |
1 |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
class Cache
|
|
|
6 |
{
|
|
|
7 |
private $_ci;
|
|
|
8 |
private $_path;
|
|
|
9 |
private $_contents;
|
|
|
10 |
private $_filename;
|
|
|
11 |
private $_expires;
|
|
|
12 |
private $_default_expires;
|
|
|
13 |
private $_created;
|
|
|
14 |
private $_dependencies;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Constructor - Initializes and references CI
|
|
|
18 |
*/
|
|
|
19 |
function __construct()
|
|
|
20 |
{
|
|
|
21 |
log_message('debug', "Cache Class Initialized.");
|
|
|
22 |
|
|
|
23 |
$this->_ci =& get_instance();
|
|
|
24 |
$this->_reset();
|
|
|
25 |
|
|
|
26 |
$this->_ci->load->config('cache');
|
|
|
27 |
|
|
|
28 |
$this->_path = $this->_ci->config->item('cache_dir');
|
|
|
29 |
$this->_default_expires = $this->_ci->config->item('cache_default_expires');
|
|
|
30 |
if ( ! is_dir($this->_path))
|
|
|
31 |
{
|
|
|
32 |
show_error("Cache Path not found: $this->_path");
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Initialize Cache object to empty
|
|
|
38 |
*
|
|
|
39 |
* @access private
|
|
|
40 |
* @return void
|
|
|
41 |
*/
|
|
|
42 |
private function _reset()
|
|
|
43 |
{
|
|
|
44 |
$this->_contents = NULL;
|
|
|
45 |
$this->_filename = NULL;
|
|
|
46 |
$this->_expires = NULL;
|
|
|
47 |
$this->_created = NULL;
|
|
|
48 |
$this->_dependencies = array();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Call a library's cached result or create new cache
|
|
|
53 |
*
|
|
|
54 |
* @access public
|
|
|
55 |
* @param string
|
|
|
56 |
* @return array
|
|
|
57 |
*/
|
|
|
58 |
public function library($library, $method, $arguments = array(), $expires = NULL)
|
|
|
59 |
{
|
|
|
60 |
if ( ! class_exists(ucfirst($library)))
|
|
|
61 |
{
|
|
|
62 |
$this->_ci->load->library($library);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
return $this->_call($library, $method, $arguments, $expires);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Call a model's cached result or create new cache
|
|
|
70 |
*
|
|
|
71 |
* @access public
|
|
|
72 |
* @return array
|
|
|
73 |
*/
|
|
|
74 |
public function model($model, $method, $arguments = array(), $expires = NULL)
|
|
|
75 |
{
|
|
|
76 |
if ( ! class_exists(ucfirst($model)))
|
|
|
77 |
{
|
|
|
78 |
$this->_ci->load->model($model);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
return $this->_call($model, $method, $arguments, $expires);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
// Depreciated, use model() or library()
|
|
|
85 |
private function _call($property, $method, $arguments = array(), $expires = NULL)
|
|
|
86 |
{
|
|
|
87 |
$this->_ci->load->helper('security');
|
|
|
88 |
|
|
|
89 |
if ( ! is_array($arguments))
|
|
|
90 |
{
|
|
|
91 |
$arguments = (array) $arguments;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Clean given arguments to a 0-index array
|
|
|
95 |
$arguments = array_values($arguments);
|
|
|
96 |
|
|
|
97 |
$cache_file = $property.DIRECTORY_SEPARATOR.do_hash($method.serialize($arguments), 'sha1');
|
|
|
98 |
|
|
|
99 |
// See if we have this cached or delete if $expires is negative
|
|
|
100 |
if($expires >= 0)
|
|
|
101 |
{
|
|
|
102 |
$cached_response = $this->get($cache_file);
|
|
|
103 |
}
|
|
|
104 |
else
|
|
|
105 |
{
|
|
|
106 |
$this->delete($cache_file);
|
|
|
107 |
return;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
// Not FALSE? Return it
|
|
|
111 |
if($cached_response !== FALSE && $cached_response !== NULL)
|
|
|
112 |
{
|
|
|
113 |
return $cached_response;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
else
|
|
|
117 |
{
|
|
|
118 |
// Call the model or library with the method provided and the same arguments
|
|
|
119 |
$new_response = call_user_func_array(array($this->_ci->$property, $method), $arguments);
|
|
|
120 |
$this->write($new_response, $cache_file, $expires);
|
|
|
121 |
|
|
|
122 |
return $new_response;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Helper functions for the dependencies property
|
|
|
128 |
*/
|
|
|
129 |
function set_dependencies($dependencies)
|
|
|
130 |
{
|
|
|
131 |
if (is_array($dependencies))
|
|
|
132 |
{
|
|
|
133 |
$this->_dependencies = $dependencies;
|
|
|
134 |
}
|
|
|
135 |
else
|
|
|
136 |
{
|
|
|
137 |
$this->_dependencies = array($dependencies);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
return $this;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
function add_dependencies($dependencies)
|
|
|
144 |
{
|
|
|
145 |
if (is_array($dependencies))
|
|
|
146 |
{
|
|
|
147 |
$this->_dependencies = array_merge($this->_dependencies, $dependencies);
|
|
|
148 |
}
|
|
|
149 |
else
|
|
|
150 |
{
|
|
|
151 |
$this->_dependencies[] = $dependencies;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
return $this;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
function get_dependencies() { return $this->_dependencies; }
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Helper function to get the cache creation date
|
|
|
161 |
*/
|
|
|
162 |
function get_created($created) { return $this->_created; }
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Retrieve Cache File
|
|
|
167 |
*
|
|
|
168 |
* @access public
|
|
|
169 |
* @param string
|
|
|
170 |
* @param boolean
|
|
|
171 |
* @return mixed
|
|
|
172 |
*/
|
|
|
173 |
function get($filename = NULL, $use_expires = true)
|
|
|
174 |
{
|
|
|
175 |
// Check if cache was requested with the function or uses this object
|
|
|
176 |
//print_r($filename);
|
|
|
177 |
if ($filename !== NULL)
|
|
|
178 |
{
|
|
|
179 |
$this->_reset();
|
|
|
180 |
$this->_filename = $filename;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
// Check directory permissions
|
|
|
184 |
if ( ! is_dir($this->_path) OR ! is_really_writable($this->_path))
|
|
|
185 |
{
|
|
|
186 |
return FALSE;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
// Build the file path.
|
|
|
190 |
$filepath = $this->_path.$this->_filename.'.cache';
|
|
|
191 |
// Check if the cache exists, if not return FALSE
|
|
|
192 |
if ( ! @file_exists($filepath))
|
|
|
193 |
{
|
|
|
194 |
|
|
|
195 |
return FALSE;
|
|
|
196 |
}
|
|
|
197 |
// Check if the cache can be opened, if not return FALSE
|
|
|
198 |
if ( ! $fp = @fopen($filepath, FOPEN_READ))
|
|
|
199 |
{
|
|
|
200 |
return FALSE;
|
|
|
201 |
}
|
|
|
202 |
// Lock the cache
|
|
|
203 |
flock($fp, LOCK_SH);
|
|
|
204 |
|
|
|
205 |
// If the file contains data return it, otherwise return NULL
|
|
|
206 |
if (filesize($filepath) > 0)
|
|
|
207 |
{
|
|
|
208 |
//echo 'file size'.filesize($filepath);
|
|
|
209 |
$this->_contents = unserialize(fread($fp, filesize($filepath)));
|
|
|
210 |
//print_r($this->_contents);
|
|
|
211 |
}
|
|
|
212 |
else
|
|
|
213 |
{
|
|
|
214 |
//echo 'inside else';
|
|
|
215 |
$this->_contents = NULL;
|
|
|
216 |
}
|
|
|
217 |
// Unlock the cache and close the file
|
|
|
218 |
flock($fp, LOCK_UN);
|
|
|
219 |
fclose($fp);
|
|
|
220 |
|
|
|
221 |
// Check cache expiration, delete and return FALSE when expired
|
|
|
222 |
if ($use_expires && ! empty($this->_contents['__cache_expires']) && $this->_contents['__cache_expires'] < time())
|
|
|
223 |
{
|
|
|
224 |
//echo 'inside if';
|
|
|
225 |
//$this->delete($filename);
|
|
|
226 |
return FALSE;
|
|
|
227 |
}
|
|
|
228 |
// Check Cache dependencies
|
|
|
229 |
if(isset($this->_contents['__cache_dependencies']))
|
|
|
230 |
{
|
|
|
231 |
//echo 'inside if';
|
|
|
232 |
foreach ($this->_contents['__cache_dependencies'] as $dep)
|
|
|
233 |
{
|
|
|
234 |
$cache_created = filemtime($this->_path.$this->_filename.'.cache');
|
|
|
235 |
|
|
|
236 |
// If dependency doesn't exist or is newer than this cache, delete and return FALSE
|
|
|
237 |
if (! file_exists($this->_path.$dep.'.cache') or filemtime($this->_path.$dep.'.cache') > $cache_created)
|
|
|
238 |
{
|
|
|
239 |
//echo 'file deleted';
|
|
|
240 |
$this->delete($filename);
|
|
|
241 |
return FALSE;
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
// Instantiate the object variables
|
|
|
246 |
$this->_expires = isset($this->_contents['__cache_expires']) ? $this->_contents['__cache_expires'] : NULL;
|
|
|
247 |
$this->_dependencies = isset($this->_contents['__cache_dependencies']) ? $this->_contents['__cache_dependencies'] : NULL;
|
|
|
248 |
$this->_created = isset($this->_contents['__cache_created']) ? $this->_contents['__cache_created'] : NULL;
|
|
|
249 |
|
|
|
250 |
// Cleanup the meta variables from the contents
|
|
|
251 |
$this->_contents = @$this->_contents['__cache_contents'];
|
|
|
252 |
log_message('debug', "Cache retrieved: ".$filename);
|
|
|
253 |
//print_r($this->_contents);
|
|
|
254 |
//echo "<br/>----------------------------------------cache file ends--------------------------------------<br/>";
|
|
|
255 |
// Return the cache
|
|
|
256 |
log_message('debug', "Cache retrieved: ".$filename);
|
|
|
257 |
return $this->_contents;
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Write Cache File
|
|
|
262 |
*
|
|
|
263 |
* @access public
|
|
|
264 |
* @param mixed
|
|
|
265 |
* @param string
|
|
|
266 |
* @param int
|
|
|
267 |
* @param array
|
|
|
268 |
* @return void
|
|
|
269 |
*/
|
|
|
270 |
function write($contents = NULL, $filename = NULL, $expires = NULL, $dependencies = array())
|
|
|
271 |
{
|
|
|
272 |
// Check if cache was passed with the function or uses this object
|
|
|
273 |
if ($contents !== NULL)
|
|
|
274 |
{
|
|
|
275 |
$this->_reset();
|
|
|
276 |
$this->_contents = $contents;
|
|
|
277 |
$this->_filename = $filename;
|
|
|
278 |
$this->_expires = $expires;
|
|
|
279 |
$this->_dependencies = $dependencies;
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
// Put the contents in an array so additional meta variables
|
|
|
283 |
// can be easily removed from the output
|
|
|
284 |
$this->_contents = array('__cache_contents' => $this->_contents);
|
|
|
285 |
|
|
|
286 |
// Check directory permissions
|
|
|
287 |
if ( ! is_dir($this->_path) OR ! is_really_writable($this->_path))
|
|
|
288 |
{
|
|
|
289 |
return;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
// check if filename contains dirs
|
|
|
293 |
$subdirs = explode(DIRECTORY_SEPARATOR, $this->_filename);
|
|
|
294 |
if (count($subdirs) > 1)
|
|
|
295 |
{
|
|
|
296 |
array_pop($subdirs);
|
|
|
297 |
$test_path = $this->_path.implode(DIRECTORY_SEPARATOR, $subdirs);
|
|
|
298 |
|
|
|
299 |
// check if specified subdir exists
|
|
|
300 |
if ( ! @file_exists($test_path))
|
|
|
301 |
{
|
|
|
302 |
// create non existing dirs, asumes PHP5
|
|
|
303 |
if ( ! @mkdir($test_path, DIR_WRITE_MODE, TRUE)) return FALSE;
|
|
|
304 |
}
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
// Set the path to the cachefile which is to be created
|
|
|
308 |
$cache_path = $this->_path.$this->_filename.'.cache';
|
|
|
309 |
|
|
|
310 |
// Open the file and log if an error occures
|
|
|
311 |
if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))
|
|
|
312 |
{
|
|
|
313 |
log_message('error', "Unable to write Cache file: ".$cache_path);
|
|
|
314 |
return;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
// Meta variables
|
|
|
318 |
$this->_contents['__cache_created'] = time();
|
|
|
319 |
$this->_contents['__cache_dependencies'] = $this->_dependencies;
|
|
|
320 |
|
|
|
321 |
// Add expires variable if its set...
|
|
|
322 |
if (! empty($this->_expires))
|
|
|
323 |
{
|
|
|
324 |
$this->_contents['__cache_expires'] = $this->_expires + time();
|
|
|
325 |
}
|
|
|
326 |
// ...or add default expiration if its set
|
|
|
327 |
elseif (! empty($this->_default_expires) )
|
|
|
328 |
{
|
|
|
329 |
$this->_contents['__cache_expires'] = $this->_default_expires + time();
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
// Lock the file before writing or log an error if it failes
|
|
|
333 |
if (flock($fp, LOCK_EX))
|
|
|
334 |
{
|
|
|
335 |
fwrite($fp, serialize($this->_contents));
|
|
|
336 |
flock($fp, LOCK_UN);
|
|
|
337 |
}
|
|
|
338 |
else
|
|
|
339 |
{
|
|
|
340 |
log_message('error', "Cache was unable to secure a file lock for file at: ".$cache_path);
|
|
|
341 |
return;
|
|
|
342 |
}
|
|
|
343 |
fclose($fp);
|
|
|
344 |
@chmod($cache_path, DIR_WRITE_MODE);
|
|
|
345 |
|
|
|
346 |
// Log success
|
|
|
347 |
log_message('debug', "Cache file written: ".$cache_path);
|
|
|
348 |
|
|
|
349 |
// Reset values
|
|
|
350 |
$this->_reset();
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* Delete Cache File
|
|
|
355 |
*
|
|
|
356 |
* @access public
|
|
|
357 |
* @param string
|
|
|
358 |
* @return void
|
|
|
359 |
*/
|
|
|
360 |
function delete($filename = NULL)
|
|
|
361 |
{
|
|
|
362 |
if ($filename !== NULL) $this->_filename = $filename;
|
|
|
363 |
|
|
|
364 |
$file_path = $this->_path.$this->_filename.'.cache';
|
|
|
365 |
|
|
|
366 |
if (file_exists($file_path)) unlink($file_path);
|
|
|
367 |
|
|
|
368 |
// Reset values
|
|
|
369 |
$this->_reset();
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
/**
|
|
|
373 |
* Delete a group of cached files
|
|
|
374 |
*
|
|
|
375 |
* Allows you to pass a group to delete cache. Example:
|
|
|
376 |
*
|
|
|
377 |
* <code>
|
|
|
378 |
* $this->cache->write($data, 'nav_title');
|
|
|
379 |
* $this->cache->write($links, 'nav_links');
|
|
|
380 |
* $this->cache->delete_group('nav_');
|
|
|
381 |
* </code>
|
|
|
382 |
*
|
|
|
383 |
* @param string $group
|
|
|
384 |
* @return void
|
|
|
385 |
*/
|
|
|
386 |
public function delete_group($group = null)
|
|
|
387 |
{
|
|
|
388 |
if ($group === null)
|
|
|
389 |
{
|
|
|
390 |
return FALSE;
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
$this->_ci->load->helper('directory');
|
|
|
394 |
$map = directory_map($this->_path, TRUE);
|
|
|
395 |
|
|
|
396 |
foreach ($map AS $file)
|
|
|
397 |
{
|
|
|
398 |
if (strpos($file, $group) !== FALSE)
|
|
|
399 |
{
|
|
|
400 |
unlink($this->_path.$file);
|
|
|
401 |
}
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
// Reset values
|
|
|
405 |
$this->_reset();
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
/**
|
|
|
409 |
* Delete Full Cache or Cache subdir
|
|
|
410 |
*
|
|
|
411 |
* @access public
|
|
|
412 |
* @param string
|
|
|
413 |
* @return void
|
|
|
414 |
*/
|
|
|
415 |
function delete_all($dirname = '')
|
|
|
416 |
{
|
|
|
417 |
if (empty($this->_path))
|
|
|
418 |
{
|
|
|
419 |
return FALSE;
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
$this->_ci->load->helper('file');
|
|
|
423 |
if (file_exists($this->_path.$dirname)) delete_files($this->_path.$dirname, TRUE);
|
|
|
424 |
|
|
|
425 |
// Reset values
|
|
|
426 |
$this->_reset();
|
|
|
427 |
}
|
|
|
428 |
}
|
|
|
429 |
|
|
|
430 |
/* End of file Cache.php */
|
|
|
431 |
/* Location: ./application/libraries/Cache.php */
|