主頁(yè) > 知識(shí)庫(kù) > PHP操作redis實(shí)現(xiàn)的分頁(yè)列表,新增,刪除功能封裝類與用法示例

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

熱門標(biāo)簽:電子圍欄 阿里云 銀行業(yè)務(wù) Linux服務(wù)器 團(tuán)購(gòu)網(wǎng)站 服務(wù)器配置 Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng)

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

?php
/*
 * redis 分頁(yè)數(shù)據(jù)類庫(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'; //前綴名稱
  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 記錄名稱
   * @param $SortName Redis SortSet 記錄名稱
   * @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 記錄名稱
   * @param $SortName Redis SortSet 記錄名稱
   * @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 記錄名稱
   * @param $SortName Redis SortSet 記錄名稱
   * @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)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語(yǔ)法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

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

您可能感興趣的文章:
  • php操作redis數(shù)據(jù)庫(kù)常見(jiàn)方法實(shí)例總結(jié)
  • ThinkPHP3.2框架操作Redis的方法分析
  • php成功操作redis cluster集群的實(shí)例教程
  • PHP操作Redis數(shù)據(jù)庫(kù)常用方法示例
  • PHP操作Redis常用技巧總結(jié)
  • PHP實(shí)現(xiàn)操作redis的封裝類完整實(shí)例
  • php操作redis緩存方法分享
  • php操作redis中的hash和zset類型數(shù)據(jù)的方法和代碼例子
  • 30個(gè)php操作redis常用方法代碼例子
  • php操作redis常見(jiàn)方法示例【key與value操作】

標(biāo)簽:蚌埠 衢州 大理 江蘇 棗莊 廣元 萍鄉(xiāng) 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP操作redis實(shí)現(xiàn)的分頁(yè)列表,新增,刪除功能封裝類與用法示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266