原生PHP的缓存类,比较简单,经测试可以正常运行,收藏好了!
class Cache
{
static function S($name,$data='',$time=100,$path='cache/')
{
$split = '|--|--|';
$prefix = '.cache.php';
$file = $path.md5(strtolower($name)).$prefix;
//如果$data为空,刚为读取缓存
if(empty($data))
{
if(is_file($file))
{
$cache = file_get_contents($file);
preg_match('//is',$cache,$rs);
$cache = $rs[1];
$data = explode($split,$cache);
$time = intval($data[0]);
$cache = $data[1];
if($time
else return unserialize($cache);
}
else return null;
}
//否则为写缓存
else
{
$time = time() + $time;
$data = serialize($data);
$data = '<?php ///'.$time.$split.$data.' ?>';
$folder = dirname($file);
mk_dir($folder);
$fp = @fopen($file,'w+');
@fwrite($fp,$data);
@fclose($fp);
}
}
}
if(!function_exists('mk_dir'))
{
// 循环创建目录
function mk_dir($dir, $mode = 0777)
{
if (is_dir($dir) || @mkdir($dir,$mode)) return true;
if (!mk_dir(dirname($dir),$mode)) return false;
return @mkdir($dir,$mode);
}
}
?>