redis thinkphp5.0的集成類

// +----------------------------------------------------------------------

// | ThinkPHP [ WE CAN DO IT JUST THINK ]

// +----------------------------------------------------------------------

// | Copyright (c) 2006~2017http://thinkphp.cnAll rights reserved.

// +----------------------------------------------------------------------

// | Licensed (http://www.apache.org/licenses/LICENSE-2.0)

// +----------------------------------------------------------------------

// | Author: liu21st <liu21st@gmail.com>

// +----------------------------------------------------------------------

namespace think\cache\driver;

use think\cache\Driver;

/**

* Redis緩存驅動,適合單機部署、有前端代理實現高可用的場景,性能最好

* 有需要在業務層實現讀寫分離、或者使用RedisCluster的需求,請使用Redisd驅動

*

* 要求安裝phpredis擴展:https://github.com/nicolasff/phpredis

* @author????robin <1257834076@qq.com>

*/

class Redis extends Driver {

protected $options = [

'host' => '127.0.0.1',

'port' => 6379,

'password' => '',

'select' => 2,

'timeout' => 0,

'expire' => 0,

'persistent' => false,

'prefix' => '',

];

/**

* 構造函數

* @param array $options 緩存參數

* @access public

*/

public function __construct($options = []) {

if (!extension_loaded('redis')) {

throw new \BadFunctionCallException('not support: redis');

}

if (!empty($options)) {

$this->options = array_merge($this->options, $options);

}

$func = $this->options['persistent'] ? 'pconnect' : 'connect';

$this->handler = new \Redis;

$this->handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);

if ('' != $this->options['password']) {

$this->handler->auth($this->options['password']);

}

if (0 != $this->options['select']) {

$this->handler->select($this->options['select']);

}

}

/**

* 判斷緩存

* @access public

* @param string $name 緩存變量名

* @return bool

*/

public function has($name) {

return $this->handler->get($this->getCacheKey($name)) ? true : false;

}

/**

* 讀取緩存

* @access public

* @param string $name 緩存變量名

* @param mixed??$default 默認值

* @return mixed

*/

public function get($name, $default = false) {

$value = $this->handler->get($this->getCacheKey($name));

if (is_null($value) || false === $value) {

return $default;

}

$jsonData = json_decode($value, true);

// 檢測是否為JSON數據 true 返回JSON解析數組, false返回源數據 byron sampson<xiaobo.sun@qq.com>

return (null === $jsonData) ? $value : $jsonData;

}

/**

* 寫入緩存

* @access public

* @param string????????????$name 緩存變量名

* @param mixed?????????????$value??存儲數據

* @param integer|\DateTime $expire??有效時間(秒)

* @return boolean

*/

public function set($name, $value, $expire = null) {

if (is_null($expire)) {

$expire = $this->options['expire'];

}

if ($expire instanceof \DateTime) {

$expire = $expire->getTimestamp() - time();

}

if ($this->tag && !$this->has($name)) {

$first = true;

}

$key = $this->getCacheKey($name);

//對數組/對象數據進行緩存處理,保證數據完整性??byron sampson<xiaobo.sun@qq.com>

$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;

if (is_int($expire) && $expire) {

$result = $this->handler->setex($key, $expire, $value);

} else {

$result = $this->handler->set($key, $value);

}

isset($first) && $this->setTagItem($key);

return $result;

}

/**

* 自增緩存(針對數值緩存)

* @access public

* @param string????$name 緩存變量名

* @param int???????$step 步長

* @return false|int

*/

public function inc($name, $step = 1) {

$key = $this->getCacheKey($name);

return $this->handler->incrby($key, $step);

}

/**

* 自減緩存(針對數值緩存)

* @access public

* @param string????$name 緩存變量名

* @param int???????$step 步長

* @return false|int

*/

public function dec($name, $step = 1) {

$key = $this->getCacheKey($name);

return $this->handler->decrby($key, $step);

}

/**

* 刪除緩存

* @access public

* @param string $name 緩存變量名

* @return boolean

*/

public function rm($name) {

return $this->handler->delete($this->getCacheKey($name));

}

/**

* 清除緩存

* @access public

* @param string $tag 標簽名

* @return boolean

*/

public function clear($tag = null) {

if ($tag) {

// 指定標簽清除

$keys = $this->getTagItem($tag);

foreach ($keys as $key) {

$this->handler->delete($key);

}

$this->rm('tag_' . md5($tag));

return true;

}

return $this->handler->flushDB();

}

//Redis lpush 命令用于將一個或多個值插入到列表的尾部(最右邊)

public function lPush($key, $value) {

return $this->handler->lPush($key, $value);

}

//移除并獲取列表的第一個元素

public function lPop($key) {

return $this->handler->lPop($key);

}

/**

* 獲取值長度

* @param string $key

* @return int

*/

public function lLen($key) {

return $this->handler->lLen($key);

}

//不確定是不是方法不對有待驗證

/**

* 寫入有序列表

* @access public

* @param string $key 列表名

* @param double $score??成員分數

* @param string $member??成員名

* @return boolean

*/

public function Zadd($key, $score, $member) {

if (is_double($score)) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$member = (is_object($member) || is_array($member)) ? json_encode($member) : $member;

$result = $this->handler->Zadd($key, $score, $member);

return $result;

} else {

return false;

}

}

/**

* 統計有序列表成員數

* @access public

* @param string $key 列表名

* @return??int or boolean

*/

public function Zcard($key) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$result = $this->handler->Zcard($key);

return $result;

}

/**

* 獲得有序列表成員排名

* @access public

* @param string??$key 列表名

* @param string $member??成員名

* @return boolean

*/

public function Zrevrank($key, $member) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$member = (is_object($member) || is_array($member)) ? json_encode($member) : $member;

$result = $this->handler->Zrevrank($key, $member);

return $result;

}

/**

* 給有序列表制定成員分數增量

* @access public

* @param string??$key 列表名

* @param float or int $value 增量分數

* @param string $member??成員名

* @return boolean

*/

public function Zincrby($key, $value, $member) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$value = is_numeric($value) ? $value : doubleval($value);

$member = (is_object($member) || is_array($member)) ? json_encode($member) : $member;

$result = $this->handler->Zincrby($key, $value, $member);

return $result;

}

/**

* 獲得有序列表成員分數

* @access public

* @param string??$key 列表名

* @param string $member??成員名

* @return boolean

*/

public function Zscore($key, $member) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$member = (is_object($member) || is_array($member)) ? json_encode($member) : $member;

$result = $this->handler->Zscore($key, $member);

return $result;

}

/**

* 返回有序集中指定區間內的成員,通過索引,分數從高到底

* @access public

* @param string??$key 列表名

* @param int $start

* @param int $end

* @param string $withscore

* @return boolean

*/

public function Zrevrange($key, $start, $end, $withscore = null) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$start = is_int($start) ? $start : intval($start);

$end = is_int($end) ? $end : intval($end);

$result = $this->handler->Zrevrange($key, $start, $end, $withscore);

return $result;

}

/**

* 移除

*/

public function Zremrangebyrank($key, $start, $end) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$start = is_int($start) ? $start : intval($start);

$end = is_int($end) ? $end : intval($end);

$result = $this->handler->Zremrangebyrank($key, $start, $end);

return $result;

}

/*

* 以下都是hash方法

*

* 刪除一個或者多個哈希表字段

* @param???string??$key

* @param???string??$hashKey1

* @param???string??$hashKey2

* @param???string??$hashKeyN

*??@return??int?????Number of deleted fields

* */

public function Hdel($key, $hashKey1) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$hashKey1 = (is_object($hashKey1) || is_array($hashKey1)) ? json_encode($hashKey1) : $hashKey1;

$result = $this->handler->Hdel($key, $hashKey1);

return $result;

}

/* +@return??bool:???If the member exists in the hash table, return TRUE, otherwise return FALSE.

查看哈希表 key 中,指定的字段是否存在。

* @param???string??$key

* @param???string??$hashKey

* */

public function Hexists($key, $hashKey) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$hashKey = (is_object($hashKey) || is_array($hashKey)) ? json_encode($hashKey) : $hashKey;

$result = $this->handler->Hexists($key, $hashKey);

return $result;

}

/* 將哈希表 key 中的字段 field 的值設為 value 。

* @param???string??$key

* @param???string??$hashKey

* @param???string??$value

*??* @return??bool????TRUE if the field was set, FALSE if it was already present.

* */

public function Hset($key, $hashKey, $value) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$hashKey = (is_object($hashKey) || is_array($hashKey)) ? json_encode($hashKey) : $hashKey;

$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;

$result = $this->handler->Hset($key, $hashKey, $value);

return $result;

}

/* 獲取存儲在哈希表中指定字段的值

* * @param???string??$key

*??@param???string??$hashKey

@return??string??The value, if the command executed successfully BOOL FALSE in case of failure

* */

public function Hget($key, $hashKey) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$hashKey = (is_object($hashKey) || is_array($hashKey)) ? json_encode($hashKey) : $hashKey;

$result = $this->handler->Hget($key, $hashKey);

return $result;

}

/* 獲取存儲在哈希表中所有字段的值

* * @param???string??$key

@return??string??The value, if the command executed successfully BOOL FALSE in case of failure

@Returns the whole hash, as an array of strings indexed by strings.

* */

public function Hgetall($key) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$result = $this->handler->Hgetall($key);

return $result;

}

/* 為哈希表 key 中的指定字段的整數值加上增量 increment 。

* * @param???string??$key

* @param???string??$hashKey

* @param???int?????$value (integer) value that will be added to the member's value

* @return??int?????the new value

* */

public function Hincrby($key, $hashKey, $value) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$hashKey = (is_object($hashKey) || is_array($hashKey)) ? json_encode($hashKey) : $hashKey;

$value = intval($value);

$result = $this->handler->Hincrby($key, $hashKey, $value);

return $result;

}

/* 為哈希表 key 中的指定字段的浮點值加上增量 increment 。

* * @param???string??$key

* @param???string??$hashKey

* @param???float???$increment

* @return??float????the new value

* */

public function Hincrbyfloat($key, $hashKey, $value) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$hashKey = (is_object($hashKey) || is_array($hashKey)) ? json_encode($hashKey) : $hashKey;

$value = floatval($value);

$result = $this->handler->Hincrbyfloat($key, $hashKey, $value);

return $result;

}

/* 獲取存儲在哈希表中所有字段

* @param???string??$key

* @return??array???An array of elements, the keys of the hash. This works like PHP's array_keys().

@Returns the whole hash, as an array of strings indexed by strings.

* */

public function Hkeys($key) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$result = $this->handler->Hkeys($key);

return $result;

}

/* 獲取存儲在哈希表中字段的數量

* @param???string??$key

* @return??int?????the number of items in a hash, FALSE if the key doesn't exist or isn't a hash.

* */

public function Hlen($key) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$result = $this->handler->Hlen($key);

return $result;

}

/* 獲取存儲在哈希表中所有指定字段的值

* * @param???string??$key

* @param???array???$hashKeys

* @return??array???Array An array of elements, the values of the specified fields in the hash,

* */

public function Hmget($key, $hashKeys) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$result = $this->handler->Hmget($key, $hashKeys);

return $result;

}

/* 同時將多個 field-value (域-值)對設置到哈希表 key 中。

* ** @param???string??$key

* @param???array???$hashKeys key → value array

* @return??bool

* */

public function Hmset($key, $hashKeys) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$result = $this->handler->Hmset($key, $hashKeys);

return $result;

}

/* 將哈希表 key 中的字段 field 的值設為 value 。

* @param???string??$key

* @param???string??$hashKey

* @param???string??$value

*??* @return??bool????TRUE if the field was set, FALSE if it was already present.

* */

public function Hsetnx($key, $hashKey, $value) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$hashKey = (is_object($hashKey) || is_array($hashKey)) ? json_encode($hashKey) : $hashKey;

$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;

$result = $this->handler->Hsetnx($key, $hashKey, $value);

return $result;

}

/* 獲取哈希表中所有值

* @param???string??$key

* @return??array???An array of elements, the values of the hash. This works like PHP's array_values().

* */

public function Hvals($key) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$result = $this->handler->Hvals($key);

return $result;

}

/* 迭代哈希表中的鍵值對。

* @param???string??$key

* @param???string??$pattern

* @param???string??$value

*??* @return??bool????TRUE if the field was set, FALSE if it was already present.

* */

public function Hscan($key, $iterator, $pattern = '', $count = 0) {

$key = (is_object($key) || is_array($key)) ? json_encode($key) : $key;

$pattern = (is_object($pattern) || is_array($pattern)) ? json_encode($pattern) : $pattern;

$iterator = intval($iterator);

$count = intval($count);

$result = $this->handler->Hscan($key, $iterator, $pattern, $count);

return $result;

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,665評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,627評論 2 380