首页 > php开发 > php 缓存技术实例

php 缓存技术实例

本文章要讲的php 缓存技术是讲把数据生成一个临时缓存文件保存到硬盘,然后根据缓存文件设定的时间删除缓存文件再次生成新的缓存文件哦。
 代码如下 复制代码

$filename = 'cachefile.php';
$str ='echo "bb";';
if( is_file( $filename ) )
{
 $tmp = readcache( $filename ) ;
}
else
{
 createcache( $filename,$str );
}

//写缓存文件

function createcache($filename,$str)
{
 if( $str =='' ){ return false;}
 $fp = fopen($filename,"w+") or die('缓存目录不可能,请设置/www.phprm.com/cache为可写权限!');
 if( ! fwrite($fp,$str) )
 {
  echo '不能创建缓存文件!';
  exit;
 }
 fclose($fp);  
}

//读取缓存文件

function readcache($filename)
{
 $str = file_get_contents($filename);
 if( $str == "" )
 {
  echo "缓存文件读取失败!";
  exit;
 }
 return $str;
}

/*
本站原创文章,转载注明来源http://www.phprm.com
*/



教程地址:http://www.phprm.com/develop/34586.html

欢迎转载!但请带上文章地址^^

标签:fopen

相关文章

发表留言