用户
 找回密码
 入住 CI 中国社区
搜索
查看: 7384|回复: 8
收起左侧

[库 Library] 用redis减少数据库频繁更新次数

[复制链接]
发表于 2012-4-13 14:36:46 | 显示全部楼层 |阅读模式
本帖最后由 longniao 于 2012-4-16 15:09 编辑

开发过程中,经常遇到各种统计,如网页查看数,每次打开网页,都需要更新数据库,如在view字段加1

此类数据,不需要实时精确,因此可以积累起来批量处理,如1分钟更新一次,简单写了个基于redis类:

PHP复制代码
<?php
/**
 * RedisBatch        Class
 * @author                Longniao <longniao@gmail.com>
 * @url                      http://www.inhai.com/* @version                1.0
*/

class RedisBatch {
 
        public        $_Server;
        public        $_Host;
        public        $_Port;
 
        public        $_Varvar;                //数组变量
        public        $_Vartime;                //时间变量
        public        $_Interval;                //时间间隔
        public        $_Runflag;                //执行开关
 
        function __construct()
        {
                $this->_Server        = new Redis();
                $this->_Host                = '127.0.0.1';
                $this->_Port                = '6379';
 
                $this->_Varvar                = 'batchvar';
                $this->_Vartime                = 'batchtime';
                $this->_Interval        = 5;
                $this->_Runflag                = false;
 
                $this->_Server->connect($this->_Host, $this->_Port);
 
                if($this->_Server->exists($this->_Vartime)){
 
                        $lastTime = $this->_Server->get($this->_Vartime);
 
                        if(time() - $lastTime > $this->_Interval)
                        {
                                $this->_Runflag = true;
                                $this->refresh();
                        }
                }
                else
                {
                        $this->refresh();
                }
        }
 
        /**
         * 添加
         */

        public function add($id)
        {
                return $this->_Server->lpush($this->_Varvar, $id);
        }
 
        /**
         * 获取
         */

        public function get()
        {
                return $this->_Server->lRange($this->_Varvar, 0, -1);
        }
 
        /**
         * 清空
         */

        public function clear()
        {
                return $this->_Server->delete($this->_Varvar);
        }
 
        /**
         * 刷新
         */

        public function refresh()
        {
                return $this->_Server->set($this->_Vartime, time());
        }
 
}
 
 
复制代码


调用方法:

PHP复制代码
 
include('redisbatch.php');
 
$batch = new RedisBatch();
for($i=1; $i<=100; $i++){
        $batch->add($i);
}
 
if($batch->_Runflag){
        $get = $batch->get();
        var_dump('run');
        $batch->clear();
}else{
        var_dump('no run');
}
 
 
复制代码


评分

参与人数 1威望 +5 收起 理由
Hex + 5 赞一个!

查看全部评分

发表于 2012-4-13 15:33:11 | 显示全部楼层
new Redis呢   标题党  {:soso_e122:}
 楼主| 发表于 2012-4-16 12:10:59 | 显示全部楼层
sdink 发表于 2012-4-13 15:33
new Redis呢   标题党

__construct 中就实例化了 
发表于 2012-4-16 12:56:50 | 显示全部楼层
longniao 发表于 2012-4-16 12:10
__construct 中就实例化了 

是化了。但是类呢 引入了吗
 楼主| 发表于 2012-4-16 14:51:45 | 显示全部楼层
sdink 发表于 2012-4-16 12:56
是化了。但是类呢 引入了吗

只要安装php的redis扩展,就可以直接使用new Redis(),不需要引入
发表于 2012-4-16 14:58:35 | 显示全部楼层
不错学习下
发表于 2012-4-16 16:13:03 | 显示全部楼层
longniao 发表于 2012-4-16 14:51
只要安装php的redis扩展,就可以直接使用new Redis(),不需要引入

恩。
发表于 2012-9-14 16:02:43 | 显示全部楼层
今天看到一个不需要安装phpredis扩展的
https://github.com/joelcox/codeigniter-redis
发表于 2012-9-24 17:26:27 | 显示全部楼层
原来还有这么一个扩展

本版积分规则