日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

redis 哈希hash实例应用

發(fā)布時(shí)間:2024/1/8 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 redis 哈希hash实例应用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

redis 哈希實(shí)例

因?yàn)楸扇司彺娣奖懵圆睢K耘ng

隨便拿一個(gè)業(yè)務(wù)場(chǎng)景 來(lái)使用hash
業(yè)務(wù)場(chǎng)景:我要把一個(gè)停車(chē)場(chǎng)列表放到redis中,再根據(jù)這個(gè)列表內(nèi)的數(shù)據(jù)計(jì)算距離不超過(guò)5公里的數(shù)據(jù),并返回這個(gè)停車(chē)場(chǎng)列表

//這里hash的存儲(chǔ)方法都是經(jīng)過(guò)封裝。下面有封裝的代碼
public funciton getParkList( $sLat,$slng,$id )
{
//由于redis我自己又封裝了一下使用的是tp框架,這里我實(shí)例化自己的redis類(lèi)
$redis = D(‘Redis’);
//先獲取存放在redis中的hash 停車(chē)場(chǎng)數(shù)據(jù)
$park= $redis->hashGet(‘park:’. i d , [ ] , 2 ) ; / / 判 斷 緩 存 中 是 否 有 數(shù) 據(jù) 如 果 沒(méi) 有 , 查 詢(xún) 數(shù) 據(jù) 庫(kù) , 在 存 在 r e d i s 中 i f ( e m p t y ( id,[],2); //判斷緩存中是否有數(shù)據(jù)如果沒(méi)有,查詢(xún)數(shù)據(jù)庫(kù),在存在redis中 if ( empty( id,[],2);//數(shù)據(jù)沒(méi)詢(xún)數(shù)據(jù)庫(kù)redisif(empty(park) )
{
//獲取所有的數(shù)據(jù)
$parkAll = D(‘parking’)->getParkAll();
//遍歷數(shù)據(jù),添加到緩存
foreach ( $parkAll as $key=>$value )
{
$redis->hashSet(‘park:’.$key,$value);
}
}
//處理數(shù)據(jù)
foreach ( $parkAll as $k=>$v )
{
//獲取相差的公里數(shù)
$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擴(kuò)展

  • by wbq 2012-5-22
    */
    class RedisModel extends Model
    {
    //REDIS服務(wù)主機(jī)IP
    private $_HOST = ‘127.0.0.1’;

    //redis服務(wù)端口
    private $_PORT = 6379;

    //連接時(shí)長(zhǎng) 默認(rèn)為0 不限制時(shí)長(zhǎng)
    private $_TIMEOUT = 0;

    //數(shù)據(jù)庫(kù)名
    private $_DBNAME = null;

    //連接類(lèi)型 1普通連接 2長(zhǎng)連接
    private $_CTYPE = 1;

    //實(shí)例名
    public $_REDIS = null;

    //事物對(duì)象
    private $_TRANSCATION = null;

    //初始化
    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’);

    if (!isset($this->_REDIS)) {$this->_REDIS = new Redis();$this->connect($this->_HOST, $this->_PORT, $this->_TIMEOUT, $this->_DBNAME, $this->_CTYPE);}

    }

    /**

    • 連接redis服務(wù)器
      */
      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連接是否斷開(kāi)

    • @return $return bool true:連接未斷開(kāi) false:連接已斷開(kāi)
      */
      public function ping()
      {
      $return = null;

      $return = $this->_REDIS->ping();

      return ‘PONG’ ? true : false;
      }

    /**

    • 設(shè)置redis模式參數(shù)
    • @param $option array 參數(shù)數(shù)組鍵值對(duì)
    • @return $return true/false
      */
      public function setOption($option=array())
      {
      return $this->_REDIS->setOption();
      }

    /**

    • 獲取redis模式參數(shù)
    • @param $option array 要獲取的參數(shù)數(shù)組
      */
      public function getOption($option=array())
      {
      return $this->_REDIS->getOption();
      }

    /**

    • 寫(xiě)入key-value

    • @param $key string 要存儲(chǔ)的key名

    • @param $value mixed 要存儲(chǔ)的值

    • @param $type int 寫(xiě)入方式 0:不添加到現(xiàn)有值后面 1:添加到現(xiàn)有值的后面 默認(rèn)0

    • @param $repeat int 0:不判斷重復(fù) 1:判斷重復(fù)

    • @param $time float 過(guò)期時(shí)間(S)

    • @param $old int 1:返回舊的value 默認(rèn)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;
      }

    /**

    • 獲取某個(gè)key值 如果指定了start end 則返回key值的start跟end之間的字符

    • @param $key string/array 要獲取的key或者key數(shù)組

    • @param $start int 字符串開(kāi)始index

    • @param $end int 字符串結(jié)束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;
      }

    /**

    • 刪除某個(gè)key值

    • @param $key array key數(shù)組

    • @return $return longint 刪除成功的key的個(gè)數(shù)
      */
      public function delete($key=array())
      {
      $return = null;

      $return = $this->_REDIS->delete($key);

      return $return;
      }

    /**

    • 判斷某個(gè)key是否存在

    • @param $key string 要查詢(xún)的key名
      */
      public function exists($key)
      {
      $return = null;

      $return = $this->_REDIS->exists($key);

      return $return;
      }

    /**

    • key值自增或者自減

    • @param $key string key名

    • @param $type int 0:自減 1:自增 默認(rèn)為1

    • @param $n int 自增步長(zhǎng) 默認(rèn)為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;
      }

    /**

    • 同時(shí)給多個(gè)key賦值

    • @param $data array key值數(shù)組 array(‘key0’=>‘value0’,‘key1’=>‘value1’)
      */
      public function mset($data)
      {
      $return = null;

      $return = $this->_REDIS->mset($data);

      return $return;
      }

    /**

    • 查詢(xún)某個(gè)key的生存時(shí)間

    • @param $key string 要查詢(xún)的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;
      }

    //++±------------------------隊(duì)列操作-------------------------+++//

    /**

    • 入隊(duì)列

    • @param $list string 隊(duì)列名

    • @param $value mixed 入隊(duì)元素值

    • @param $deriction int 0:數(shù)據(jù)入隊(duì)列頭(左) 1:數(shù)據(jù)入隊(duì)列尾(右) 默認(rèn)為0

    • @param $repeat int 判斷value是否存在 0:不判斷存在 1:判斷存在 如果value存在則不入隊(duì)列
      */
      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;
      }

    /**

    • 出隊(duì)列
    • @param $list1 string 隊(duì)列名
    • @param $deriction int 0:數(shù)據(jù)入隊(duì)列頭(左) 1:數(shù)據(jù)入隊(duì)列尾(右) 默認(rèn)為0
    • @param $list2 string 第二個(gè)隊(duì)列名 默認(rèn)null
    • @param $timeout int timeout為0:只獲取list1隊(duì)列的數(shù)據(jù)
    • timeout>0:如果隊(duì)列l(wèi)ist1為空 則等待timeout秒 如果還是未獲取到數(shù)據(jù) 則對(duì)list2隊(duì)列執(zhí)行pop操作

    */
    public function listPop($list1,$deriction=0,$list2=null,$timeout=0)
    {
    $return = null;

    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;

    }

    /**

    • 獲取隊(duì)列中元素?cái)?shù)

    • @param $list string 隊(duì)列名
      */
      public function listSize($list)
      {
      $return = null;

      $return = $this->_REDIS->lSize($list);

      return $return;
      }

    /**

    • 為list隊(duì)列的index位置的元素賦值

    • @param $list string 隊(duì)列名

    • @param $index int 隊(duì)列元素位置

    • @param $value mixed 元素值
      */
      public function listSet($list,$index=0,$value=null)
      {
      $return = null;

      $return = $this->_REDIS->lSet($list, $index, $value);

      return $return;
      }

    /**

    • 獲取list隊(duì)列的index位置的元素值

    • @param $list string 隊(duì)列名

    • @param $index int 隊(duì)列元素開(kāi)始位置 默認(rèn)0

    • @param $end int 隊(duì)列元素結(jié)束位置 $index=0,$end=-1:返回隊(duì)列所有元素
      */
      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隊(duì)列,保留start至end之間的元素

    • @param $list string 隊(duì)列名

    • @param $start int 開(kāi)始位置

    • @param $end int 結(jié)束位置
      */
      public function listTrim($list,$start=0,$end=-1)
      {
      $return = null;

      $return = $this->_REDIS->lTrim($list, $start, $end);

      return $return;
      }

    /**

    • 刪除list隊(duì)列中count個(gè)值為value的元素

    • @param $list string 隊(duì)列名

    • @param $value int 元素值

    • @param $count int 刪除個(gè)數(shù) 0:刪除所有 >0:從頭部開(kāi)始刪除 <0:從尾部開(kāi)始刪除 默認(rèn)為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不存在,不會(huì)插入,如果$value1不存在,return -1

    • @param $list string 隊(duì)列名

    • @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 隊(duì)列名

    • @param $list2 string 隊(duì)列名
      */
      public function rpoplpush($list1, $list2)
      {
      $return = null;

      $return = $this->_REDIS->rpoplpush($list1, $list2);

      return $return;
      }

    //++±------------------------集合操作-------------------------+++//

    /**

    • 將value寫(xiě)入set集合 如果value存在 不寫(xiě)入 返回false

    • 如果是有序集合則根據(jù)score值更新該元素的順序

    • @param $set string 集合名

    • @param $value mixed 值

    • @param $stype int 集合類(lèi)型 0:無(wú)序集合 1:有序集和 默認(rèn)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 則將該元素寫(xiě)入set2

    • @param $set1 string 集合名

    • @param $value mixed 值

    • @param $stype int 集合類(lèi)型 0:無(wú)序集合 1:有序集和 默認(rèn)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;
      }

    /**

    • 查詢(xún)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中所有元素個(gè)數(shù) 有序集合要指定$stype=1

    • 如果是有序集合并指定了$start和$end 則返回score在start跟end之間的元素個(gè)數(shù)

    • @param $set string 集合名

    • @param $stype int 集合類(lèi)型 0:無(wú)序集合 1:有序集和 默認(rèn)0

    • @param $start int 開(kāi)始index

    • @param $end int 結(jié)束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;
      }

    /**

    • 隨機(jī)返回set中一個(gè)元素并可選是否刪除該元素

    • @param $set string 集合名

    • @param $isdel int 是否刪除該元素 0:不刪除 1:刪除 默認(rèn)為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 集合名數(shù)組

    • @param $newset string 要保存到的集合名 默認(rèn)為null 即不保存交集到新集合

    • @param $stype int 集合類(lèi)型 0:無(wú)序集合 1:有序集和 默認(rèn)0

    • @param $weight array 權(quán)重 執(zhí)行function操作時(shí)要指定的每個(gè)集合的相同元素所占的權(quán)重 默認(rèn)1

    • @param $function string 不同集合的相同元素的取值規(guī)則函數(shù) 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 集合名數(shù)組

    • @param $newset string 要保存到的集合名 默認(rèn)為null 即不保存交集到新集合

    • @param $stype int 集合類(lèi)型 0:無(wú)序集合 1:有序集和 默認(rèn)0

    • @param $weight array 權(quán)重 執(zhí)行function操作時(shí)要指定的每個(gè)集合的相同元素所占的權(quán)重 默認(rèn)1

    • @param $function string 不同集合的相同元素的取值規(guī)則函數(shù) 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 集合名數(shù)組

    • @param $newset string 要保存到的集合名 默認(rèn)為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;
      }

    /**

    • 排序 分頁(yè)等

    • @param $set string 集合名

    • @param $option array 選項(xiàng)
      /
      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_’, //多個(gè)匹配格式:array('some_other_pattern1_’,‘some_other_pattern2_’)
      ‘sort’ => ‘a(chǎn)sc’, // asc|desc 默認(rèn)asc
      ‘a(chǎn)lpha’ => 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;
      }

    //++±------------------------有序集合操作-------------------------+++//

    /**

    • ***只針對(duì)有序集合操作

    • 返回set中index從start到end的所有元素

    • @param $set string 集合名

    • @param $start int 開(kāi)始Index

    • @param $end int 結(jié)束Index

    • @param $order int 排序方式 0:從小到大排序 1:從大到小排序 默認(rèn)0

    • @param $score bool 元素排序值 \false:返回?cái)?shù)據(jù)不帶score true:返回?cái)?shù)據(jù)帶score 默認(rèn)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;
      }

    /**

    • ***只針對(duì)有序集合操作

    • 刪除set中score從start到end的所有元素

    • @param $set string 集合名

    • @param $start int 開(kāi)始score

    • @param $end int 結(jié)束score
      */
      public function setDeleteRange($set,$start,$end)
      {
      $return = null;

      $return = $this->_REDIS->zRemRangeByScore($set, $start, $end);

      return $return;
      }

    /**

    • ***只針對(duì)有序集合操作

    • 獲取set中某個(gè)元素的score

    • 如果指定了inc參數(shù) 則給該元素的score增加inc值

    • 如果沒(méi)有該元素 則將該元素寫(xiě)入集合

    • @param $set string 集合名

    • @param $value mixed 元素值

    • @param $inc int 要給score增加的數(shù)值 默認(rèn)是null 不執(zhí)行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寫(xiě)入hash表中

    • @param $hash string 哈希表名

    • @param $data array 要寫(xiě)入的數(shù)據(jù) 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表的數(shù)據(jù)

    • @param $hash string 哈希表名

    • @param $key mixed 表中要存儲(chǔ)的key名 默認(rèn)為null 返回所有key>value

    • @param $type int 要獲取的數(shù)據(jù)類(lèi)型 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表中元素個(gè)數(shù)

    • @param $hash string 哈希表名
      */
      public function hashLen($hash)
      {
      $return = null;

      $return = $this->_REDIS->hLen($hash);

      return $return;
      }

    /**

    • 刪除hash表中的key

    • @param $hash string 哈希表名

    • @param $key mixed 表中存儲(chǔ)的key名
      */
      public function hashDel($hash,$key)
      {
      $return = null;

      $return = $this->_REDIS->hDel($hash,$key);

      return $return;
      }

    /**

    • 查詢(xún)hash表中某個(gè)key是否存在

    • @param $hash string 哈希表名

    • @param $key mixed 表中存儲(chǔ)的key名
      */
      public function hashExists($hash,$key)
      {
      $return = null;

      $return = $this->_REDIS->hExists( h a s h , hash, hash,key);

      return $return;
      }

    /**

    • 自增hash表中某個(gè)key的值

    • @param $hash string 哈希表名

    • @param $key mixed 表中存儲(chǔ)的key名

    • @param $inc int 要增加的值
      */
      public function hashInc($hash,$key,$inc)
      {
      $return = null;

      $return = $this->_REDIS->hIncrBy($hash, $key, $inc);

      return $return;
      }

    //++±------------------------其他操作-------------------------+++//

    /**

    • 自增hash表中某個(gè)key的值

    • @param $key string 哈希表名

    • @param $time mixed 表中存儲(chǔ)的key名
      */
      public function setKeyExpire($key, $time)
      {
      $return = null;

      $return = $this->_REDIS->setTimeout($key, $time);

      return $return;
      }

    /**

    • 獲取滿(mǎn)足給定pattern的所有key

    • @param $key regexp key匹配表達(dá)式 模式:user* 匹配以u(píng)ser開(kāi)始的key
      */
      public function getKeys($key=null)
      {
      $return = null;

      $return = $this->_REDIS->keys($key);

      return $return;
      }

    /**

    • 將數(shù)據(jù)保存到硬盤(pán) 同步/異步

    • @param $type int 保存方式 0:同步 1:異步 默認(rèn)0

    • @param $time int 是否要獲取上次成功將數(shù)據(jù)保存到磁盤(pán)的Unix時(shí)戳 0:不返回時(shí)間 1:返回時(shí)間
      */
      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;
      }

    /**

    • 獲取上次成功將數(shù)據(jù)保存到磁盤(pán)的Unix時(shí)戳
      */
      public function lastSave()
      {
      $return = null;

      $return = $this->_REDIS->lastSave();

      return $return;
      }

    /**

    • 獲取redis版本信息等詳情
      */
      public function info()
      {
      $return = null;

      $return = $this->_REDIS->info();

      return $return;
      }

    /**

    • 獲取數(shù)據(jù)庫(kù)中key的數(shù)目
      */
      public function dbSize()
      {
      $return = null;

      $return = $this->_REDIS->dbSize();

      return $return;
      }

    //++±------------------------事務(wù)操作-------------------------+++//

    /**

    • 開(kāi)始進(jìn)入事務(wù)操作
    • @param $return object 事務(wù)對(duì)象
      */
      public function tranStart()
      {
      $this->_TRANSCATION = $this->_REDIS->multi();
      }

    /**

    • 提交完成事務(wù)
    • @param $return bool 事務(wù)執(zhí)行成功 提交操作
      */
      public function tranCommit()
      {
      return $this->_TRANSCATION->exec();
      }

    /**

    • 回滾事務(wù)
    • @param $return bool 事務(wù)執(zhí)行失敗 回滾操作
      */
      public function tranRollback()
      {
      return $this->_TRANSCATION->discard();
      }
      }

總結(jié)

以上是生活随笔為你收集整理的redis 哈希hash实例应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。