Home php教程 PHP源码 PHP通用缓存方法

PHP通用缓存方法

May 26, 2016 am 08:18 AM

/**
  通用缓存
  说明:
    根据传递参数的个数用来确认需要进行的操作
    若传递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 &#39;<pre class="brush:php;toolbar:false">&#39;,var_dump(&#39;读&#39;),&#39;
'; echo $che[2]; unset($che); }else{ //已超出缓存期 echo '
&#39;,var_dump(&#39;写&#39;),&#39;
'; $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(); } }
Copy after login

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)