<?php
/**
* ------------------------------------------
* 統一redis的配置與數據存儲規范,便於擴展與修改
* # redis通常用於熱數據與消息列隊等場景
* # list內存儲array是采用json格式
*
*/
class RedisDriver
{
protected $redis; // redis對象
protected $ip = '127.0.0.1'; // redis服務器ip地址
protected $port = '6379'; // redis服務器端口
protected $passwd = null; // redis密碼
public function __construct($config = array())
{
$this->redis = new Redis();
empty($config) or $this->connect($config);
}
// 連接redis服務器
public function connect($config = array())
{
if (!empty($config)) {
$this->ip = $config['ip'];
$this->port = $config['port'];
if (isset($config['passwd'])) {
$this->passwd = $config['passwd'];
}
}
$state = $this->redis->connect($this->ip, $this->port);
if ($state == false) {
die('redis connect failure');
}
if (!is_null($this->passwd)) {
$this->redis->auth($this->passwd);
}
}
// 設置一條String
public function setStr($key, $text, $expire = null)
{
$key = 'string:' . $key;
$this->redis->set($key, $text);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
}
// 獲取一條String
public function getStr($key)
{
$key = 'string:' . $key;
$text = $this->redis->get($key);
return empty($text) ? null : $text;
}
// 刪除一條String
public function delStr($key)
{
$key = 'string:' . $key;
$this->redis->del($key);
}
// 設置一條Hash
public function setHash($key, $arr, $expire = null)
{
$key = 'hash:' . $key;
$this->redis->hMset($key, $arr);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
}
// 獲取一條Hash,$fields可為字符串或數組
public function getHash($key, $fields = null)
{
$key = 'hash:' . $key;
if (is_null($fields)) {
$arr = $this->redis->hGetAll($key);
} else {
if (is_array($fields)) {
$arr = $this->redis->hmGet($key, $fields);
foreach ($arr as $key => $value) {
if ($value === false) {
unset($arr[$key]);
}
}
} else {
$arr = $this->redis->hGet($key, $fields);
}
}
return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
}
// 刪除一條Hash,$field為字符串
public function delHash($key, $field = null)
{
$key = 'hash:' . $key;
if (is_null($field)) {
$this->redis->del($key);
} else {
$this->redis->hDel($key, $field);
}
}
// 在Hash的field內增加一個值 (值之間使用“,”分隔)
public function fieldAddVal($key, $field, $val)
{
$arr = $this->getHash($key, $field);
if (!is_null($arr)) {
$str = reset($arr);
$arr = explode(',', $str);
foreach ($arr as $v) {
if ($v == $val) {
return;
}
}
$str .= ",{$val}";
$this->setHash($key, array($field => $str));
} else {
$this->setHash($key, array($field => $val));
}
}
// 在Hash的field內刪除一個值
public function fieldDelVal($key, $field, $val)
{
$arr = $this->getHash($key, $field);
if (!is_null($arr)) {
$arr = explode(',', reset($arr));
$tmpStr = '';
foreach ($arr as $v) {
if ($v != $val) {
$tmpStr .= ",{$v}";
}
}
if ($tmpStr == '') {
$this->delHash($key, $field);
} else {
$this->setHash($key, array($field => substr($tmpStr, 1)));
}
}
}
// 設置表格的一行數據
public function setTableRow($table, $id, $arr, $expire = null)
{
$key = '' . $table . ':' . $id;
$this->redis->hMset($key, $arr);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
}
// 獲取表格的一行數據,$fields可為字符串或數組
public function getTableRow($table, $id, $fields = null)
{
$key = '' . $table . ':' . $id;
if (is_null($fields)) {
$arr = $this->redis->hGetAll($key);
} else {
if (is_array($fields)) {
$arr = $this->redis->hmGet($key, $fields);
foreach ($arr as $key => $value) {
if ($value === false) {
unset($arr[$key]);
}
}
} else {
$arr = $this->redis->hGet($key, $fields);
}
}
return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
}
// 刪除表格的一行數據
public function delTableRow($table, $id)
{
$key = '' . $table . ':' . $id;
$this->redis->del($key);
}
// 推送一條數據至列表,頭部
public function pushList($key, $arr)
{
$key = 'list:' . $key;
$this->redis->lPush($key, json_encode($arr));
}
// 從列表拉取一條數據,尾部
public function pullList($key, $timeout = 0)
{
$key = 'list:' . $key;
if ($timeout > 0) {
$val = $this->redis->brPop($key, $timeout); // 該函數返回的是一個數組, 0=key 1=value
} else {
$val = $this->redis->rPop($key);
}
$val = is_array($val) && isset($val[1]) ? $val[1] : $val;
return empty($val) ? null : $this->objectToArray(json_decode($val));
}
// 取得列表的數據總條數
public function getListSize($key)
{
$key = 'list:' . $key;
return $this->redis->lSize($key);
}
// 刪除列表
public function delList($key)
{
$key = 'list:' . $key;
$this->redis->del($key);
}
// 使用遞歸,將stdClass轉為array
protected function objectToArray($obj)
{
if (is_object($obj)) {
$arr = (array) $obj;
}
if (is_array($obj)) {
foreach ($obj as $key => $value) {
$arr[$key] = $this->objectToArray($value);
}
}
return !isset($arr) ? $obj : $arr;
}
}