六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 818|回复: 0

PHP文件缓存类

[复制链接]
 楼主| 发表于 2015-12-28 17:52:50 | 显示全部楼层 |阅读模式
PHP文件缓存类
cache.inc.php类
  1. <?php

  2. class Cache {
  3.    /**
  4.     * $dir : 缓存文件存放目录
  5.     * $lifetime : 缓存文件有效期,单位为秒
  6.     * $cacheid : 缓存文件路径,包含文件名
  7.     * $ext : 缓存文件扩展名(可以不用),这里使用是为了查看文件方便
  8.    */
  9.    private $dir;
  10.    private $lifetime;
  11.    private $cacheid;
  12.    private $ext;
  13.    /**
  14.     * 析构函数,检查缓存目录是否有效,默认赋值
  15.    */
  16.    function __construct($dir='',$lifetime=1800) {
  17.        if ($this--->dir_isvalid($dir)) {
  18.            $this->dir = $dir;
  19.            $this->lifetime = $lifetime;
  20.            $this->ext = '.Php';
  21.            $this->cacheid = $this->getcacheid();
  22.        }
  23.    }
  24.    /**
  25.     * 检查缓存是否有效
  26.    */
  27.    private function isvalid() {
  28.        if (!file_exists($this->cacheid)) return false;
  29.        if (!(@$mtime = filemtime($this->cacheid))) return false;
  30.        if (mktime() - $mtime > $this->lifetime) return false;
  31.        return true;
  32.    }
  33.    /**
  34.     * 写入缓存
  35.     * $mode == 0 , 以浏览器缓存的方式取得页面内容
  36.     * $mode == 1 , 以直接赋值(通过$content参数接收)的方式取得页面内容
  37.     * $mode == 2 , 以本地读取(fopen ile_get_contents)的方式取得页面内容(似乎这种方式没什么必要)
  38.    */
  39.    public function write($mode=0,$content='') {
  40.        switch ($mode) {
  41.            case 0:
  42.                $content = ob_get_contents();
  43.                break;
  44.            default:
  45.                break;
  46.        }
  47.        ob_end_flush();
  48.        try {
  49.            file_put_contents($this->cacheid,$content);
  50.        }
  51.        catch (Exception $e) {
  52.            $this->error('写入缓存失败!请检查目录权限!');
  53.        }
  54.    }
  55.    /**
  56.     * 加载缓存
  57.     * exit() 载入缓存后终止原页面程序的执行,缓存无效则运行原页面程序生成缓存
  58.     * ob_start() 开启浏览器缓存用于在页面结尾处取得页面内容
  59.    */
  60.    public function load() {
  61.        if ($this->isvalid()) {
  62.            echo " ";
  63.            //以下两种方式,哪种方式好?????
  64.            require_once($this->cacheid);
  65.            //echo file_get_contents($this->cacheid);
  66.            exit();
  67.        }
  68.        else {
  69.            ob_start();
  70.        }
  71.    }
  72.    /**
  73.     * 清除缓存
  74.    */
  75.    public function clean() {
  76.        try {
  77.            unlink($this->cacheid);
  78.        }
  79.        catch (Exception $e) {
  80.            $this->error('清除缓存文件失败!请检查目录权限!');
  81.        }
  82.    }
  83.    /**
  84.     * 取得缓存文件路径
  85.    */
  86.    private function getcacheid() {
  87.        return $this->dir.md5($this->geturl()).$this->ext;
  88.    }
  89.    /**
  90.     * 检查目录是否存在或是否可创建
  91.     */
  92.    private function dir_isvalid($dir) {
  93.        if (is_dir($dir)) return true;
  94.        try {
  95.            mkdir($dir,0777);
  96.        }
  97.        catch (Exception $e) {
  98.              $this->error('所设定缓存目录不存在并且创建失败!请检查目录权限!');
  99.              return false;            
  100.        }
  101.        return true;
  102.    }
  103.    /**
  104.     * 取得当前页面完整url
  105.    */
  106.    private function geturl() {
  107.        $url = '';
  108.        if (isset($_SERVER['REQUEST_URI'])) {
  109.            $url = $_SERVER['REQUEST_URI'];
  110.        }
  111.        else {
  112.            $url = $_SERVER['Php_SELF'];
  113.            $url .= empty($_SERVER['QUERY_STRING'])?'':'?'.$_SERVER['QUERY_STRING'];
  114.        }
  115.        return $url;
  116.    }
  117.    /**
  118.     * 输出错误信息
  119.    */
  120.    private function error($str) {
  121.        echo $str;
  122.    }
  123. }
  124. ?>
复制代码
demo.php

  1. <php
  2. /*
  3. * 使用方法举例
  4. ------------------------------------Demo-------------------------------------------

  5.    require_once('cache.inc.php');
  6.    $cachedir = './Cache/'; //设定缓存目录
  7.    $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
  8.    if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
  9.        $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
  10.    //页面代码开始
  11.    echo date('H:i:s jS F');
  12.    //页面代码结束
  13.    $cache->write(); //首次运行或缓存过期,生成缓存

  14. ------------------------------------Demo2-------------------------------------------

  15.    require_once('cache.inc.php');
  16.    $cachedir = './Cache/'; //设定缓存目录
  17.    $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
  18.    if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
  19.        $cache->load(); //装载缓存,缓存有效则不执行以下页面代码
  20.    //页面代码开始
  21.    $content = date('H:i:s jS F');
  22.    echo $content;
  23.    //页面代码结束
  24.    $cache->write(1,$content); //首次运行或缓存过期,生成缓存

  25. ------------------------------------Demo3-------------------------------------------

  26.    require_once('cache.inc.php');
  27.    define('CACHEENABLE',true);
  28.    
  29.    if (CACHEENABLE) {
  30.        $cachedir = './Cache/'; //设定缓存目录
  31.        $cache = new Cache($cachedir,10); //省略参数即采用缺省设置, $cache = new Cache($cachedir);
  32.        if ($_GET['cacheact'] != 'rewrite') //此处为一技巧,通过xx.Php?cacheact=rewrite更新缓存,以此类推,还可以设定一些其它操作
  33.            $cache->load(); //装载缓存,缓存有效则不执行以下页面代码   
  34.    }
  35.    //页面代码开始
  36.    $content = date('H:i:s jS F');
  37.    echo $content;
  38.    //页面代码结束
  39.    if (CACHEENABLE)
  40.        $cache->write(1,$content); //首次运行或缓存过期,生成缓存
  41. */
  42. ?>
复制代码
http://www.phpxs.com/post/2927
PHP文件缓存类

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表