?php
/**
* redis操作類
* 說明,任何為false的串,存在redis中都是空串。
* 只有在key不存在時,才會返回false。
* 這點(diǎn)可用于防止緩存穿透
*
*/
class Redis
{
private $redis;
//當(dāng)前數(shù)據(jù)庫ID號
protected $dbId=0;
//當(dāng)前權(quán)限認(rèn)證碼
protected $auth;
/**
* 實(shí)例化的對象,單例模式.
* @var \iphp\db\Redis
*/
static private $_instance=array();
private $k;
//連接屬性數(shù)組
protected $attr=array(
//連接超時時間,redis配置文件中默認(rèn)為300秒
'timeout'=>30,
//選擇的數(shù)據(jù)庫。
'db_id'=>0,
);
//什么時候重新建立連接
protected $expireTime;
protected $host;
protected $port;
private function __construct($config,$attr=array())
{
$this->attr = array_merge($this->attr,$attr);
$this->redis = new Redis();
$this->port = $config['port'] ? $config['port'] : 6379;
$this->host = $config['host'];
$this->redis->connect($this->host, $this->port, $this->attr['timeout']);
if($config['auth'])
{
$this->auth($config['auth']);
$this->auth = $config['auth'];
}
$this->expireTime = time() + $this->attr['timeout'];
}
/**
* 得到實(shí)例化的對象.
* 為每個數(shù)據(jù)庫建立一個連接
* 如果連接超時,將會重新建立一個連接
* @param array $config
* @param int $dbId
* @return \iphp\db\Redis
*/
public static function getInstance($config, $attr = array())
{
//如果是一個字符串,將其認(rèn)為是數(shù)據(jù)庫的ID號。以簡化寫法。
if(!is_array($attr))
{
$dbId = $attr;
$attr = array();
$attr['db_id'] = $dbId;
}
$attr['db_id'] = $attr['db_id'] ? $attr['db_id'] : 0;
$k = md5(implode('', $config).$attr['db_id']);
if(! (static::$_instance[$k] instanceof self))
{
static::$_instance[$k] = new self($config,$attr);
static::$_instance[$k]->k = $k;
static::$_instance[$k]->dbId = $attr['db_id'];
//如果不是0號庫,選擇一下數(shù)據(jù)庫。
if($attr['db_id'] != 0){
static::$_instance[$k]->select($attr['db_id']);
}
}
elseif( time() > static::$_instance[$k]->expireTime)
{
static::$_instance[$k]->close();
static::$_instance[$k] = new self($config,$attr);
static::$_instance[$k]->k = $k;
static::$_instance[$k]->dbId= $attr['db_id'];
//如果不是0號庫,選擇一下數(shù)據(jù)庫。
if($attr['db_id']!=0){
static::$_instance[$k]->select($attr['db_id']);
}
}
return static::$_instance[$k];
}
private function __clone(){}
/**
* 執(zhí)行原生的redis操作
* @return \Redis
*/
public function getRedis()
{
return $this->redis;
}
/*****************hash表操作函數(shù)*******************/
/**
* 得到hash表中一個字段的值
* @param string $key 緩存key
* @param string $field 字段
* @return string|false
*/
public function hGet($key,$field)
{
return $this->redis->hGet($key,$field);
}
/**
* 為hash表設(shè)定一個字段的值
* @param string $key 緩存key
* @param string $field 字段
* @param string $value 值。
* @return bool
*/
public function hSet($key,$field,$value)
{
return $this->redis->hSet($key,$field,$value);
}
/**
* 判斷hash表中,指定field是不是存在
* @param string $key 緩存key
* @param string $field 字段
* @return bool
*/
public function hExists($key,$field)
{
return $this->redis->hExists($key,$field);
}
/**
* 刪除hash表中指定字段 ,支持批量刪除
* @param string $key 緩存key
* @param string $field 字段
* @return int
*/
public function hdel($key,$field)
{
$fieldArr=explode(',',$field);
$delNum=0;
foreach($fieldArr as $row)
{
$row=trim($row);
$delNum+=$this->redis->hDel($key,$row);
}
return $delNum;
}
/**
* 返回hash表元素個數(shù)
* @param string $key 緩存key
* @return int|bool
*/
public function hLen($key)
{
return $this->redis->hLen($key);
}
/**
* 為hash表設(shè)定一個字段的值,如果字段存在,返回false
* @param string $key 緩存key
* @param string $field 字段
* @param string $value 值。
* @return bool
*/
public function hSetNx($key,$field,$value)
{
return $this->redis->hSetNx($key,$field,$value);
}
/**
* 為hash表多個字段設(shè)定值。
* @param string $key
* @param array $value
* @return array|bool
*/
public function hMset($key,$value)
{
if(!is_array($value))
return false;
return $this->redis->hMset($key,$value);
}
/**
* 為hash表多個字段設(shè)定值。
* @param string $key
* @param array|string $value string以','號分隔字段
* @return array|bool
*/
public function hMget($key,$field)
{
if(!is_array($field))
$field=explode(',', $field);
return $this->redis->hMget($key,$field);
}
/**
* 為hash表設(shè)這累加,可以負(fù)數(shù)
* @param string $key
* @param int $field
* @param string $value
* @return bool
*/
public function hIncrBy($key,$field,$value)
{
$value=intval($value);
return $this->redis->hIncrBy($key,$field,$value);
}
/**
* 返回所有hash表的所有字段
* @param string $key
* @return array|bool
*/
public function hKeys($key)
{
return $this->redis->hKeys($key);
}
/**
* 返回所有hash表的字段值,為一個索引數(shù)組
* @param string $key
* @return array|bool
*/
public function hVals($key)
{
return $this->redis->hVals($key);
}
/**
* 返回所有hash表的字段值,為一個關(guān)聯(lián)數(shù)組
* @param string $key
* @return array|bool
*/
public function hGetAll($key)
{
return $this->redis->hGetAll($key);
}
/*********************有序集合操作*********************/
/**
* 給當(dāng)前集合添加一個元素
* 如果value已經(jīng)存在,會更新order的值。
* @param string $key
* @param string $order 序號
* @param string $value 值
* @return bool
*/
public function zAdd($key,$order,$value)
{
return $this->redis->zAdd($key,$order,$value);
}
/**
* 給$value成員的order值,增加$num,可以為負(fù)數(shù)
* @param string $key
* @param string $num 序號
* @param string $value 值
* @return 返回新的order
*/
public function zinCry($key,$num,$value)
{
return $this->redis->zinCry($key,$num,$value);
}
/**
* 刪除值為value的元素
* @param string $key
* @param stirng $value
* @return bool
*/
public function zRem($key,$value)
{
return $this->redis->zRem($key,$value);
}
/**
* 集合以order遞增排列后,0表示第一個元素,-1表示最后一個元素
* @param string $key
* @param int $start
* @param int $end
* @return array|bool
*/
public function zRange($key,$start,$end)
{
return $this->redis->zRange($key,$start,$end);
}
/**
* 集合以order遞減排列后,0表示第一個元素,-1表示最后一個元素
* @param string $key
* @param int $start
* @param int $end
* @return array|bool
*/
public function zRevRange($key,$start,$end)
{
return $this->redis->zRevRange($key,$start,$end);
}
/**
* 集合以order遞增排列后,返回指定order之間的元素。
* min和max可以是-inf和+inf 表示最大值,最小值
* @param string $key
* @param int $start
* @param int $end
* @package array $option 參數(shù)
* withscores=>true,表示數(shù)組下標(biāo)為Order值,默認(rèn)返回索引數(shù)組
* limit=>array(0,1) 表示從0開始,取一條記錄。
* @return array|bool
*/
public function zRangeByScore($key,$start='-inf',$end="+inf",$option=array())
{
return $this->redis->zRangeByScore($key,$start,$end,$option);
}
/**
* 集合以order遞減排列后,返回指定order之間的元素。
* min和max可以是-inf和+inf 表示最大值,最小值
* @param string $key
* @param int $start
* @param int $end
* @package array $option 參數(shù)
* withscores=>true,表示數(shù)組下標(biāo)為Order值,默認(rèn)返回索引數(shù)組
* limit=>array(0,1) 表示從0開始,取一條記錄。
* @return array|bool
*/
public function zRevRangeByScore($key,$start='-inf',$end="+inf",$option=array())
{
return $this->redis->zRevRangeByScore($key,$start,$end,$option);
}
/**
* 返回order值在start end之間的數(shù)量
* @param unknown $key
* @param unknown $start
* @param unknown $end
*/
public function zCount($key,$start,$end)
{
return $this->redis->zCount($key,$start,$end);
}
/**
* 返回值為value的order值
* @param unknown $key
* @param unknown $value
*/
public function zScore($key,$value)
{
return $this->redis->zScore($key,$value);
}
/**
* 返回集合以score遞增加排序后,指定成員的排序號,從0開始。
* @param unknown $key
* @param unknown $value
*/
public function zRank($key,$value)
{
return $this->redis->zRank($key,$value);
}
/**
* 返回集合以score遞增加排序后,指定成員的排序號,從0開始。
* @param unknown $key
* @param unknown $value
*/
public function zRevRank($key,$value)
{
return $this->redis->zRevRank($key,$value);
}
/**
* 刪除集合中,score值在start end之間的元素 包括start end
* min和max可以是-inf和+inf 表示最大值,最小值
* @param unknown $key
* @param unknown $start
* @param unknown $end
* @return 刪除成員的數(shù)量。
*/
public function zRemRangeByScore($key,$start,$end)
{
return $this->redis->zRemRangeByScore($key,$start,$end);
}
/**
* 返回集合元素個數(shù)。
* @param unknown $key
*/
public function zCard($key)
{
return $this->redis->zCard($key);
}
/*********************隊(duì)列操作命令************************/
/**
* 在隊(duì)列尾部插入一個元素
* @param unknown $key
* @param unknown $value
* 返回隊(duì)列長度
*/
public function rPush($key,$value)
{
return $this->redis->rPush($key,$value);
}
/**
* 在隊(duì)列尾部插入一個元素 如果key不存在,什么也不做
* @param unknown $key
* @param unknown $value
* 返回隊(duì)列長度
*/
public function rPushx($key,$value)
{
return $this->redis->rPushx($key,$value);
}
/**
* 在隊(duì)列頭部插入一個元素
* @param unknown $key
* @param unknown $value
* 返回隊(duì)列長度
*/
public function lPush($key,$value)
{
return $this->redis->lPush($key,$value);
}
/**
* 在隊(duì)列頭插入一個元素 如果key不存在,什么也不做
* @param unknown $key
* @param unknown $value
* 返回隊(duì)列長度
*/
public function lPushx($key,$value)
{
return $this->redis->lPushx($key,$value);
}
/**
* 返回隊(duì)列長度
* @param unknown $key
*/
public function lLen($key)
{
return $this->redis->lLen($key);
}
/**
* 返回隊(duì)列指定區(qū)間的元素
* @param unknown $key
* @param unknown $start
* @param unknown $end
*/
public function lRange($key,$start,$end)
{
return $this->redis->lrange($key,$start,$end);
}
/**
* 返回隊(duì)列中指定索引的元素
* @param unknown $key
* @param unknown $index
*/
public function lIndex($key,$index)
{
return $this->redis->lIndex($key,$index);
}
/**
* 設(shè)定隊(duì)列中指定index的值。
* @param unknown $key
* @param unknown $index
* @param unknown $value
*/
public function lSet($key,$index,$value)
{
return $this->redis->lSet($key,$index,$value);
}
/**
* 刪除值為vaule的count個元素
* PHP-REDIS擴(kuò)展的數(shù)據(jù)順序與命令的順序不太一樣,不知道是不是bug
* count>0 從尾部開始
* >0 從頭部開始
* =0 刪除全部
* @param unknown $key
* @param unknown $count
* @param unknown $value
*/
public function lRem($key,$count,$value)
{
return $this->redis->lRem($key,$value,$count);
}
/**
* 刪除并返回隊(duì)列中的頭元素。
* @param unknown $key
*/
public function lPop($key)
{
return $this->redis->lPop($key);
}
/**
* 刪除并返回隊(duì)列中的尾元素
* @param unknown $key
*/
public function rPop($key)
{
return $this->redis->rPop($key);
}
/*************redis字符串操作命令*****************/
/**
* 設(shè)置一個key
* @param unknown $key
* @param unknown $value
*/
public function set($key,$value)
{
return $this->redis->set($key,$value);
}
/**
* 得到一個key
* @param unknown $key
*/
public function get($key)
{
return $this->redis->get($key);
}
/**
* 設(shè)置一個有過期時間的key
* @param unknown $key
* @param unknown $expire
* @param unknown $value
*/
public function setex($key,$expire,$value)
{
return $this->redis->setex($key,$expire,$value);
}
/**
* 設(shè)置一個key,如果key存在,不做任何操作.
* @param unknown $key
* @param unknown $value
*/
public function setnx($key,$value)
{
return $this->redis->setnx($key,$value);
}
/**
* 批量設(shè)置key
* @param unknown $arr
*/
public function mset($arr)
{
return $this->redis->mset($arr);
}
/*************redis 無序集合操作命令*****************/
/**
* 返回集合中所有元素
* @param unknown $key
*/
public function sMembers($key)
{
return $this->redis->sMembers($key);
}
/**
* 求2個集合的差集
* @param unknown $key1
* @param unknown $key2
*/
public function sDiff($key1,$key2)
{
return $this->redis->sDiff($key1,$key2);
}
/**
* 添加集合。由于版本問題,擴(kuò)展不支持批量添加。這里做了封裝
* @param unknown $key
* @param string|array $value
*/
public function sAdd($key,$value)
{
if(!is_array($value))
$arr=array($value);
else
$arr=$value;
foreach($arr as $row)
$this->redis->sAdd($key,$row);
}
/**
* 返回?zé)o序集合的元素個數(shù)
* @param unknown $key
*/
public function scard($key)
{
return $this->redis->scard($key);
}
/**
* 從集合中刪除一個元素
* @param unknown $key
* @param unknown $value
*/
public function srem($key,$value)
{
return $this->redis->srem($key,$value);
}
/*************redis管理操作命令*****************/
/**
* 選擇數(shù)據(jù)庫
* @param int $dbId 數(shù)據(jù)庫ID號
* @return bool
*/
public function select($dbId)
{
$this->dbId=$dbId;
return $this->redis->select($dbId);
}
/**
* 清空當(dāng)前數(shù)據(jù)庫
* @return bool
*/
public function flushDB()
{
return $this->redis->flushDB();
}
/**
* 返回當(dāng)前庫狀態(tài)
* @return array
*/
public function info()
{
return $this->redis->info();
}
/**
* 同步保存數(shù)據(jù)到磁盤
*/
public function save()
{
return $this->redis->save();
}
/**
* 異步保存數(shù)據(jù)到磁盤
*/
public function bgSave()
{
return $this->redis->bgSave();
}
/**
* 返回最后保存到磁盤的時間
*/
public function lastSave()
{
return $this->redis->lastSave();
}
/**
* 返回key,支持*多個字符,?一個字符
* 只有* 表示全部
* @param string $key
* @return array
*/
public function keys($key)
{
return $this->redis->keys($key);
}
/**
* 刪除指定key
* @param unknown $key
*/
public function del($key)
{
return $this->redis->del($key);
}
/**
* 判斷一個key值是不是存在
* @param unknown $key
*/
public function exists($key)
{
return $this->redis->exists($key);
}
/**
* 為一個key設(shè)定過期時間 單位為秒
* @param unknown $key
* @param unknown $expire
*/
public function expire($key,$expire)
{
return $this->redis->expire($key,$expire);
}
/**
* 返回一個key還有多久過期,單位秒
* @param unknown $key
*/
public function ttl($key)
{
return $this->redis->ttl($key);
}
/**
* 設(shè)定一個key什么時候過期,time為一個時間戳
* @param unknown $key
* @param unknown $time
*/
public function exprieAt($key,$time)
{
return $this->redis->expireAt($key,$time);
}
/**
* 關(guān)閉服務(wù)器鏈接
*/
public function close()
{
return $this->redis->close();
}
/**
* 關(guān)閉所有連接
*/
public static function closeAll()
{
foreach(static::$_instance as $o)
{
if($o instanceof self)
$o->close();
}
}
/** 這里不關(guān)閉連接,因?yàn)閟ession寫入會在所有對象銷毀之后。
public function __destruct()
{
return $this->redis->close();
}
**/
/**
* 返回當(dāng)前數(shù)據(jù)庫key數(shù)量
*/
public function dbSize()
{
return $this->redis->dbSize();
}
/**
* 返回一個隨機(jī)key
*/
public function randomKey()
{
return $this->redis->randomKey();
}
/**
* 得到當(dāng)前數(shù)據(jù)庫ID
* @return int
*/
public function getDbId()
{
return $this->dbId;
}
/**
* 返回當(dāng)前密碼
*/
public function getAuth()
{
return $this->auth;
}
public function getHost()
{
return $this->host;
}
public function getPort()
{
return $this->port;
}
public function getConnInfo()
{
return array(
'host'=>$this->host,
'port'=>$this->port,
'auth'=>$this->auth
);
}
/*********************事務(wù)的相關(guān)方法************************/
/**
* 監(jiān)控key,就是一個或多個key添加一個樂觀鎖
* 在此期間如果key的值如果發(fā)生的改變,剛不能為key設(shè)定值
* 可以重新取得Key的值。
* @param unknown $key
*/
public function watch($key)
{
return $this->redis->watch($key);
}
/**
* 取消當(dāng)前鏈接對所有key的watch
* EXEC 命令或 DISCARD 命令先被執(zhí)行了的話,那么就不需要再執(zhí)行 UNWATCH 了
*/
public function unwatch()
{
return $this->redis->unwatch();
}
/**
* 開啟一個事務(wù)
* 事務(wù)的調(diào)用有兩種模式Redis::MULTI和Redis::PIPELINE,
* 默認(rèn)是Redis::MULTI模式,
* Redis::PIPELINE管道模式速度更快,但沒有任何保證原子性有可能造成數(shù)據(jù)的丟失
*/
public function multi($type=\Redis::MULTI)
{
return $this->redis->multi($type);
}
/**
* 執(zhí)行一個事務(wù)
* 收到 EXEC 命令后進(jìn)入事務(wù)執(zhí)行,事務(wù)中任意命令執(zhí)行失敗,其余的命令依然被執(zhí)行
*/
public function exec()
{
return $this->redis->exec();
}
/**
* 回滾一個事務(wù)
*/
public function discard()
{
return $this->redis->discard();
}
/**
* 測試當(dāng)前鏈接是不是已經(jīng)失效
* 沒有失效返回+PONG
* 失效返回false
*/
public function ping()
{
return $this->redis->ping();
}
public function auth($auth)
{
return $this->redis->auth($auth);
}
/*********************自定義的方法,用于簡化操作************************/
/**
* 得到一組的ID號
* @param unknown $prefix
* @param unknown $ids
*/
public function hashAll($prefix,$ids)
{
if($ids==false)
return false;
if(is_string($ids))
$ids=explode(',', $ids);
$arr=array();
foreach($ids as $id)
{
$key=$prefix.'.'.$id;
$res=$this->hGetAll($key);
if($res!=false)
$arr[]=$res;
}
return $arr;
}
/**
* 生成一條消息,放在redis數(shù)據(jù)庫中。使用0號庫。
* @param string|array $msg
*/
public function pushMessage($lkey,$msg)
{
if(is_array($msg)){
$msg = json_encode($msg);
}
$key = md5($msg);
//如果消息已經(jīng)存在,刪除舊消息,已當(dāng)前消息為準(zhǔn)
//echo $n=$this->lRem($lkey, 0, $key)."\n";
//重新設(shè)置新消息
$this->lPush($lkey, $key);
$this->setex($key, 3600, $msg);
return $key;
}
/**
* 得到條批量刪除key的命令
* @param unknown $keys
* @param unknown $dbId
*/
public function delKeys($keys,$dbId)
{
$redisInfo=$this->getConnInfo();
$cmdArr=array(
'redis-cli',
'-a',
$redisInfo['auth'],
'-h',
$redisInfo['host'],
'-p',
$redisInfo['port'],
'-n',
$dbId,
);
$redisStr=implode(' ', $cmdArr);
$cmd="{$redisStr} KEYS \"{$keys}\" | xargs {$redisStr} del";
return $cmd;
}
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》