thinkphp 使用redis 整理(二)


參考手冊   http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html

redis  幾種數據類型選擇,參考 :

    https://blog.csdn.net/xlgen157387/article/details/60958657

  https://www.cnblogs.com/George1994/p/7191011.html

 

PHP + redis 類庫: https://www.cnblogs.com/whoamme/p/5379469.html

實際代碼中應用:

參考 : http://www.runoob.com/redis/redis-data-types.html 

整理如下:

數組 排序  array_multisort(array_column($list,'order_no'),SORT_DESC,$list); // 根據數組中某個字段 倒序排序

 

$findkey = $this->redis->Keys('product:news:'.$pro_id); // zset    或者 string

$this->redis->delete($findkey);

可直接 使用 $this->redis->del('product:news:1');   指定key 進行刪除 $this->redis->del('index_info');

刪除 所有detail :

    $ret = $this->redis->Keys('news:detail:*');

    $this->redis->delete($ret);

幾種常用數據類型

1.String  字符串類型, key value 的數據類型   (推薦/置頂  的幾條數據)

當查詢返回的數據$data是數組時,(單個值時,不需要json_encode … 處理)


 注意:兩數組合並時,避免null 影響合並,需轉換數據類型。

$data=array_merge((array)$pushdata,(array)$data);   
[rɪˈkɜ:sɪv]   遞歸的

array_merge_recursive() 與 array_merge() 函數的區別在於處理兩個或更多個數組元素有相同的鍵名時。array_merge_recursive() 不會進行鍵名覆蓋,而是將多個相同鍵名的值遞歸組成一個數組。

二維數組 選擇某個字段 作為key  鍵名: 出處 https://blog.csdn.net/m0_38030271/article/details/80660271
$newArr= array_column($array,NULL,'某個字段');


 ==========


 ==========

賦值:$this->redis->set(‘news:topdata‘,json_encode($data));

取值json_decode($this->redis->get(‘news: topdata’),true);  數據轉換成數組

2.Hash 哈希 類型,一個string 類型的field ,value 的映射表,適合 存儲對象(詳情頁detail時  利用該類型)

注意 :獲取多條數據時,hMget;    單條數據時,hGet,例如 上下篇:

Json_decode($this->redis->hGet(‘news:list’,$preno),true); // 上下篇

 

 ==========

 

 ========== 

刪除: $this->redis->del(‘news:list’);// 刪除 全部

移除某一條:$this->redis->hDel(‘news:list’, $id);  // 注意 hDel 需要傳遞兩個參數

賦值

  多條數據:$this->redis->hMset(‘news:list’,$data);// $data 即查詢返回的二維數組

  單條數據:$this->redis->hSet(‘news:list’, $id , json_encode($data) );

判斷news:list是否有數據 可使用:$this->redis->hLen(‘news:list’);

取值:$this->redis->hMget(‘news:list’,$ids);// 返回json 格式  

$news = jsonToArray($news);// 轉換成 數組

function jsonToArray($array)

{     

  return array_map('arrayMapHandler',$array);  

}

function arrayMapHandler($v){
  $res = json_decode($v,true);
  if($res || $v == '[]'){
    return $res;
  }else{
    return $v;
  }
}

 

 

詳情 detail

 ========== 

 

 ========== 

賦值:$this->redis->hMset(‘news:detail:’.$id,$detail);// …->field(‘…’)->find();

取值:$this->redis->hMget(‘news:detail:’.$id,[‘id’,’title’,’content’]);  //

  獲取 某個字段的值

  $data= $this->redis->hMGet('solution:detail:213',['status']);  echo $data['status'];

  $stu = $this->redis->hGet('solution:detail:213','status');  echo $stu;

判斷detail 是否存在: If(!$this->redis->exists(‘news:detail:’.$id)){// 不存在,此時需要記入redis}

3.ZSET(sorted set :有序集合) 類型,和set 一樣也是string類型元素的集合不同的是每個元素都會關聯一個double類型的分數。redis正是通過分數來為集合中的成員進行從小到大的排序。成員是唯一的,但分數 score 卻可以重復

 ========== 

 

 

 ========== 

刪除:$this->redis->del(‘news:id’);// 多條

移除某一條:$this->redis ->zRem(‘news:id’, $id);

賦值

  多條數據

    $ids = $model->…->getField(‘order_no,id’);// 返回以 order_no 為key 的數組

    $this->redis->zAddArray (‘news:id’, $ids);

 

  $arr = $model->...->field('id,name,sex,sort,....')->order('sort desc')->select();

  $data = [];

  foreach($arr as $key =>$val){

    $arr[$val['sort']] = $val; // 返回 以sort 為主鍵的數組

  }

   $this->redis->zAddArray("product:", $data );

 

 單條數據:$this->redis->zAdd(‘news:id’, $cur_orderno, $cur_id);

獲取個數 或者 判斷news:id 是否存在,可使用:$sum = $this->redis->zCard(‘news:id’);// zCard  返回有序集合的成員數

 

取值: 取新聞為例,分頁讀取新聞列表數據時

$page = I(‘get.page’,1,’intval’);

$row =10;

$start = ($page-1)*$row;

$end = $start +($row-1);

$ids = $this->redis->zRevRange(‘news:id’, $start , $end);// 返回 有序集中指定區間內成員,通過索引,分數從高到低

【$ids = $this->redis->zRevRange('news:id',0,-1)】 即返回全部

$news = $this->redis->hMget(‘news:list‘,   $ids);

$news =jsonToArray($news);

下篇處理

 

zRevRangeByScore :從高到低的分數排序 ,讀取范圍內的數據    http://blog.csdn.net/chwshuang/article/details/52834380

zRangeByScore : 讀取范圍內的數據

參數: 有序集合鍵名稱,  max ,min, [WITHSCORES] [LIMIT offset count]

+inf-inf分別表示Sorted-Sets中分數的最大值最小值  

$order_no = $this->redis->zScore('news:id',$id); // 返回名稱為key的zset中元素 $id 的score

$nextno = $this->redis->zRevRangeByScore('news:id', $order_no, '-inf', ['limit'=>[1,1]] ); // 下一篇    

$preno = $this->redis->zRangeByScore('news:id', $order_no, '+inf', ['limit'=>[1,1]] ); // 上一篇

zset   並集zUnion,交集 zInter  

 

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM