PHP + redis缓存类

  1. <?php  
  2.     
  3. class redisCache {  
  4.    /** 
  5.     * $host : redis服务器ip 
  6.     * $port : redis服务器端口 
  7.     * $lifetime : 缓存文件有效期,单位为秒 
  8.     * $cacheid : 缓存文件路径,包含文件名 
  9.    */  
  10.    private $host;  
  11.    private $port;  
  12.    private $lifetime;  
  13.    private $cacheid;  
  14.    private $data;  
  15.    public $redis;  
  16.    /** 
  17.     * 析构函数,检查缓存目录是否有效,默认赋值 
  18.    */  
  19.    function __construct($lifetime=1800) {  
  20.         //配置  
  21.         $this->host = "127.0.0.1";  
  22.         $this->port = "6379";  
  23.         $redis = new Redis();   
  24.         $redis->pconnect($this->host,$this->port);   
  25.         $this->redis=$redis;  
  26.         $this->cacheid = $this->getcacheid();  
  27.         $this->lifetime = $lifetime;  
  28.         $this->data=$redis->hMGet($this->cacheid, array('content','creattime'));  
  29.         //print_r($this->redis);  
  30.         //print_r($this->data);  
  31.    }  
  32.    
  33.    
  34.    /** 
  35.     * 检查缓存是否有效 
  36.    */  
  37.    private function isvalid(){  
  38.        $data=$this->data;  
  39.        if (!$data['content']) return false;  
  40.        if (time() - $data['creattime'] > $this->lifetime) return false;  
  41.        return true;  
  42.    }  
  43.    /** 
  44.     * 写入缓存 
  45.     * $mode == 0 , 以浏览器缓存的方式取得页面内容 
  46.    */  
  47.    public function write($mode=0,$content='') {  
  48.        switch ($mode) {  
  49.            case 0:  
  50.                $content = ob_get_contents();  
  51.                break;  
  52.            default:  
  53.                break;  
  54.        }  
  55.        ob_end_flush();  
  56.        try {  
  57.             $this->redis->hMset($this->cacheid, array('content'=>$content,'creattime'=>time()));  
  58.             $this->redis->expireAt($this->cacheid, time() + $this->lifetime);  
  59.        }  
  60.        catch (Exception $e) {  
  61.            $this->error('写入缓存失败!');  
  62.        }  
  63.    }  
  64.    /** 
  65.     * 加载缓存 
  66.     * exit() 载入缓存后终止原页面程序的执行,缓存无效则运行原页面程序生成缓存 
  67.     * ob_start() 开启浏览器缓存用于在页面结尾处取得页面内容 
  68.    */  
  69.    public function load() {  
  70.        if ($this->isvalid()) {  
  71.            echo $this->data['content'];  
  72.            exit();  
  73.        }  
  74.        else {  
  75.            ob_start();  
  76.        }  
  77.    }  
  78.    /** 
  79.     * 清除缓存 
  80.    */  
  81.    public function clean() {  
  82.        try {  
  83.            $this->redis->hDel($this->cacheid, array('content','creattime'));  
  84.        }  
  85.        catch (Exception $e) {  
  86.            $this->error('清除缓存失败!');  
  87.        }  
  88.    }  
  89.    /** 
  90.     * 取得缓存文件路径 
  91.    */  
  92.    private function getcacheid() {  
  93.        return $this->dir.md5($this->geturl()).$this->ext;  
  94.    }  
  95.    /** 
  96.     * 取得当前页面完整url 
  97.    */  
  98.    private function geturl() {  
  99.        $url = '';  
  100.        if (isset($_SERVER['REQUEST_URI'])) {  
  101.            $url = $_SERVER['REQUEST_URI'];  
  102.        }  
  103.        else {  
  104.            $url = $_SERVER['Php_SELF'];  
  105.            $url .= empty($_SERVER['QUERY_STRING'])?'':'?'.$_SERVER['QUERY_STRING'];  
  106.        }  
  107.        return $url;  
  108.    }  
  109.    /** 
  110.     * 输出错误信息 
  111.    */  
  112.    private function error($str) {  
  113.        echo '<div style="color:red;">'.$str.'</div>';  
  114.    }  
  115. }  
  116.    
  117.    
  118. //用法:  
  119. // require_once('redisCache.php');  
  120. // $cache = new redisCache(10); //设置缓存生存期  
  121. // if ($_GET['clearCache']) $cache->clean();  
  122. // else  $cache->load(); //装载缓存,缓存有效则不执行以下页面代码  
  123. // //页面代码开始  
  124.    
  125. // //页面代码结束  
  126. // $cache->write(); //首次运行或缓存过期,生成缓存  
  127.    
  128.    
  129.    
  130. ?>  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值