网页资讯贴吧知道视频音乐图片地图文库
进入贴吧全吧搜索吧内搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
04月13日漏签0天
php吧 关注:185,547贴子:1,133,176
  • 看贴

  • 图片

  • 精品

  • 视频

  • 游戏

  • 4回复贴,共1页
<<返回php吧
>0< 加载中...

我也发个php缓存类

  • 只看楼主
  • 收藏

  • 回复
  • huzp123
  • 路过酱油
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
<?php
/*
* ClassName   : Cache
* Version     : 1.0
* Author      : 翔天
* Description : 文件缓存类,支持数组缓存
* Link        : http://www.yaayee.net/
* E-mail      : 6770386@qq.com
* License     : 本程序是开源的,你可以运用于各种开发。你可以根据需要修改程序主体,但请保留以上版本信息,谢谢!
*/
/*
功能:create()为创建缓存;remove()删除缓存;getcreatetime()获取缓存创建时间;getcache()获取缓存内容
实例:
$cache=new Cache('/cache_date/','inc'); //初始化,配置缓存目录,缓存文件扩展名,可以为任意可读取的文件类型
$cname='test';
$ccontent='testcontent';
$cache->create($cname,$ccontent); //保存单个缓存

$cname_arr=array('test1','test2','test3');
$ccontent_arr=array('testcontent1','testcontent2','testcontent3');
$cache->create($cname_arr,$ccontent_arr); //保存多个缓存

$cname_arr=array('test_arr1','test_arr2');
$ccontent_arr=array(array('content_arr_1_1','content_arr_1_2'),array('content_arr_2_1','content_arr_2_1'));
$cache->create($cname_arr,$ccontent_arr); //保存多个数组缓存

$cname=$cache->getcache('test'); //获取单个缓存
$ccontent_arr=$cache->getcache($cname_arr); //获取多个缓存
*/
class Cache{
    var $cachepatch;
    var $ftype;
    function Cache($cachepatch,$ftype){
        $cp=$_SERVER['DOCUMENT_ROOT'].$cachepatch;
        $this->ftype=$ftype;
        if (!is_dir($cp)){
            mkdir($cp,0777);
        }
        $this->cachepatch=$cp;
    }
    //创建缓存:$cachename为缓存名称,$content为缓存内容,可以为array类型
    function create($cachename,$content){
        if (is_array($cachename)){
            foreach ($cachename as $k=>$v){
                if (file_exists($cachefile)){
                    @unlink($cachefile);
                }
                if ($this->ftype=='php' or $this->ftype=='inc'){
                    if (is_array($content[$k])){
                        $content[$k]="<?php\n$".$v."=".var_export($content[$k],true).";\n?>";


  • huzp123
  • 路过酱油
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
                    }else {
                        $content[$k]="<?php\n$".$v."=\"".$content[$k]."\";\n?>";
                    }
                }
                $cachefile[$k]=$this->cachepatch.$v.".".$this->ftype;
                $handle=fopen($cachefile[$k],'w');
                $flag=fwrite($handle,$content[$k]);
                fclose($handle);
            }
        }else {
            if ($this->ftype=='php' or $this->ftype=='inc'){
                if (is_array($content)){
                    $content="<?php\n$".$cachename."=".var_export($content,true).";\n?>";
                }else {
                    $content="<?php\n$".$cachename."=\"".$content."\";\n?>";
                }
            }
            $cachefile=$this->cachepatch.$cachename.".".$this->ftype;
            if (file_exists($cachefile)){
                @unlink($cachefile);
            }
            $handle=fopen($cachefile,'w');
            $flag=fwrite($handle,$content);
            fclose($handle);
        }
        clearstatcache();
    }
    //删除缓存
    function remove($cachename){
        if (is_array($cachename)){
            foreach ($cachename as $k=>$v){
                if ($v!=''){
                    @unlink($this->cachepatch.$v.".".$this->ftype);


  • huzp123
  • 路过酱油
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
                }
            }
        }else {
            if ($cachename!=''){
                @unlink($this->cachepatch.$cachename.".".$this->ftype);
            }
        }
    }
    //获取缓存创建时间:$cachename为缓存名称,$timeformat为返回时间的格式,参照date()函数,可以为array类型
    function getcreatetime($cachename,$timeformat=''){
        $arr=array();
        if (is_array($cachename)){
            foreach ($cachename as $k=>$v){
                $time=filemtime($this->cachepatch.$v.".".$this->ftype);
                if ($timeformat!=''){
                    if (is_array($timeformat)){
                        $time=date($timeformat[$k],$time);
                    }else {
                        $time=date($timeformat,$time);
                    }
                }
                array_push($arr,$time);
            }
            return $arr;
        }else {
            if (file_exists($this->cachepatch.$cachename.".".$this->ftype)){
                $time=filemtime($this->cachepatch.$cachename.".".$this->ftype);
                if ($timeformat!=''){
                    $time=date($timeformat,$time);
                }
                return $time;
            }else {
                return 0;


  • huzp123
  • 路过酱油
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
            }
        }
    }
    //获取缓存:可以为array类型
    function getcache($cachename){
        if (is_array($cachename)){
            $arr=array();
            if ($this->ftype=='php' or $this->ftype=='inc'){
                foreach ($cachename as $k=>$v){
                    require($this->cachepatch.$v.".".$this->ftype);
                    array_push($arr,$$v);
                }
            }else {
                foreach ($cachename as $k=>$v){
                    $v=file_get_contents($this->cachepatch.$v.".".$this->ftype);
                    array_push($arr,$v);
                }
            }
            return $arr;
        }else {
            if ($this->ftype=='php' or $this->ftype=='inc'){
                if (file_exists($this->cachepatch.$cachename.".".$this->ftype)){
                    require($this->cachepatch.$cachename.".".$this->ftype);
                    return $$cachename;
                }else {
                    return '';
                }
            }else {
                $cachename=file_get_contents($this->cachepatch.$cachename.".".$this->ftype);
                return $cachename;
            }
        }
    }
}
?>


  • 123.118.250.*
快试试吧,
可以对自己使用挽尊卡咯~
◆
◆
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
学习了


登录百度帐号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 4回复贴,共1页
<<返回php吧
分享到:
©2020 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示