php 文件缓存函数

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

function createHashDir($sign)
{
$md5 = md5($sign);
if(!is_dir(MB_CACHE)) mkdir(MB_CACHE);
for($i=1;$i<=4;$i++)
{
$dir .= $md5{$i}.'/';
if(!is_dir(MB_CACHE.$dir))
{
mkdir(MB_CACHE.$dir);
}
}
return MB_CACHE.$dir;
}
function setCacheFile($data,$sign = 'a',$type = 'array',$id = '')
{
$cacheDir = $this -> createHashDir($sign);
if(!empty($data))
{
$id = $id ? $id : $sign;
$cacheFile = $cacheDir.$id.'.php';
$content = $type == 'array' ? var_export($data,true) : $data;
file_put_contents($cacheFile,'<?php $'.$sign.' = '.$content.'; ?>');
}
}
function getCacheFile($sign = 'a',$id = '')
{
$cacheDir = $this -> createHashDir($sign);
$id = $id ? $id : $sign;
$cacheFile = $cacheDir.$id.'.php';
if(is_file($cacheFile) && include_once($cacheFile))
{
return $$sign;
}
}
function getCacheFilePath($sign = 'a',$id = '')
{
$cacheDir = $this -> createHashDir($sign);
$id = $id ? $id : $sign;
return $cacheDir.$id.'.php';
}
function delCacheFile($sign = 'a')
{
$cacheDir = $this -> createHashDir($sign);
$id = $id ? $id : $sign;
$cacheFile = $cacheDir.$id.'.php';
$this -> del_file($cacheFile);
}

相关文章

PHP、Nginx、Apache中禁止网页被iframe引用的方法

可以使用php或nginx等添加X-Frame-Options header来控制frame权限X-Frame-Options有三个可选的值: DENY:浏览器拒绝当前页面加载任何Fra...

php Smarty date_format [格式化时间日期]

Example 5-8. date_format[日期格式] index.php: 复制代码 代码如下: $smarty = new Smarty; $smarty->assign...

php异常处理方法实例汇总

本文实例讲述了php异常处理方法。分享给大家供大家参考。具体如下: <?php $path = "D://in.txt"; try //检测异常 { fil...

php获取当前url地址的方法小结

本文实例讲述了php获取当前url地址的方法。分享给大家供大家参考,具体如下: js 获取: top.location.href //顶级窗口的地址 this.location.h...

PHP中创建空文件的代码[file_put_contents vs touch]

I has passed a small test to check which function is faster to create a new file. file_put_co...