Home  >  Article  >  Backend Development  >  php file caching function

php file caching function

高洛峰
高洛峰Original
2016-11-30 13:30:451492browse

The code is as follows:

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn