當前位置:網站首頁>Redis分頁

Redis分頁

2022-05-13 21:24:42assasinSteven

<?php


class RedisPage {
    protected $_redis;
    protected $_redis_ip ;
    protected $_redis_port ;
    protected $_redis_db ;
    protected $_hash_prefix;

    /**
     * RedisPage constructor.
     * 初始化redis配置
     */
    public function __construct($ip = '',$port=6379,$db = 0,$hash_prefix = ''){
        $this->_redis_ip = $ip ? $ip : '127.0.0.1';
        $this->_redis_port = $port ? $port : 6379;
        $this->_redis_db = $db ? $db : 0 ;
        $this->_hash_prefix = $hash_prefix ? $hash_prefix : '';
        //實例化redis數據庫連接
        $this->_redis = new Redis();
        $this->_redis->connect($this->_redis_ip,$this->_redis_port);
        $this->_redis->select($this->_redis_db);
    }

    /**
     * @notes: 設置分頁數據至redis數據庫
     * 采用hash table 與 集合 Score字段
     * @auther: assasin <[email protected]>
     * @dateTime: 2022/04/15 10:36
     */
    public function set_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;
    }

    /**
     * @notes:獲取分頁數據
     * @auther: assasin <[email protected]>
     * @dateTime: 2022/04/15 10:42
     */
    public function get_page_info($page,$pageSize,$key = []){
        if(!is_numeric($page) || !is_numeric($pageSize)) return false;
        $limit_s = ($page - 1) * $pageSize;
        $limit_e = ($limit_s + $pageSize) - 1;
        //獲取在該區間內所有帶 score 的有序集合成員列錶
        $range = $this->_redis->zRange($this->_hash_prefix.'_sort',$limit_s,$limit_e);
        $count = $this->_redis->zCard($this->_hash_prefix.'_sort'); //統計總數
        $pageCount = ceil($count / $pageSize); //總頁數

        $page_data = [];
        foreach($range as $item){
            if(count($key) > 0){
                $page_data[] = $this->_redis->hMGet($this->_hash_prefix.'_'.$item,$key); //獲取hash table 中所有的集合數據
            }else{
                $page_data[] = $this->_redis->hGetAll($this->_hash_prefix.'_'.$item);
            }
        }

        $return_data = [
            'data' => $page_data, // 返回的數據
            'page' => $page, //當前頁
            'pageSize' => $pageSize,  //每頁的記錄數
            'total' => $count, //總條目數
            'pageCount' => $pageCount, //總頁數
        ];

        return $return_data;

    }

    /**
     * @notes:删除記錄
     * @auther: assasin <[email protected]>
     * @dateTime: 2022/04/15 10:54
     */
    public function del_page_info($id){
        if(!is_array($id)) return false;
        foreach($id as $val){
            $hashName = $this->_hash_prefix.'_'.$val;
            $this->_redis->del($hashName);
            $this->_redis->zRem($this->_hash_prefix.'_sort'.$val);
        }
        return true;
    }

    /**
     * @notes: 0:清空當前數據庫 -1:清空所有數據庫
     * @auther: assasin <[email protected]>
     * @dateTime: 2022/04/15 10:57
     */
    public function clear_db($db = 0){
        if ($db >= 0 ) {
            $this->_redis->flushDB();
        }elseif ($db == -1 ){
            $this->_redis->flushAll();
        }else{
            // 非法數據庫參數
            return false;
        }
        return true;
    }
}


// MySQL數據庫查詢
$conn = mysqli_connect('127.0.0.1','root1','a12345');
mysqli_select_db($conn,'chainpos');
mysqli_query($conn,"set names utf8");
$sql = "SELECT did,dish,sno FROM o_dish ORDER BY did LIMIT 100 ";
$query = mysqli_query($conn,$sql);
$redis = new Redispage('127.0.0.1',6379,0,'assasin');
//$redis->clear_db(0);die;
while($info = mysqli_fetch_assoc($query)){
    $redis->set_page_info($info['did'],$info); //插入數據
}
$redis->del_page_info(array(0)); //删除數據
$data = $redis->get_page_info(1,10,array('did','dish','sno')); //獲取分頁數據
echo "<pre>";
print_r($data);

版權聲明
本文為[assasinSteven]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/133/202205132045228398.html

隨機推薦