现在的位置: 首页 > 编程语言 > 正文

PHP缓存类的用法有什么

2020年05月29日 编程语言 ⁄ 共 1470字 ⁄ 字号 评论关闭

  PHP功能很简单,就是缓存整个页面,可以设定缓存时间,可以缓存特定的URL,例如:test.php?id=12,当目标文件更新时,如test.php,缓存文件也会更新,即使仍处于缓存期内。下面学步园小编来讲解下PHP缓存类的用法有什么?

  PHP缓存类的用法有什么

  classcache

  {

  var$cache_dir='./cache/';//Thisisthedirectorywherethecachefileswillbestored;

  var$cache_time=120;//Howmuchtimewillkeepthecachefilesinseconds.

  var$caching=false;

  var$file='';

  functioncache()

  {

  //Constructoroftheclass

  $this->file=$this->cache_dir.urlencode($_SERVER['REQUEST_URI']);

  if(file_exists($this->file))$expired=$this->check_expire();

  else$expired=false;

  if(file_exists($this->file)&&(filemtime($this->file)+$this->cache_time)>time()&&!$expired)

  {

  //Grabthecache:

  $handle=fopen($this->file,"r");

  do{

  $data=fread($handle,8192);

  if(strlen($data)==0){

  break;

  }

  PHP缓存类的用法有什么

  echo$data;

  }while(true);

  fclose($handle);

  exit();

  }

  else

  {

  //createcache:

  $this->caching=true;

  ob_start();

  $now=time();

  echo"\n";

  }

  }

  functionclose()

  {

  //Youshouldhavethisattheendofeachpage

  if($this->caching)

  {

  //Youwerecachingthecontentssodisplaythem,andwritethecachefile

  $data=ob_get_clean();

  echo$data;

  $fp=fopen($this->file,'w');

  fwrite($fp,$data);

  fclose($fp);

  }

  }

  functioncheck_expire(){

  $fp=fopen($this->file,"r");

  preg_match("/\:([\d]+)\-/",fread($fp,200),$time);

  $modify_time=$time[1];

  if($modify_time

  returntrue;

  }

  else{

  returnfalse;

  }

  }

  }

  用法:

  //Example:

  $ch=newcache();

  echodate("DMjG:i:sTY");

  $ch->close();

  以上就是关于“PHP缓存类的用法有什么”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!

抱歉!评论已关闭.