Codeigniter 缓存文件的使用 函数封装

Codeigniter 缓存文件的使用 函数封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if(!function_exists('cache_read')){
function cache_read($file, $dir = '', $mode = '') {
$file = _get_cache_file($file, $dir);
if(!is_file($file)) return NULL;
return $mode ? read_file($file) : include $file;
}
}

if(!function_exists('cache_write')){
function cache_write($file, $string, $dir = '') {
if(is_array($string)) {
$string = "<?php return ".var_export($string, true)."; ?>";
$string = str_replace(array(chr(13), chr(10), "\n", "\r", "\t", ' '),array('', '', '', '', '', ''), $string);
}
$file = _get_cache_file($file, $dir);
return write_file($file, $string);
}
}


if(!function_exists('cache_delete')){
function cache_delete($file, $dir = '') {
$file = _get_cache_file($file, $dir);
return unlink($file);
}
}


if(!function_exists('_get_cache_file')){
function _get_cache_file($file, $dir) {
$path = config_item('cache_path') ? config_item('cache_path') : APPPATH . 'cache/';
return ($dir ? $path.$dir.'/'.$file : $path.$file);
}
}