thinkphp封装phpredis类

本文介绍了如何在ThinkPHP框架中封装并使用phpredis类,通过redisModel类扩展实现对Redis的数据操作,包括配置项定义及常用命令如string、list等的使用。
摘要由CSDN通过智能技术生成

redisModel类扩展:Think/Extend/Model/redisModel.class.php

配置项定义:conf/config.php

/*Redis设置*/
'REDIS_HOST'		=> 'localhost', //主机
'REDIS_PORT'		=> 6379, //端口
'REDIS_DBNAME'          => 'appdb', //库名
'REDIS_CTYPE'           => 1, //连接类型 1:普通连接 2:长连接
'REDIS_TIMEOUT'         => 0, //连接超时时间(S) 0:永不超时

扩展类封装代码:

<?php
/**
 * phpredis扩展
 * by wbq 2012-5-22
 */
class RedisModel extends Model
{
	//REDIS服务主机IP
	private $_HOST = null;

	//redis服务端口
	private $_PORT = null;

	//连接时长 默认为0 不限制时长
	private $_TIMEOUT = 0;

	//数据库名
	private $_DBNAME = null;

	//连接类型 1普通连接 2长连接
	private $_CTYPE = 1;

	//实例名
	public $_REDIS = null;

	//事物对象
	private $_TRANSCATION = null;

	//初始化
	public function __construct()
	{
		$this->_HOST = C('REDIS_HOST');
		$this->_PORT = C('REDIS_PORT');
		$this->_TIMEOUT = C('REDIS_TIMEOUT');
		$this->_DBNAME = C('REDIS_DBNAME');
		$this->_CTYPE = C('REDIS_CTYPE');

		if (!isset($this->_REDIS)) {
			$this->_REDIS = new Redis();
			$this->connect($this->_HOST, $this->_PORT, $this->_TIMEOUT, $this->_DBNAME, $this->_CTYPE);
		}
	}

	/**
	 * 连接redis服务器
	 */
	private function connect($host,$port,$timeout,$dbname,$type)
	{
		switch ($type) {
			case 1:
				$this->_REDIS->connect($host, $port, $timeout);
			break;
			case 2:
				$this->_REDIS->pconnect($host, $port, $timeout);
			break;
			default:
			break;
		}
	}

	/**
	 * 查看redis连接是否断开
	 * @return $return bool true:连接未断开 false:连接已断开
	 */
	public function ping()
	{
		$return = null;

		$return = $this->_REDIS->ping();

		return 'PONG' ? true : false;
	}

	/**
	 * 设置redis模式参数
	 * @param $option array 参数数组键值对
	 * @return $return true/false 
	 */
	public function setOption($option=array())
	{
		return $this->_REDIS->setOption();
	}

	/**
	 * 获取redis模式参数
	 * @param $option array 要获取的参数数组
	 */
	public function getOption($option=array())
	{
		return $this->_REDIS->getOption();
	}

	/**
	 * 写入key-value
	 * @param $key string 要存储的key名
	 * @param $value mixed 要存储的值
	 * @param $type int 写入方式 0:不添加到现有值后面 1:添加到现有值的后面 默认0 
	 * @param $repeat int 0:不判断重复 1:判断重复
	 * @param $time float 过期时间(S)
	 * @param $old int 1:返回旧的value 默认0
	 * @return $return bool true:成功 flase:失败
	 */
	public function set($key,$value,$type=0,$repeat=0,$time=0,$old=0)
	{
		$return = null;

		if ($type) {
			$return = $this->_REDIS->append($key, $value);
		} else {
			if ($old) {
				$return = $this->_REDIS->getSet($key, $value);
			} else {
				if ($repeat) {
					$return = $this->_REDIS->setnx($key, $value);
				} else {
					if ($time && is_numeric($time)) $return = $this->_REDIS->setex($key, $time, $value);
					else $return = $this->_REDIS->set($key, $value);
				}
			}
		}

		return $return;
	}

	/**
	 * 获取某个key值 如果指定了start end 则返回key值的start跟end之间的字符
	 * @param $key string/array 要获取的key或者key数组
	 * @param $start int 字符串开始index
	 * @param $end int 字符串结束index
	 * @return $return mixed 如果key存在则返回key值 如果不存在返回false
	 */
	public function get($key=null,$start=null,$end=null)
	{
		$return = null;

		if (is_array($key) && !empty($key)) {
			$return = $this->_REDIS->getMultiple($key);
		} else {
			if (isset($start) && isset($end)) $return = $this->_REDIS->getRange($key);
			else $return = $this->_REDIS->get($key);
		}

		return $return;
	}

	/**
	 * 删除某个key值
	 * @param $key array key数组
	 * @return $return longint 删除成功的key的个数
	 */
	public function delete($key=array())
	{
		$return = null;

		$return = $this->_REDIS->delete($key);

		return $return;
	}

	/**
	 * 判断某个key是否存在
	 * @param $key string 要查询的key名
	 */
	public function exists($key)
	{
		$return = null;

		$return = $this->_REDIS->exists(
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值