-
Notifications
You must be signed in to change notification settings - Fork 10
/
cache.class.php
57 lines (51 loc) · 1.07 KB
/
cache.class.php
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
//dingchengliang simple cache.
//读文件
function cache_read($file)
{
$file = cachefilepath($file,$folder);
//echo $file . '===';
if(file_exists($file))
{ include($file);
return $content;
}
}
//写缓存内容
function cache_write($file,$content)
{
$file = cachefilepath($file,$folder);
if(is_array($content))
{
$content= var_export($content,1);
}
else
{
$content='array()';
}
/*$content = '<? $content = unserialize (\'' . serialize($content) . '\'); ?>'; */
$content = '<? $content = ' . ($content) . ' ; ?>';
if(function_exists('file_put_contents'))
{
file_put_contents($file,$content);
}
else
{
$fp=fopen($file,'w');
flock($fp,2); //加锁
fwrite($fp,$content);
if(flock($fp,3)) //解除锁定
fclose($fp);
}
}
function cachefilepath($file)
{
$folder=$GLOBALS['db_cache_path'];
return $folder.'cache.'.($file) .'.php';
}
function cache_outofdate($file, $long){
$file = cachefilepath($file);
if(!is_file($file)) return true;
$t = filemtime ($file);
if(time()-$t > $long) return true;
else return false;
}