具體redis類
redis.php
<?php
/**
* redis操作類
* 說明,任何爲false的串,存在redis中都是空串。
* 只有在key不存在時,纔會返回false。
* 這點可用於防止緩存穿透
*
*/
class RedisServer
{
private $redis;
//當前數據庫ID號
protected $dbId = 0;
//當前權限認證碼
protected $auth;
/**
* 實例化的對象,單例模式.
* @var \iphp\db\Redis
*/
static private $_instance = array();
private $k;
//連接屬性數組
protected $attr = array(
//連接超時時間,redis配置文件中默認爲300秒
'timeout' => 30,
//選擇的數據庫。
'db_id' => 0,
);
//什麼時候重新建立連接
protected $expireTime;
protected $host;
protected $port = 6379;
public function __construct($attr=[]) {
$this->attr = array_merge($this->attr, $attr);
$this->redis = new Redis();
//可以根據自定義的系統環境變量來區分master和online分支,選擇連接不同的redis庫
$path = getenv('QIPA_ENV');
switch ($path) {
case 'test':
case 'local':
$this->host = '127.0.0.1';
break;
case 'online':
$this->host = 'www.qipa250.com';
}
$this->redis->connect($this->host, $this->port, $this->attr['timeout']);
$this->expireTime = time() + $this->attr['timeout'];
}
/**
* 執行原生的redis操作
* @return \Redis
*/
public function getRedis() {
return $this->redis;
}
/*****************hash表操作函數*******************/
/**
* 得到hash表中一個字段的值
* @param string $key 緩存key
* @param string $field 字段
* @return string|false
*/
public function hGet($key, $field) {
return $this->redis->hGet($key, $field);
}
/**
* 爲hash表設定一個字段的值
* @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表元素個數
* @param string $key 緩存key
* @return int|bool
*/
public function hLen($key) {
return $this->redis->hLen($key);
}
/**
* 爲hash表設定一個字段的值,如果字段存在,返回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表多個字段設定值。
* @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表多個字段設定值。
* @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表設這累加,可以負數
* @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表的字段值,爲一個索引數組
* @param string $key
* @return array|bool
*/
public function hVals($key) {
return $this->redis->hVals($key);
}
/**
* 返回所有hash表的字段值,爲一個關聯數組
* @param string $key
* @return array|bool
*/
public function hGetAll($key) {
return $this->redis->hGetAll($key);
}
/*********************有序集合操作*********************/
/**
* 給當前集合添加一個元素
* 如果value已經存在,會更新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,可以爲負數
* @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 參數
* withscores=>true,表示數組下標爲Order值,默認返回索引數組
* 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 參數
* withscores=>true,表示數組下標爲Order值,默認返回索引數組
* 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之間的數量
* @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 刪除成員的數量。
*/
public function zRemRangeByScore($key, $start, $end) {
return $this->redis->zRemRangeByScore($key, $start, $end);
}
/**
* 返回集合元素個數。
* @param unknown $key
*/
public function zCard($key) {
return $this->redis->zCard($key);
}
/*********************隊列操作命令************************/
/**
* 在隊列尾部插入一個元素
* @param unknown $key
* @param unknown $value
* 返回隊列長度
*/
public function rPush($key, $value) {
return $this->redis->rPush($key, $value);
}
/**
* 在隊列尾部插入一個元素 如果key不存在,什麼也不做
* @param unknown $key
* @param unknown $value
* 返回隊列長度
*/
public function rPushx($key, $value) {
return $this->redis->rPushx($key, $value);
}
/**
* 在隊列頭部插入一個元素
* @param unknown $key
* @param unknown $value
* 返回隊列長度
*/
public function lPush($key, $value) {
return $this->redis->lPush($key, $value);
}
/**
* 在隊列頭插入一個元素 如果key不存在,什麼也不做
* @param unknown $key
* @param unknown $value
* 返回隊列長度
*/
public function lPushx($key, $value) {
return $this->redis->lPushx($key, $value);
}
/**
* 返回隊列長度
* @param unknown $key
*/
public function lLen($key) {
return $this->redis->lLen($key);
}
/**
* 返回隊列指定區間的元素
* @param string $key
* @param integer $start
* @param integer $end
*/
public function lRange($key, $start, $end) {
return $this->redis->lrange($key, $start, $end);
}
/**
* 返回隊列中指定索引的元素
* @param unknown $key
* @param unknown $index
*/
public function lIndex($key, $index) {
return $this->redis->lIndex($key, $index);
}
/**
* 設定隊列中指定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擴展的數據順序與命令的順序不太一樣,不知道是不是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);
}
/**
* 刪除並返回隊列中的頭元素。
* @param unknown $key
*/
public function lPop($key) {
return $this->redis->lPop($key);
}
/**
* 刪除並返回隊列中的尾元素
* @param unknown $key
*/
public function rPop($key) {
return $this->redis->rPop($key);
}
/*************redis字符串操作命令*****************/
/**
* 設置一個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);
}
/**
* 設置一個有過期時間的key
* @param unknown $key
* @param unknown $expire
* @param unknown $value
*/
public function setex($key, $expire, $value) {
return $this->redis->setex($key, $expire, $value);
}
/**
* 設置一個key,如果key存在,不做任何操作.
* @param unknown $key
* @param unknown $value
*/
public function setnx($key, $value) {
return $this->redis->setnx($key, $value);
}
/**
* 批量設置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);
}
/**
* 添加集合。由於版本問題,擴展不支持批量添加。這裏做了封裝
* @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);
}
/**
* 返回無序集合的元素個數
* @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管理操作命令*****************/
/**
* 選擇數據庫
* @param int $dbId 數據庫ID號
* @return bool
*/
public function select($dbId) {
$this->dbId = $dbId;
return $this->redis->select($dbId);
}
/**
* 清空當前數據庫
* @return bool
*/
public function flushDB() {
return $this->redis->flushDB();
}
/**
* 返回當前庫狀態
* @return array
*/
public function info() {
return $this->redis->info();
}
/**
* 同步保存數據到磁盤
*/
public function save() {
return $this->redis->save();
}
/**
* 異步保存數據到磁盤
*/
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設定過期時間 單位爲秒
* @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);
}
/**
* 設定一個key什麼時候過期,time爲一個時間戳
* @param unknown $key
* @param unknown $time
*/
public function exprieAt($key, $time) {
return $this->redis->expireAt($key, $time);
}
/**
* 關閉服務器鏈接
*/
public function close() {
return $this->redis->close();
}
/**
* 關閉所有連接
*/
public static function closeAll() {
foreach (static::$_instance as $o) {
if ($o instanceof self)
$o->close();
}
}
/** 這裏不關閉連接,因爲session寫入會在所有對象銷燬之後。
* public function __destruct()
* {
* return $this->redis->close();
* }
**/
/**
* 返回當前數據庫key數量
*/
public function dbSize() {
return $this->redis->dbSize();
}
/**
* 返回一個隨機key
*/
public function randomKey() {
return $this->redis->randomKey();
}
/**
* 得到當前數據庫ID
* @return int
*/
public function getDbId() {
return $this->dbId;
}
/**
* 返回當前密碼
*/
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
);
}
/*********************事務的相關方法************************/
/**
* 監控key,就是一個或多個key添加一個樂觀鎖
* 在此期間如果key的值如果發生的改變,剛不能爲key設定值
* 可以重新取得Key的值。
* @param unknown $key
*/
public function watch($key) {
return $this->redis->watch($key);
}
/**
* 取消當前鏈接對所有key的watch
* EXEC 命令或 DISCARD 命令先被執行了的話,那麼就不需要再執行 UNWATCH 了
*/
public function unwatch() {
return $this->redis->unwatch();
}
/**
* 開啓一個事務
* 事務的調用有兩種模式Redis::MULTI和Redis::PIPELINE,
* 默認是Redis::MULTI模式,
* Redis::PIPELINE管道模式速度更快,但沒有任何保證原子性有可能造成數據的丟失
*/
public function multi($type = \Redis::MULTI) {
return $this->redis->multi($type);
}
/**
* 執行一個事務
* 收到 EXEC 命令後進入事務執行,事務中任意命令執行失敗,其餘的命令依然被執行
*/
public function exec() {
return $this->redis->exec();
}
/**
* 回滾一個事務
*/
public function discard() {
return $this->redis->discard();
}
/**
* 測試當前鏈接是不是已經失效
* 沒有失效返回+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數據庫中。使用0號庫。
* @param string|array $msg
*/
public function pushMessage($lkey, $msg) {
if (is_array($msg)) {
$msg = json_encode($msg);
}
$key = md5($msg);
//如果消息已經存在,刪除舊消息,已當前消息爲準
//echo $n=$this->lRem($lkey, 0, $key)."\n";
//重新設置新消息
$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;
}
}
引用redis類
require_once 'redis.php';
$redisInstance = new RedisServer(['db_id' => 0]);