redis 哈希hash实例应用
redis 哈希實例
因為鄙人緩存方便略差。所以努力ing
隨便拿一個業務場景 來使用hash
業務場景:我要把一個停車場列表放到redis中,再根據這個列表內的數據計算距離不超過5公里的數據,并返回這個停車場列表
//這里hash的存儲方法都是經過封裝。下面有封裝的代碼
public funciton getParkList( $sLat,$slng,$id )
{
//由于redis我自己又封裝了一下使用的是tp框架,這里我實例化自己的redis類
$redis = D(‘Redis’);
//先獲取存放在redis中的hash 停車場數據
$park= $redis->hashGet(‘park:’. i d , [ ] , 2 ) ; / / 判 斷 緩 存 中 是 否 有 數 據 如 果 沒 有 , 查 詢 數 據 庫 , 在 存 在 r e d i s 中 i f ( e m p t y ( id,[],2); //判斷緩存中是否有數據如果沒有,查詢數據庫,在存在redis中 if ( empty( id,[],2);//判斷緩存中是否有數據如果沒有,查詢數據庫,在存在redis中if(empty(park) )
{
//獲取所有的數據
$parkAll = D(‘parking’)->getParkAll();
//遍歷數據,添加到緩存
foreach ( $parkAll as $key=>$value )
{
$redis->hashSet(‘park:’.$key,$value);
}
}
//處理數據
foreach ( $parkAll as $k=>$v )
{
//獲取相差的公里數
$diff = getDistance( $sLat,$sLng,$sLat1,$sLng1 );
if ( $diff < 5 )
{
$parkAll[$key][‘diff’] = $diff;
$arrData[] = $parkAll[$key];
}
}
return $arrData;
}
//封裝redis
namespace ParkApi\Model;
use Think\Model;
use Redis;
/**
-
phpredis擴展
-
by wbq 2012-5-22
*/
class RedisModel extends Model
{
//REDIS服務主機IP
private $_HOST = ‘127.0.0.1’;//redis服務端口
private $_PORT = 6379;//連接時長 默認為0 不限制時長
private $_TIMEOUT = 0;//數據庫名
private $_DBNAME = null;//連接類型 1普通連接 2長連接
private $_CTYPE = 1;//實例名
public $_REDIS = null;//事物對象
private $_TRANSCATION = null;//初始化
if (!isset($this->_REDIS)) {$this->_REDIS = new Redis();$this->connect($this->_HOST, $this->_PORT, $this->_TIMEOUT, $this->_DBNAME, $this->_CTYPE);}
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’);}
/**
- 連接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, KaTeX parse error: Expected 'EOF', got '}' at position 10: 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($key);
return $return;
}
/**
-
key值自增或者自減
-
@param $key string key名
-
@param $type int 0:自減 1:自增 默認為1
-
@param $n int 自增步長 默認為1
*/
public function deinc($key,$type=1,$n=1)
{
$return = null;
$n = (int)$n;switch ($type) {
case 0:
if ($n == 1) $return = $this->_REDIS->decr($key);
else if ($n > 1) $return = $this->_REDIS->decrBy($key, $n);
break;
case 1:
if ($n == 1) $return = $this->_REDIS->incr($key);
else if ($n > 1) $return = $this->_REDIS->incrBy($key, $n);
break;
default:
$return = false;
break;
}return $return;
}
/**
-
同時給多個key賦值
-
@param $data array key值數組 array(‘key0’=>‘value0’,‘key1’=>‘value1’)
*/
public function mset($data)
{
$return = null;$return = $this->_REDIS->mset($data);
return $return;
}
/**
-
查詢某個key的生存時間
-
@param $key string 要查詢的key名
*/
public function ttl($key)
{
$return = null;$return = $this->_REDIS->ttl($key);
return $return;
}
/**
-
刪除到期的key
-
@param $key string key名
*/
public function persist($key)
{
$return = null;$return = $this->_REDIS->persist($key);
return $return;
}
/**
-
獲取某一key的value
-
@param $key string key名
*/
public function strlen($key)
{
$return = null;$return = $this->_REDIS->strlen($key);
return $return;
}
//++±------------------------隊列操作-------------------------+++//
/**
-
入隊列
-
@param $list string 隊列名
-
@param $value mixed 入隊元素值
-
@param $deriction int 0:數據入隊列頭(左) 1:數據入隊列尾(右) 默認為0
-
@param $repeat int 判斷value是否存在 0:不判斷存在 1:判斷存在 如果value存在則不入隊列
*/
public function listPush($list,$value,$direction=0,$repeat=0)
{
$return = null;switch ($direction) {
case 0:
if ($repeat)
$return = $this->_REDIS->lPushx( l i s t , list, list,value);
else
$return = $this->_REDIS->lPush( l i s t , list, list,value);
break;
case 1:
if ($repeat)
$return = $this->_REDIS->rPushx( l i s t , list, list,value);
else
$return = $this->_REDIS->rPush( l i s t , list, list,value);
break;
default:
$return = false;
break;
}return $return;
}
/**
- 出隊列
- @param $list1 string 隊列名
- @param $deriction int 0:數據入隊列頭(左) 1:數據入隊列尾(右) 默認為0
- @param $list2 string 第二個隊列名 默認null
- @param $timeout int timeout為0:只獲取list1隊列的數據
- timeout>0:如果隊列list1為空 則等待timeout秒 如果還是未獲取到數據 則對list2隊列執行pop操作
*/
switch ($direction) {case 0:if ($timeout && $list2)$return = $this->_REDIS->blPop($list1,$list2,$timeout);else$return = $this->_REDIS->lPop($list1);break;case 1:if ($timeout && $list2)$return = $this->_REDIS->brPop($list1,$list2,$timeout);else$return = $this->_REDIS->rPop($list1);break;default:$return = false;break;}return $return;
public function listPop($list1,$deriction=0,$list2=null,$timeout=0)
{
$return = null;}
/**
-
獲取隊列中元素數
-
@param $list string 隊列名
*/
public function listSize($list)
{
$return = null;$return = $this->_REDIS->lSize($list);
return $return;
}
/**
-
為list隊列的index位置的元素賦值
-
@param $list string 隊列名
-
@param $index int 隊列元素位置
-
@param $value mixed 元素值
*/
public function listSet($list,$index=0,$value=null)
{
$return = null;$return = $this->_REDIS->lSet($list, $index, $value);
return $return;
}
/**
-
獲取list隊列的index位置的元素值
-
@param $list string 隊列名
-
@param $index int 隊列元素開始位置 默認0
-
@param $end int 隊列元素結束位置 $index=0,$end=-1:返回隊列所有元素
*/
public function listGet($list,$index=0,$end=null)
{
$return = null;if ($end) {
$return = $this->_REDIS->lRange($list, $index, $end);
} else {
$return = $this->_REDIS->lGet($list, $index);
}return $return;
}
/**
-
截取list隊列,保留start至end之間的元素
-
@param $list string 隊列名
-
@param $start int 開始位置
-
@param $end int 結束位置
*/
public function listTrim($list,$start=0,$end=-1)
{
$return = null;$return = $this->_REDIS->lTrim($list, $start, $end);
return $return;
}
/**
-
刪除list隊列中count個值為value的元素
-
@param $list string 隊列名
-
@param $value int 元素值
-
@param $count int 刪除個數 0:刪除所有 >0:從頭部開始刪除 <0:從尾部開始刪除 默認為0刪除所有
*/
public function listRemove($list,$value,$count=0)
{
$return = null;$return = $this->_REDIS->lRem($list, $value, $count);
return $return;
}
/**
-
在list中值為$value1的元素前Redis::BEFORE或者后Redis::AFTER插入值為$value2的元素
-
如果list不存在,不會插入,如果$value1不存在,return -1
-
@param $list string 隊列名
-
@param $location int 插入位置 0:之前 1:之后
-
@param $value1 mixed 要查找的元素值
-
@param $value2 mixed 要插入的元素值
*/
public function listInsert($list,$location=0,$value1,$value2)
{
$return = null;switch ($location) {
case 0:
$return = $this->_REDIS->lInsert($list, Redis::BEFORE, $value1, $value2);
break;
case 1:
$return = $this->_REDIS->lInsert($list, Redis::AFTER, $value1, $value2);
break;
default:
$return = false;
break;
}return $return;
}
/**
-
pop出list1的尾部元素并將該元素push入list2的頭部
-
@param $list1 string 隊列名
-
@param $list2 string 隊列名
*/
public function rpoplpush($list1, $list2)
{
$return = null;$return = $this->_REDIS->rpoplpush($list1, $list2);
return $return;
}
//++±------------------------集合操作-------------------------+++//
/**
-
將value寫入set集合 如果value存在 不寫入 返回false
-
如果是有序集合則根據score值更新該元素的順序
-
@param $set string 集合名
-
@param $value mixed 值
-
@param $stype int 集合類型 0:無序集合 1:有序集和 默認0
-
@param $score int 元素排序值
*/
public function setAdd($set,$value=null,$stype=0,$score=null)
{
$return = null;if ($stype && $score !== null) {
$return = $this->_REDIS->zAdd($set, $score, $value);
} else {
$return = $this->_REDIS->sAdd($set, $value);
}return $return;
}
/**
-
移除set1中的value元素 如果指定了set2 則將該元素寫入set2
-
@param $set1 string 集合名
-
@param $value mixed 值
-
@param $stype int 集合類型 0:無序集合 1:有序集和 默認0
-
@param $set2 string 集合名
*/
public function setMove($set1, $value=null, $stype=0, $set2=null)
{
$return = null;if ($set2) {
$return = $this->_REDIS->sMove($set1, $set2, $value);
} else {
if ($stype) $return = $this->_REDIS->zRem($set1, $value);
else $return = $this->_REDIS->sRem($set1, $value);
}return $return;
}
/**
-
查詢set中是否有value元素
-
@param $set string 集合名
-
@param $value mixed 值
*/
public function setSearch($set, $value=null)
{
$return = null;$return = $this->_REDIS->sIsMember($set, $value);
return $return;
}
/**
-
返回set中所有元素個數 有序集合要指定$stype=1
-
如果是有序集合并指定了$start和$end 則返回score在start跟end之間的元素個數
-
@param $set string 集合名
-
@param $stype int 集合類型 0:無序集合 1:有序集和 默認0
-
@param $start int 開始index
-
@param $end int 結束index
*/
public function setSize($set,$stype=0,$start=0,$end=0)
{
$return = null;if ($stype) {
if ($start && $end) $return = $this->_REDIS->zCount($set, $start, $end);
else $return = $this->_REDIS->zSize($set);
} else {
$return = $this->_REDIS->sSize($set);
}return $return;
}
/**
-
隨機返回set中一個元素并可選是否刪除該元素
-
@param $set string 集合名
-
@param $isdel int 是否刪除該元素 0:不刪除 1:刪除 默認為0
*/
public function setPop($set,$isdel=0)
{
$return = null;if ($isdel) {
$return = $this->_REDIS->sPop($set);
} else {
$return = $this->_REDIS->sRandMember($set);
}return $return;
}
/**
-
求交集 并可選是否將交集保存到新集合
-
@param $set array 集合名數組
-
@param $newset string 要保存到的集合名 默認為null 即不保存交集到新集合
-
@param $stype int 集合類型 0:無序集合 1:有序集和 默認0
-
@param $weight array 權重 執行function操作時要指定的每個集合的相同元素所占的權重 默認1
-
@param $function string 不同集合的相同元素的取值規則函數 SUM:取元素值的和 MAX:取最大值元素 MIN:取最小值元素
*/
public function setInter($set,$newset=null,$stype=0,$weight=array(1),$function=‘SUM’)
{
$return = array();if (is_array($set) && !empty($set)) {
if ($newset) {
if ($stype) $return = $this->_REDIS->zInter($newset, $set, $weight, $function);
else $return = $this->_REDIS->sInterStore($newset, $set);
} else {
$return = $this->_REDIS->sInter($set);
}
}return $return;
}
/**
-
求并集 并可選是否將交集保存到新集合
-
@param $set array 集合名數組
-
@param $newset string 要保存到的集合名 默認為null 即不保存交集到新集合
-
@param $stype int 集合類型 0:無序集合 1:有序集和 默認0
-
@param $weight array 權重 執行function操作時要指定的每個集合的相同元素所占的權重 默認1
-
@param $function string 不同集合的相同元素的取值規則函數 SUM:取元素值的和 MAX:取最大值元素 MIN:取最小值元素
*/
public function setUnion($set,$newset=null,$stype=0,$weight=array(1),$function=‘SUM’)
{
$return = array();if (is_array($set) && !empty($set)) {
if ($newset) {
if ($stype) $return = $this->_REDIS->zUnion($newset, $set, $weight, $function);
else $return = $this->_REDIS->sUnionStore($newset, $set);
} else {
$return = $this->_REDIS->sUnion($set);
}
}return $return;
}
/**
-
求差集 并可選是否將交集保存到新集合
-
@param $set array 集合名數組
-
@param $newset string 要保存到的集合名 默認為null 即不保存交集到新集合
*/
public function setDiff($set,$newset=null)
{
$return = array();if (is_array($set) && !empty($set)) {
if ($newset) {
$return = $this->_REDIS->sDiffStore($newset, $set);
} else {
$return = $this->_REDIS->sDiff($set);
}
}return $return;
}
/**
-
返回set中所有元素
-
@param $set string 集合名
*/
public function setMembers($set)
{
$return = null;$return = $this->_REDIS->sMembers($set);
return $return;
}
/**
-
排序 分頁等
-
@param $set string 集合名
-
@param $option array 選項
/
public function setSort($set,$option)
{
$return = null;
$default_option = array(
‘by’ => 'some_pattern_’, //要匹配的排序value值
‘limit’ => array(0, 1), //array(start,length)
‘get’ => ‘some_other_pattern_’, //多個匹配格式:array('some_other_pattern1_’,‘some_other_pattern2_’)
‘sort’ => ‘asc’, // asc|desc 默認asc
‘alpha’ => TRUE, //
‘store’ => 'some_need_pattern_’ //永久性排序值
);o p t i o n = a r r a y m e r g e ( option = array_merge( option=arraym?erge(default_option, $option);
r e t u r n = / / return = // return=//this->_REDIS->sort($set, $option);
return /$return;
}
//++±------------------------有序集合操作-------------------------+++//
/**
-
***只針對有序集合操作
-
返回set中index從start到end的所有元素
-
@param $set string 集合名
-
@param $start int 開始Index
-
@param $end int 結束Index
-
@param $order int 排序方式 0:從小到大排序 1:從大到小排序 默認0
-
@param $score bool 元素排序值 \false:返回數據不帶score true:返回數據帶score 默認false
*/
public function setRange($set,$start,$end,$order=0,$score=false)
{
$return = null;if ($order) {
$return = $this->_REDIS->zRevRange($set, $start, $end, $score);
} else {
$return = $this->_REDIS->zRange($set, $start, $end, $score);
}return $return;
}
/**
-
***只針對有序集合操作
-
刪除set中score從start到end的所有元素
-
@param $set string 集合名
-
@param $start int 開始score
-
@param $end int 結束score
*/
public function setDeleteRange($set,$start,$end)
{
$return = null;$return = $this->_REDIS->zRemRangeByScore($set, $start, $end);
return $return;
}
/**
-
***只針對有序集合操作
-
獲取set中某個元素的score
-
如果指定了inc參數 則給該元素的score增加inc值
-
如果沒有該元素 則將該元素寫入集合
-
@param $set string 集合名
-
@param $value mixed 元素值
-
@param $inc int 要給score增加的數值 默認是null 不執行score增加操作
*/
public function setScore($set,$value,$inc=null)
{
$return = null;if ($inc) {
$return = $this->_REDIS->zIncrBy($set, $inc, $value);
} else {
$return = $this->_REDIS->zScore($set, $value);
}return $return;
}
//++±------------------------哈希操作-------------------------+++//
/**
-
將key->value寫入hash表中
-
@param $hash string 哈希表名
-
@param $data array 要寫入的數據 array(‘key’=>‘value’)
*/
public function hashSet($hash,$data)
{
$return = null;if (is_array($data) && !empty($data)) {
$return = $this->_REDIS->hMset($hash, $data);
}return $return;
}
/**
-
獲取hash表的數據
-
@param $hash string 哈希表名
-
@param $key mixed 表中要存儲的key名 默認為null 返回所有key>value
-
@param $type int 要獲取的數據類型 0:返回所有key 1:返回所有value 2:返回所有key->value
*/
public function hashGet($hash,$key=array(),$type=0)
{
$return = null;if ($key) {
if (is_array(KaTeX parse error: Expected 'EOF', got '&' at position 6: key) &?& !empty(key))
$return = $this->_REDIS->hMGet($hash,$key);
else
$return = $this->_REDIS->hGet($hash,$key);
} else {
switch ($type) {
case 0:
$return = $this->_REDIS->hKeys($hash);
break;
case 1:
$return = $this->_REDIS->hVals($hash);
break;
case 2:
$return = $this->_REDIS->hGetAll($hash);
break;
default:
$return = false;
break;
}
}return $return;
}
/**
-
獲取hash表中元素個數
-
@param $hash string 哈希表名
*/
public function hashLen($hash)
{
$return = null;$return = $this->_REDIS->hLen($hash);
return $return;
}
/**
-
刪除hash表中的key
-
@param $hash string 哈希表名
-
@param $key mixed 表中存儲的key名
*/
public function hashDel($hash,$key)
{
$return = null;$return = $this->_REDIS->hDel($hash,$key);
return $return;
}
/**
-
查詢hash表中某個key是否存在
-
@param $hash string 哈希表名
-
@param $key mixed 表中存儲的key名
*/
public function hashExists($hash,$key)
{
$return = null;$return = $this->_REDIS->hExists( h a s h , hash, hash,key);
return $return;
}
/**
-
自增hash表中某個key的值
-
@param $hash string 哈希表名
-
@param $key mixed 表中存儲的key名
-
@param $inc int 要增加的值
*/
public function hashInc($hash,$key,$inc)
{
$return = null;$return = $this->_REDIS->hIncrBy($hash, $key, $inc);
return $return;
}
//++±------------------------其他操作-------------------------+++//
/**
-
自增hash表中某個key的值
-
@param $key string 哈希表名
-
@param $time mixed 表中存儲的key名
*/
public function setKeyExpire($key, $time)
{
$return = null;$return = $this->_REDIS->setTimeout($key, $time);
return $return;
}
/**
-
獲取滿足給定pattern的所有key
-
@param $key regexp key匹配表達式 模式:user* 匹配以user開始的key
*/
public function getKeys($key=null)
{
$return = null;$return = $this->_REDIS->keys($key);
return $return;
}
/**
-
將數據保存到硬盤 同步/異步
-
@param $type int 保存方式 0:同步 1:異步 默認0
-
@param $time int 是否要獲取上次成功將數據保存到磁盤的Unix時戳 0:不返回時間 1:返回時間
*/
public function hwSave($type=0,$time=0)
{
$return = null;if ($type) {
$return = $this->_REDIS->bgsave();
} else {
$return = $this->_REDIS->save();
}
if ($time) {
$return = $this->_REDIS->lastSave();
}return $return;
}
/**
-
獲取上次成功將數據保存到磁盤的Unix時戳
*/
public function lastSave()
{
$return = null;$return = $this->_REDIS->lastSave();
return $return;
}
/**
-
獲取redis版本信息等詳情
*/
public function info()
{
$return = null;$return = $this->_REDIS->info();
return $return;
}
/**
-
獲取數據庫中key的數目
*/
public function dbSize()
{
$return = null;$return = $this->_REDIS->dbSize();
return $return;
}
//++±------------------------事務操作-------------------------+++//
/**
- 開始進入事務操作
- @param $return object 事務對象
*/
public function tranStart()
{
$this->_TRANSCATION = $this->_REDIS->multi();
}
/**
- 提交完成事務
- @param $return bool 事務執行成功 提交操作
*/
public function tranCommit()
{
return $this->_TRANSCATION->exec();
}
/**
- 回滾事務
- @param $return bool 事務執行失敗 回滾操作
*/
public function tranRollback()
{
return $this->_TRANSCATION->discard();
}
}
- 連接redis服務器
總結
以上是生活随笔為你收集整理的redis 哈希hash实例应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杀戮尖塔(Slay the Spire)
- 下一篇: 入门级经验:学三维建模从哪个软件开始学起