php封装redis及调用

分为两个文件,
第一个用于具体redis的操作封装

直接上代码:

1.Common/Custom/Db/Redis.class.php

<?php
namespace Common\Custom\Db;

/**
 * redis
 *
 * @return   void
 **/
class Redis
{
    
    static $db = null;
    static $_config = null;
    private function __construct($params)
    {
        if (!class_exists('Redis')) {
            exit('请安装redis插件');
        }

        try {
            $this->_getRedisClient($params);
        } catch(\Exception $e) {
            exit('连接redis失败');
        }
    }

    /**
     * 获取redis示例
     *
     * @param    array
     *
     * @return   object
     */
    static public function getInstance($params = array())
    {
        self::$_config = array(
            'host' => C('REDIS_HOST'),
            'port' => C('REDIS_PORT'),
        );
        $params = !empty($params) ? $params : self::$_config;
        if (empty(self::$db[$params['host']])) {
            self::$db[$params['host']] = new self($params);
        }

        return self::$db[$params['host']];
    }

    /**
     * 获取redis示例
     *
     * @param    array
     * @param    int
     *
     * @return   object
     */
    private function _getRedisClient($params,$retry = 3)
    {
        try {
            $this->_redis = new \Redis();
            if(isset($params['host'])){
                $this->_redis->connect($params['host'], $params['port']);
            }else{
                $this->_redis->connect(C('REDIS_HOST'), C('REDIS_PORT'));
            }
            return;
        } catch(\Exception $e) {
        }

        if ($retry > 0) {
            return $this->_getRedisClient($params, --$retry);
        }

        throw new \Exception('尝试3次连接失败');
    }

    /**
     * 设置hash数据
     *
     * @param    string
     * @param    array
     *
     * @return   boolean
     **/
    public function hMset($key, $params)
    {
        return $this->_redis->hMset($key, $params);
    }

    /**
     * 返回hash数据
     *
     * @param    string
     *
     * @return   array
     **/
    public function hGetAll($key)
    {
        return $this->_redis->hGetAll($key);
    }

    /**
     * 返回hash 对应的字段的数据
     *
     * @param    string
     * @param    array
     *
     * @return   array
     **/
    public function hMget($key, $params)
    {
        return $this->_redis->hMget($key, $params);
    }

    /**
     * 模糊匹配 redis对应的key
     *
     * @param    string
     *
     * @return   boolean
     **/
    public function keys($key)
    {
        return $this->_redis->keys($key);
    }

    /**
     * 删除对应的key
     *
     * @param    string
     *
     * @return   boolean
     **/
    public function delete($key)
    {
        return $this->_redis->delete($key);
    }

    /**
     * 删除对应的key
     *
     * @param    string
     *
     * @return   boolean
     **/
    public function blurDelete($key)
    {
        $keys = $this->_redis->keys($key);
        foreach ($keys as $key) {
            $this->_redis->delete($key);
        }
        return true;
    }

    /**
     * 给一个值添加多条记录
     *
     * @param    string
     *
     * @return   boolean
     **/
    public function sAdd($key, $values)
    {
        $pipeline = $this->_redis->pipeline();

        foreach($values as $value) {
            $pipeline->sAdd($key, $value);
        }

        return $this->_redis->exec();
    }

    /**
     * 获取一个值的多条记录
     *
     * @param    string
     *
     * @return   array
     **/
    public function sMembers($key)
    {
        return $this->_redis->sMembers($key);
    }



    /**
     * 返回错误
     *
     * @return   string
     **/
    public function getError()
    {
        return $this->_error;
    }
    
}

2.调用

<?php
namespace Home\Logic\Redis;
/**
 * 公告模块
 * 
 *
 *
 * @return void
 */
class NoticeLogic 
{
    private $_redis_api;
    public function __construct()
    {
        $this->_redis_api = \Common\Custom\Db\Redis::getInstance();
	}

    /**
    * 获取所有notice信息
    *
    * @return array
    */
    public function getAllNoticeInfos()
    {
        $keys = $this->_redis_api->keys('notice:*');
        $infos = array();
        foreach ($keys as $key) {
            $info = $this->_redis_api->hMget($key, array('notice_id', 'title', 'creat_ts', 'order_num', 'is_alert'));
            $infos[$info['notice_id']] = $info;
        }

        krsort($infos);

        return $infos;
    }

    /**
    * 获取所有member_notice信息
    *
    * @return array
    */
    public function getAllMemberNoticeInfos($member_id)
    {
        $keys = $this->_redis_api->keys('notice_member:'.$member_id.'_*');

        $infos = array();
        foreach ($keys as $key) {
            $info = $this->_redis_api->hMget($key, array('notice_id', 'member_id'));
            $infos[$info['notice_id']] = $info;
        }

        krsort($infos);

        return $infos;
    }
    /**
    * 获取会员记录的公告id
    *
    * @param    int
    *
    * @return   array
    */
    public function getNoticeMemberInfoByMemberId($member_id,$notice_id)
    {
        if (empty($member_id)) {
            $this->_error = 'member id不正确';
            return false;
        }

        return $this->_redis_api->hMget('notice_member:'.$member_id.'_'.$notice_id, array('notice_id','member_id'));
    }

    /**
    * 更新公告会员信息
    *
    * @param    array
    *
    * @return   boolean
    */
    public function updateNoticeMemberInfoByMemberId($member_id, $params)
    {
        if (empty($member_id)) {
            $this->_error = '会员ID不能为空';
            return false;
        }

        if (count($params) < 1) {
            $this->_error = '参数为空';
            return false;
        }

        return $this->_redis_api->hMset('notice_member:'.$member_id.'_'.$params['notice_id'], $params);
    }

    /**
    * 根据公告ID获取公告信息
    *
    * @param    int
    *
    * @return   array
    */
    public function getNoticeInfoByNoticeId($notice_id)
    {
        if (intval($notice_id) < 1) {
            $this->_error = 'notice id不正确';
            return false;
        }

        return $this->_redis_api->hMget('notice:'.$notice_id, array('notice_id', 'title', 'content', 'creat_ts'));
    }
	 
	 
    /**
     * 返回错误信息
     *
     * @return    string
     **/
    public function getError()
    {
        return $this->_error;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值