redis实现分页
redis实现分页功能,主要是将数据缓存起来,无需频繁查询数据库,减少数据库的压力。
适用场景:单用户操作列表界面分页,如博客列表。
缺点:不可模糊查询,缺少灵活性。
封装类:
class XgRedis
{
protected $_redis;
public function __construct($hash_prefix=''){
$this->_redis = connectRedis::getinstance();;
//$this->_redis = Redis::connection();
} /*
* 添加记录
* @param $hash_prefix 前缀
* @param $id 记录id
* @param $data 数据
* @return bool 返回值
*/
public function set_redis_page_info($hash_prefix,$id,$data){
if(!is_numeric($id) || !is_array($data)) return false;
$hashName = $hash_prefix.'_'.$id;
//同时设置 hash 的多个 field,已存在会自动更新
$this->_redis->hmset($hashName, $data);
//添加元素到有序集合,元素在集合中存在则更新对应的score(权)(key,权,值)
$this->_redis->zadd($hash_prefix.'_sort',$id,$id);
return true;
} /*
* 获取分页数据
* @param $hash_prefix 前缀
* @param $page 当前页数
* @param $pageSize 每页多少条
* @param $hashName Hash 记录名称
* @param $SortName Redis SortSet 记录名称
* @param $redis Redis 对象
* @param $key 字段数组 不传为取出全部字段
* @return array
*/
public function get_redis_page_info($hash_prefix,$page,$pageSize,$key=array()){
if(!is_numeric($page) || !is_numeric($pageSize)) return false;
$limit_s = ($page-1) * $pageSize;
$limit_e = ($limit_s + $pageSize) - 1;
//类似lrange操作从集合中去指定区间的元素,返回的是带有 score 值(可选)的有序结果集:
$range = $this->_redis->zrange($hash_prefix.'_sort',$limit_s,$limit_e);
//统计ScoreSet集合元素总数
$count = $this->_redis->zcard($hash_prefix.'_sort');
$pageCount = ceil($count/$pageSize); //总共多少页
$pageList = array();
foreach($range as $qid){
if(count($key) > 0){
$pageList[] = $this->_redis->hmget($hash_prefix.'_'.$qid,$key); //获取hash表中的field所有的数据
}else{
$pageList[] = $this->_redis->hgetall($hash_prefix.'_'.$qid); //获取hash表中所有的数据
}
}
$data = array(
'data'=>$pageList, //需求数据
'page'=>array(
'page'=>$page, //当前页数
'pageSize'=>$pageSize, //每页多少条
'count'=>$count, //记录总数
'pageCount'=>$pageCount //总页数
)
);
return $data;
} /*
* 获取单条记录
* @param $id id
* @return array
*/
public function show_redis_page_info($hash_prefix,$id){
$info = $this->_redis->hgetall($hash_prefix.'_'.$id);
return $info;
} /*
* 删除记录 单条或多条
* @param $ids ids 数组形式 [1,2,3]
* @param $hashName Hash 记录名称
* @param $SortName Redis SortSet 记录名称
* @param $redis Redis 对象
* @return bool
*/
public function del_redis_page_info($hash_prefix,$ids){
if(!is_array($ids)) return false;
foreach($ids as $value){
$hashName = $hash_prefix.'_'.$value;
$this->_redis->del($hashName);
$this->_redis->zrem($hash_prefix.'_sort',$value);
}
return true;
} /*
* 清空数据
* @param string $type db:清空当前数据库 all:清空所有数据库
* @return bool
*/
public function clear($type='db'){
if($type == 'db'){
$this->_redis->flushdb();
}elseif($type == 'all'){
$this->_redis->flushall();
}else{
return false;
}
return true;
} /*
* 入栈单条数据,针对某个用户可以,多用户并发就不好了
* @param $hash_prefix 前缀
* @param $data 数据
* @return bool 返回值
*/
public function in_data($hash_prefix,$data){
if(count($data) != count($data,1)) echo '须为一维数组';return false;
if(!is_array($data)) return false;
//统计ScoreSet集合元素总数
$id = $this->_redis->zcard($hash_prefix.'_sort');
$hashName = $hash_prefix.'_'.$id;
//同时设置 hash 的多个 field,已存在会自动更新
$this->_redis->hmset($hashName, $data);
//添加元素到有序集合,元素在集合中存在则更新对应的score(权)(key,权,值)
$this->_redis->zadd($hash_prefix.'_sort',$id,$id);
return true;
} }
连接redis:
class connectRedis
{
static $redis_port = '6379';
static $redis_host = '127.0.0.1'; private function __construct()
{
} static public $instance; static public function getinstance()
{
if (!self::$instance) self::$instance = new self();
try {
$redis = new Redis();
$redis->connect(static::$redis_host, static::$redis_port);
} catch (Exception $e) {
die('RedisException : Can\'t connect to ' . static::$redis_host . ':' . static::$redis_port);
}
return $redis;
}
}
连接数据库:
class connectVboxDb{
static $db = 'demo';
static $host = '192.168.1.20';
static $pdo;
private function __construct(){
$pdo = new PDO('mysql:host='.static::$host.';dbname='.static::$db,'root','Aaa2019.cn');
$pdo->query('set names utf8');//设置字符集
static::$pdo = $pdo;
} static public $instance;
static public function getinstance(){
if(!self::$instance) self::$instance = new self();
return self::$instance;
} static public function fetchAll($sql){
$qry = static::$pdo->query($sql);
$rst = $qry->fetchAll(PDO::FETCH_ASSOC);
return $rst;
}
}
index:
require_once('../connectRedis.php');
require_once('xgredis.php');
require_once('../../connectVboxDb.php'); $XgRedis = new XgRedis();
connectVboxDb::getinstance(); $post = connectVboxDb::fetchAll('select * from PaydayCardOrder');
$key_name = 'PaydayCardOrderList';
//var_dump($post);die;
foreach ($post as $k => $v) {
//将每条数据信息存储起来,这里key是数组key,信息后期有添加可以入栈添加,不影响其他信息,
$XgRedis->set_redis_page_info($key_name, $k, $v);
}
//$XgRedis->in_data($key_name,$post[0]);
//var_dump($XgRedis->del_redis_page_info($key_name,[52,53]));
var_dump($XgRedis->get_redis_page_info($key_name, 1, 100));
//var_dump($XgRedis->show_redis_page_info($key_name,0));
总结:redis做为缓存工具使用,
① 定义的key要简洁明了,不可过长。
② 无用数据要及时清除掉,防止服务器内存过大。清除的时候,建议设置过期时间,防止当前有其他用户使用。
③ 更新缓存数据,应当先更新数据库,再更新redis缓存,防止并发下出现脏数据。
当然网上很多也很全,自己整理的先就这些。
redis实现分页的更多相关文章
- hbase+springboot+redis实现分页
实现原理: 1.读取hbase数据每页的数据时多取一条数据.如:分页是10条一页,第一次查询hbase时, 取10+1条数据,然后把第一条和最后一条rowkey数据保存在redis中,redis中的k ...
- redis缓存分页思路
传统分页一般分页做缓存都是直接查找出来,按页放到缓存里,但是这种缓存方式有很多缺点.如缓存不能及时更新,一旦数据有变化,所有的之前的分页缓存都失效了.比如像微博这样的场景,微博下面现在有一个顶次数的排 ...
- [redis]redis实现分页的方法
每个主题下的用户的评论组装好写入Redis中,每个主题会有一个topicId,每一条评论会和topicId关联起来,大致的数据模型如下:{ topicId: 'xxxxxxxx', comments: ...
- redis缓存分页数据ID
1.用户通过分类.属性进来分页时 如果没有缓存,就读数据库前10页的数据Id,转为json,根据cate_分类1+cate_分类2+cate_分类3+arr_属性1+arr_属性2+arr_属性3作为 ...
- 使用MYSQL+Redis完成分页读取功能
public function getAnchorByPopularity($page, $pagesize){ //验证参数的正确性 if(!is_numeric($page) || !is_num ...
- redis实现分页技术
声明:原博客在这里https://www.cnblogs.com/find-the-right-direction/p/8465011.html,谢谢哥们提供,尊重原创. 本人是在原有的springb ...
- Redis 笔记与总结8 PHP + Redis 信息管理系统(分页+好友关注)
分页 要对列表页进行分页,需要知道: ①用户总数 $count ② 页大小 $pageSize:用户自定义 ③ 当前页:$page:GET 方式获取 ④ 总页数:$pageCount = ceil($ ...
- redis分页摘抄
Redis 笔记与总结8 PHP + Redis 信息管理系统(分页+好友关注) 分页 要对列表页进行分页,需要知道: ①用户总数 $count ② 页大小 $pageSize:用户自定义 ③ 当前页 ...
- 分页查询和redis
问题 我在做论坛的是时候遇到了如下的问题.论坛里可以有很多的主题topic,每个topic对应到很多回复reply.现在要查询某个topic下按照replyTime升序排列的第pageNo页的repl ...
随机推荐
- weblogic 安全漏洞 CVE-2017-5638
关于安全漏洞 CVE-2017-5638 的 Weblogic Server 防护建议 关于Weblogic Server如何防护防止近期爆出的Struts 2远程代码执行安全漏洞,为您提供以下内容参 ...
- Android为TV端助力 EventBus出现has no public methods called onEvent的问题
Caused by: de.greenrobot.event.EventBusException: Subscriber class com.hhzt.iptv.lvb_w.socket.MyMsgS ...
- 第五篇Scrum冲刺博客
一.Daily Scrum Meeting照片 二.每个人的工作 成员 ItemID 已完成工作 明天计划完成的工作 遇到的困难 张鸿 o1 整合界面至游戏中 将其他剩余功能进行整合 游戏状态的切换 ...
- 使用Log4Net进行错误日志记录
http://blog.csdn.net/zdw_wym/article/details/48802821
- Vue-cli在webpack内使用雪碧图(响应式)
先执行install cnpm install webpack-spritesmith 文件位置 build\webpack.dev.conf.js 添加内容: const SpritesmithPl ...
- java8 list转map,list集合中的元素的属性转set,list集合中对象的属性转list
一.使用java8对list操作 1.1list转map private Map<String, Member> getMemberMap() { List<Member> m ...
- UVA - 11090 - Going in Cycle!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- js 发送短信倒计时、秒杀倒计时实现代码
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- TensorRT&Sample&Python[yolov3_onnx]
本文是基于TensorRT 5.0.2基础上,关于其内部的yolov3_onnx例子的分析和介绍. 本例子展示一个完整的ONNX的pipline,在tensorrt 5.0的ONNX-TensorRT ...