PHP头条
热点:

PHP通用缓存方法-PHP源码


/**
  通用缓存
  说明:
    根据传递参数的个数用来确认需要进行的操作
    若传递2个参数,作用是判断缓存档是否已经过期
    若传递3个参数,作用是对内容进行缓存
  返回:
    若传递2个参数:
      若未超出缓存期,则返回缓存的内容(该内容已解密)
      若超出了缓存期,则返回bool值的FALSE
    若传递3个参数:
      对内容进行缓存(传递进来的内容未被加密,存储前要先加密),无返回值
  参数:
    第1个参数:string型,缓存档
    第2个参数:int型,缓存多少秒
    第3个参数:string型,缓存什么[可选]
  用法:
    $che[0]=Run.'_cache/_qian_main_0.che';  //缓存档
    $che[1]=600;              //缓存多少秒
    $che[2]=Fun::Cache($che[0],$che[1]);
    if(!is_bool($che[2])){        //未超出缓存期
      echo '
',var_dump('读'),'
'; echo $che[2]; unset($che); }else{ //已超出缓存期 echo '
',var_dump('写'),'
'; $che[2]='???'; Fun::Cache($che[0],$che[1],$che[2]); echo $che[2]; unset($che); } //删除缓存档 Fun::Cache(Run.'_super_main_0.che',0,''); /**/ public static function Cache(){ $o=func_get_args(); if(!is_array($o)){ throw new exception('Error:'.__LINE__.',必须传递参数!');die(); } $count=count($o); switch($count){ case 2://判断缓存是否已经过期(未过期则返回缓存的内容,已过期则返回FALSE) //缓存档是否存在 if(!file_exists($o[0])){unset($o,$count);return FALSE;} //取得上次的修改时间 $o[3]=filemtime($o[0]); $o[3]=(!is_numeric($o[3]) or $o[3]=$o[1]){unset($o,$count);return FALSE;} //返回解密之后的内容 unset($count); return base64_decode(file_get_contents($o[0])); case 3://对内容进行缓存 //如果缓存时间小于1,则删除缓存档 if($o[1]<1){ if(file_exists($o[0])){@unlink($o[0]);} unset($o,$count); return ; } //更新缓存 file_put_contents($o[0],base64_encode(''.$o[2].''),LOCK_EX); if(file_exists($o[0])){ chmod($o[0],0777); } unset($o,$count); return ; default: unset($o,$count); throw new exception('Error:'.__LINE__.',参数个数不对!');die(); } }

www.phpzy.comtrue/php/33077.htmlTechArticlePHP通用缓存方法-PHP源码 /** 通用缓存 说明: 根据传递参数的个数用来确认需要进行的操作 若传递2个参数,作用是判断缓存档是否已经过期 若传递3个参数,作用是对内容进行缓存 返回: 若...

相关文章

PHP之友评论

今天推荐