欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP操作redis實(shí)現(xiàn)的分頁(yè)列表,新增,刪除功能封裝類(lèi)與用法示例

 更新時(shí)間:2018年08月04日 11:20:43   作者:燈火cj闌珊  
這篇文章主要介紹了PHP操作redis實(shí)現(xiàn)的分頁(yè)列表,新增,刪除功能封裝類(lèi)與用法,結(jié)合實(shí)例形式分析了php針對(duì)redis數(shù)據(jù)庫(kù)基本的連接、查詢(xún)、添加、分頁(yè)等操作封裝與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP操作redis實(shí)現(xiàn)的分頁(yè)列表,新增,刪除功能封裝類(lèi)與用法。分享給大家供大家參考,具體如下:

<?php
/*
 * redis 分頁(yè)數(shù)據(jù)類(lèi)庫(kù)
 */
class redisPage{
  protected $_redis;
  protected $_redis_ip = '127.0.0.1'; //ip
  protected $_redis_port = 6379; //端口
  protected $_redis_db = 0; //數(shù)據(jù)庫(kù)號(hào)
  protected $_hash_prefix = 'my_data'; //前綴名稱(chēng)
  public function __construct($ip='',$port='',$db='',$hash_prefix=''){
    if($ip != '') $this->_redis_ip = $ip;
    if($port != '') $this->_redis_port = $port;
    if($db != '') $this->_redis_db = $db;
    if($hash_prefix != '') $this->_hash_prefix = $hash_prefix;
    $this->_redis = new Redis();
    $this->_redis->connect($this->_redis_ip, $this->_redis_port);
    $this->_redis->select($this->_redis_db);
  }
  /*
   * 添加記錄
   * @param $id id
   * @param $data hash數(shù)據(jù)
   * @param $hashName Hash 記錄名稱(chēng)
   * @param $SortName Redis SortSet 記錄名稱(chēng)
   * @param $redis Redis 對(duì)象
   * @return bool
   */
  public function set_redis_page_info($id,$data){
    if(!is_numeric($id) || !is_array($data)) return false;
    $hashName = $this->_hash_prefix.'_'.$id;
    $this->_redis->hMset($hashName, $data);
    $this->_redis->zAdd($this->_hash_prefix.'_sort',$id,$id);
    return true;
  }
  /*
   * 獲取分頁(yè)數(shù)據(jù)
   * @param $page 當(dāng)前頁(yè)數(shù)
   * @param $pageSize 每頁(yè)多少條
   * @param $hashName Hash 記錄名稱(chēng)
   * @param $SortName Redis SortSet 記錄名稱(chēng)
   * @param $redis Redis 對(duì)象
   * @param $key 字段數(shù)組 不傳為取出全部字段
   * @return array
   */
  public function get_redis_page_info($page,$pageSize,$key=array()){
    if(!is_numeric($page) || !is_numeric($pageSize)) return false;
    $limit_s = ($page-1) * $pageSize;
    $limit_e = ($limit_s + $pageSize) - 1;
    $range = $this->_redis->ZRANGE($this->_hash_prefix.'_sort',$limit_s,$limit_e); //指定區(qū)間內(nèi),帶有 score 值(可選)的有序集成員的列表。
    $count = $this->_redis->zCard($this->_hash_prefix.'_sort'); //統(tǒng)計(jì)ScoreSet總數(shù)
    $pageCount = ceil($count/$pageSize); //總共多少頁(yè)
    $pageList = array();
    foreach($range as $qid){
      if(count($key) > 0){
        $pageList[] = $this->_redis->hMGet($this->_hash_prefix.'_'.$qid,$key); //獲取hash表中所有的數(shù)據(jù)
      }else{
        $pageList[] = $this->_redis->hGetAll($this->_hash_prefix.'_'.$qid); //獲取hash表中所有的數(shù)據(jù)
      }
    }
    $data = array(
      'data'=>$pageList, //需求數(shù)據(jù)
      'page'=>array(
        'page'=>$page, //當(dāng)前頁(yè)數(shù)
        'pageSize'=>$pageSize, //每頁(yè)多少條
        'count'=>$count, //記錄總數(shù)
        'pageCount'=>$pageCount //總頁(yè)數(shù)
      )
    );
    return $data;
  }
  /*
   * 刪除記錄
   * @param $id id
   * @param $hashName Hash 記錄名稱(chēng)
   * @param $SortName Redis SortSet 記錄名稱(chēng)
   * @param $redis Redis 對(duì)象
   * @return bool
   */
  public function del_redis_page_info($id){
    if(!is_array($id)) return false;
    foreach($id as $value){
      $hashName = $this->_hash_prefix.'_'.$value;
      $this->_redis->del($hashName);
      $this->_redis->zRem($this->_hash_prefix.'_sort',$value);
    }
    return true;
  }
  /*
   * 清空數(shù)據(jù)
   * @param string $type db:清空當(dāng)前數(shù)據(jù)庫(kù) all:清空所有數(shù)據(jù)庫(kù)
   * @return bool
   */
  public function clear($type='db'){
    if($type == 'db'){
      $this->_redis->flushDB();
    }elseif($type == 'all'){
      $this->_redis->flushAll();
    }else{
      return false;
    }
    return true;
  }
}
//數(shù)據(jù)庫(kù)
$host='localhost';
$user='root';
$psd='';
$dbname='china';
$link = @mysql_connect($host,$user,$psd);
mysql_select_db($dbname,$link);
mysql_query("set names utf8");
$SQL = "SELECT * FROM js_collection_node order by nodeid asc limit 100 ";
$query = mysql_query($SQL);
$redis = new redisPage('127.0.0.1',6379,0,'collection_node'); //實(shí)例化對(duì)象
$redis->clear(); //測(cè)試清空數(shù)據(jù)
while($info = mysql_fetch_assoc($query)){
  $redis->set_redis_page_info($info['nodeid'],$info); //插入數(shù)據(jù)
}
$redis->del_redis_page_info(array(61)); //刪除數(shù)據(jù)
$data = $redis->get_redis_page_info(1,10,array('nodeid','name')); //獲取分頁(yè)數(shù)據(jù)
print_r($data);
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+redis數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論