Real_Madrid 2016-11-30 17:27:55 4471次浏览 1条评论 3 2 0
<?php
class FCache{

    protected $endTime=360;//缓存保存时间(秒)
    protected $fileTime=360;//缓存保存时间(秒)
    public  $rootDir='../filecache/wap';//主目录地址

    public function __construct() {
        ob_start();
    }
    /**
    * set設置一致key  修改键名的值
    * @param string $key 键名
    * @param string $value 键值
    * @param int $timeLife 生命周期
    * @param int $target 是否为pc目录 true pc端 false wap端
    */
    public function set($key, $value,$target=null) {
    ob_start();
    if($target){
        $this->rootDir='./filecache/pc';
    }
    $retMes=$this->saveDir($value,$key);
    return $retMes;
    }
    /**
     * 保存文件
     */
    public function saveDir($data,$dirName){
        if($data!==null && $dirName!==null){
            $fileDir=$this->rootDir.'/'.Yii::app()->controller->id.'/';
            if(!is_dir($fileDir)){
                mkdir($fileDir,0777,true);
            }
            $dir=$fileDir.$dirName.'.html';//静态 URL 文件名
            if(file_exists($dir) && filemtime($dir)>time()){
                file_get_contents($dir);
            }else{
                file_put_contents($dir,$data);
            }
            return $fileDir;
        }
    }

    /**
     * 清空所有缓存文件
     * @param $path 主目录
     * @param $target 目录主文件夹
     * @return bool
     */
    public function emptyCache($path=null){
        $target=Yii::app()->params['FCachePath'];
        if(!file_exists($target)) return false;
        if($path) $target=$path;
        $handle = opendir($target);
        $op = dir($target);
        while(false != ($item = $op->read())) {
            if($item == '.' || $item == '..') {
                continue;
            }elseif(is_dir($op->path.'/'.$item)) {
                $this->emptyCache($op->path.'/'.$item);
            } else {
                unlink($op->path.'/'.$item);
            }
            $op = dir($target);
        }
        closedir($handle);
        return rmdir($target) ;
    }

    /**
     * 读取文件
     */

    public function readDir($dirName,$target=null){
        if($target){
            $this->rootDir='./filecache/pc';
        }
        if(Yii::app()->controller->id=='project'){
            $this->ChangePStatus($this->rootDir);
        }
        if($dirName!==null){
            $fileDir=$this->rootDir.'/'.Yii::app()->controller->id.'/'.$dirName.'.html';
            if(file_exists($fileDir)){
                if((Yii::app()->controller->id=='project' && filemtime($fileDir)+$this->endTime*30<time())|| filemtime($fileDir)+$this->fileTime*360<time()){
                    unlink($fileDir);
                    $data=false;
                }else{
                    $data=true;
                }
            }else{
                $data=false;
            }
        }else{
            $data=false;
        }
        return $data;
    }

    /**
     * 读取文件时的判断
     */
    protected function ChangePStatus($path){
        $p_id=isset($_GET['id'])?$_GET['id']:'';
        $model=ProjectModel::model()->findByPk($p_id);
        $file_url=$path.'/'.Yii::app()->controller->id.'/'.$this->md5($p_id).'.html';
        if($model->attributes['status']==ProjectModel::STATUS_SELL && file_exists($file_url)){
             unlink($file_url);
        }
    }

    /**
     * 跳转地址
     */
    public function readUrl($dirName){
        if($dirName!==null){
            $fileDir=$this->rootDir.'/'.Yii::app()->controller->id.'/'.$dirName.'.html';
            if(file_exists($fileDir)){
               return file_get_contents($fileDir);
            }
        }

    }


    /**
     * 当前文件名
     */
    public function filename(){
        $url = $_SERVER['REQUEST_URI'];
        $filename=explode('/',$url);
        $filename=end($filename);
        $filename=$filename?explode($filename,'.'):'index';
        $filename=$this->md5($filename[0]);
        if(isset($_GET['id'])){
            $p_id=$_GET['id'];
            $model=ProjectModel::model()->findByPk($p_id);
            if($model->attributes['status']==ProjectModel::STATUS_SELL){
                $filename=$this->md5($p_id.ProjectModel::STATUS_SELL);
            }else{
                $filename=$this->md5($p_id);
            }
        }
        return $filename;
    }

    /**
     * 文件名加密
     */
    public static function md5($name){
        $string='fenjinshe';
        if($name){
            return md5(md5($name).$string);
        }
    }
    /**
     * 时间间隔删除缓存
     */
//    public function TimeClean(){
//        ignore_user_abort(true);
//        @set_time_limit(0);
//        session_write_close();
//        while(true){
//            $this->emptyCache();
//            sleep(2);
//        }
//
//    }
}
觉得很赞
  • 评论于 2016-12-01 09:22 举报

    public function emptyCache($path=null){

        $target=Yii::app()->params['FCachePath'];
    

    }
    FCachePath 是什么

    1 条回复
    评论于 2016-12-05 08:34 回复

    估计是在配置文件中配置的地址参数,楼主没有说明。另外楼主将一些变量写死在了代码里觉得这样会给后期维护带来很大的困难。

您需要登录后才可以评论。登录 | 立即注册