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 '<pre class="brush:php;toolbar:false">',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(); } }
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
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How to fix KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD
Nordhold: Fusion System, Explained
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Blue Prince: How To Get To The Basement
1 months ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
Java Tutorial
1664
14


CakePHP Tutorial
1423
52


Laravel Tutorial
1318
25


PHP Tutorial
1269
29


C# Tutorial
1248
24

