search
Homephp教程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(); } }

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!