<?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 <assasin0308@sina.com>
* @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 <assasin0308@sina.com>
* @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 <assasin0308@sina.com>
* @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 <assasin0308@sina.com>
* @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);
Redis分页
最新推荐文章于 2023-06-17 10:37:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)