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();

}

}

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值