php redis(2)

【config_inc.php】
<?php
//APP的读账号
//$config['appServer']['servername'] = 'app.com';
$config['appServer']['servername'] = '127.0.0.1';
$config['appServer']['port'] = 3306;
$config['appServer']['username'] = 'app';
$config['appServer']['password'] = 'fjdi23ksKdj';
$config['appServer']['pconnect'] = 0;
$config['appServer']['dbname'] = 'research';
$config['appServer']['charset'] = 'utf8';
 
 
/*redis 队列配置文件*/
//默认配置
$redis_cfg['default']['server'] = '127.0.0.1';//write
$redis_cfg['default']['port'] = '6379';//write
$redis_cfg['read']['server'] = '127.0.0.1';
$redis_cfg['read']['port'] = '6379';
?>
 
 
 
【redis.class.php】
<?php
 /**
 * redis 公共类
 * 使用phpredis扩展,根据目前应用只提供如下方法,请不要随意添加,如有需要请与作者联系
 * @version 1.00
 * @author sean
 */
class redis_class{
    private $ret = Null;
    private $rets = Array();
 
    public function __construct(){}
 
    public function connect($redis_cfg){
        //iflese用来处理是否连接过,连接过则直接返回
        if(isset($this -> rets[$redis_cfg['server']]) === TRUE){
            $this -> ret = $this -> rets[$redis_cfg['server']];
            return TRUE;
        }else{
            $this -> ret = new Redis();
            //判断是否连接成功
            if($this -> ret -> connect($redis_cfg['server'], $redis_cfg['port']) === TRUE){
                $this -> rets[$redis_cfg['server']] = $this -> ret;
                return TRUE;
            }
        }
    }
 
    public function close(){
        foreach($this -> rets as $rets){
            $rets -> close();
        }
    }
 
    public function sadd($key, $message = ''){
        global $redis_cfg;
        $this -> connnect($redis_cfg['default']);
        //sAdd:向键为Key的Set中加值,如果值存在,则不写入,返回FALSE
        return $this -> ret -> sAdd($key, $message);
    }
 
    public function sget($key){
        global $redis_cfg;
        $this -> connect($redis_cfg['read']);
        //sort:对名称为Key的set集合进行排序 默认升序
        return $this -> ret -> sort($key);
    }
 
    public function srem($key, $value){
        global $redis_cfg;
        $this -> connect($redis_cfg['default']);
        //sRem:删除名为Key的set中的一个value
        return $this -> ret -> sRem($key, $value);
    }
     
    public function setex($key, $value = '', $time = 86400){
        global $redis_cfg;
        $this -> connect($redis_cfg['default']);
        //setex: 设置数据缓存时间
        return $this -> ret -> setex($key, $time, serialize($value));
    }
 
    public function set($key, $value){
        global $redis_cfg;
        $this -> connect($redis_cfg['default']);
        //set:写入key 和 value
        return $this -> ret -> set($key, serialize($value));
    }
 
    public function get($key){
        global $redis_cfg;
        $this -> connect($redis_cfg['read']);
        //get:读取某个key的值,如果key不存在,返回FALSE
        return unserialize($this -> ret -> get($key));
    }
 
    public function delete($key){
        global $redis_cfg;
        $this -> connect($redis_cfg['default']);
        //delete: 删除指定key的值 返回已删除key的个数(长整数)
        return $this -> ret -> delete($key);
    }
 
    public function hincrby($hash, $key, $num){
        global $redis_cfg;
        $this -> connect($redis_cfg['default']);
        //hIncrBy:将名称为hash中的key的值增加num
        return $this -> ret -> hIncrBy($hash, $key, $num);
    }
 
    public function hget($hash, $key){
        global $redis_cfg;
        $this -> connect($redis_cfg['read']);
        //hGet: 返回名称为hash中key的值
        return $this -> ret -> hGet($hash, $key);
    }
 
    public function hgetall($hash){
        global $redis_cfg;
        $this -> connect($redis_cfg['read']);
        //hGetAll: 返回名称为hash中所有的键及其对应的Value
        return $this -> ret -> hGetAll($hash);
    }
 
    //key不能md5加密, 否则无法读取信息
    public function redis_mc_set($key, $value, $max_cache_file_time = 86400){
         
        //只提取第一个表名
        preg_match_all('/[\s]+from[\s]+([^,\s(.*)]*)/i',$key, $out);
        if(isset($out[1][0]) === TRUE){
            $table_name = $out[1][0];
            $all_tables = $this -> sget('TABLE_ALL');
            //验证数据库的合法性
            if(in_array($table_name, $all_tables) === FALSE){
                return FALSE;
            }
        }else{
            $talbe_name = 'other_keys';
        }
         
        $tmp_res = $this -> sadd('TABLE_' . $table_name, $key);
        if($tmp_res){
            $this -> hincrby('KEY_SIZE', $key, strlen(serialize($value)));
        }
        $this -> hincrby('KEY_WRITE_NUMBER',$key, 1);
        return $this -> setex(md5($key), $value, $max_cache_file_time);
    }
 
    public function redis_mc_get($key){
        $this -> hincrby('KEY_NUMBER', $key, 1);
        return $this -> get(md5($key));
    }
}
?>
 
 
 
【produc.class.php 数据读取放入redis】
<?php
require_once('config_inc.php');
class product{
    function get_sql_array($db,$sql,$flash=0,$max_cache_file_time=3600){
        require_once('redis.class.php');
        $redis = new redis_class();
        if ($flash == 0)
        {
            $res = $redis->redis_mc_get($sql);
        }
        if ((isset($res) === FALSE) || ($res === FALSE))
        {
            $res = $db->fetch_array($db->query($sql));
            $redis -> redis_mc_set($sql , $res, $max_cache_file_time);
        }
        return $res;
    }
}
 
//实例
$prd = new product();
$result = $prd -> get_sql_array($db,$sql,$flash=0,$max_cache_file_time=3600);
var_dump($result);
?>

 

posted @ 2014-11-29 14:50  jshaibozhong  阅读(310)  评论(0编辑  收藏  举报