redis分页
模仿的https://www.cnblogs.com/dee0912/p/4612183.html
第一步连接redis后进行添加数据
require_once '../redis/redis.php';
$redis = new RedisDriver();
/*$result = $redis->getStr('userid');
var_dump($result) ;*/
//uid 自加
//当有 userid 这个键时,执行 incr时该键的值加1;不存在该键时,创建一个 userid,值为0,执行incr,值为1
/* for ($i=0; $i < 50; $i++) {
$uid = $redis->incrs('userid');
//向 hash 表中批量添加数据:hMset
$age = mt_rand(10,20);
$redis->setHash('user:'.$i, array('uid'=>$i, 'name'=>'name:'.$i, 'age'=>$age));
//把用户 id 存入链表
$redis->rpushs('uid', $i);
}
die;*/
第二部,从redis中查询数据进行分页
/*分页*/
//用户总数
$count = $redis->getListSize('uid');
//页大小
$pageSize = 5;
//当前页码
$page = !empty($_GET['page'])?$_GET['page']:1;
//页总数
$pageCount = ceil($count / $pageSize);
//echo $count.'='.$page.'='.$pageCount;die;
/*
第 1 页:lrange 0 4
第 2 页:lrange 5 9
第 3 页:lrange 10 14
第 n 页:lrange ($page - 1) * $pageSize ($page - 1) * $pageSize + ($pageSize - 1)
*/
$ids = $redis->lranges('uid', (($page - 1) * $pageSize), (($page - 1) * $pageSize + ($pageSize - 1))); /*echo "<pre>";
print_r($ids); die;*/
/*
var_dump($ids);
$page = 1
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" }
*/ foreach($ids as $k => $v){
$data[] = $redis->hgetalls('user:'.$v);
} //$newArray1 = array_column($data,NULL,'uid');
///array_column() 返回input数组中键值为column_key的列, 如果指定了可选参数index_key,那么input数组中的这一列的值将作为返回数组中对应值的键。 ?>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th>uid</th>
<th>name</th>
<th>age</th>
<th>操作</th>
</tr>
<?php foreach($data as $v){ ?>
<tr>
<td><?php echo $v['uid']?></td>
<td><?php echo $v['name']?></td>
<td><?php echo $v['age']?></td>
<td><a >删除</a> | uid:<?php echo $v['uid'];?></td>
</tr>
<?php } ?>
<tr><td colspan="4">
<a href="?page=<?php echo ($page-1)<=1?1:($page-1);?>">上一页</a>
<a href="?page=<?php echo ($page+1)>$pageCount?$pageCount:($page+1);?>">下一页 </a>
<a href="?page=1">首页</a>
<a href="?page=<?php echo $pageCount;?>">尾页</a>
当前<?php echo $page;?>页
总共<?php echo $pageCount;?>页
共<?php echo $count;?>用户
</td>
</tr>
</table>
</body>
</html>
require_once '../redis/redis.php';上面引入的redis类
<?php /**
* ------------------------------------------
* 统一redis的配置与数据存储规范,便于扩展与修改
* # redis通常用于热数据与消息列队等场景
* # list内存储array是采用json格式
*
*/ class RedisDriver
{ public $redis = null; public function __construct() {
if (is_null($this->redis)) {
$this->redis = new \Redis();
$this->redis->connect('127.0.0.1', '6379');
}
} /**给key加一
* [incrs description]
* @param [type] $key [description]
* @return [type] [description]
*/
public function incrs($key) {
$this->redis->incr($key);
} // 设置一条String
public function setStr($key, $text, $expire = null)
{
$key = 'string:' . $key;
$this->redis->set($key, $text);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
} // 获取一条String
public function getStr($key)
{
$key = 'string:' . $key;
$text = $this->redis->get($key);
return empty($text) ? null : $text;
} // 删除一条String
public function delStr($key)
{
$key = 'string:' . $key;
$this->redis->del($key);
} // 设置一条Hash
public function setHash($key, $arr, $expire = null)
{
$key = $key;
$this->redis->hMset($key, $arr);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
} public function hgetalls($key) {
$arr = $this->redis->hgetall($key);
return empty($arr) ? 'null' : $arr;
} // 获取一条Hash,$fields可为字符串或数组
public function getHash($key, $fields = null)
{
$key = 'hash:' . $key;
if (is_null($fields)) {
$arr = $this->redis->hGetAll($key);
} else {
if (is_array($fields)) { $arr = $this->redis->hmGet($key, $fields);
foreach ($arr as $key => $value) {
if ($value === false) {
unset($arr[$key]);
}
}
} else {
$arr = $this->redis->hGet($key, $fields);
}
}
return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
} // 删除一条Hash,$field为字符串
public function delHash($key, $field = null)
{
$key = 'hash:' . $key;
if (is_null($field)) {
$this->redis->del($key);
} else {
$this->redis->hDel($key, $field);
}
} // 在Hash的field内增加一个值 (值之间使用“,”分隔)
public function fieldAddVal($key, $field, $val)
{
$arr = $this->getHash($key, $field);
if (!is_null($arr)) {
$str = reset($arr);
$arr = explode(',', $str);
foreach ($arr as $v) {
if ($v == $val) {
return;
}
}
$str .= ",{$val}";
$this->setHash($key, array($field => $str));
} else {
$this->setHash($key, array($field => $val));
}
} // 在Hash的field内删除一个值
public function fieldDelVal($key, $field, $val)
{
$arr = $this->getHash($key, $field);
if (!is_null($arr)) {
$arr = explode(',', reset($arr));
$tmpStr = '';
foreach ($arr as $v) {
if ($v != $val) {
$tmpStr .= ",{$v}";
}
}
if ($tmpStr == '') {
$this->delHash($key, $field);
} else {
$this->setHash($key, array($field => substr($tmpStr, 1)));
}
}
} // 设置表格的一行数据
public function setTableRow($table, $id, $arr, $expire = null)
{
$key = '' . $table . ':' . $id;
$this->redis->hMset($key, $arr);
if (!is_null($expire)) {
$this->redis->setTimeout($key, $expire);
}
} // 获取表格的一行数据,$fields可为字符串或数组
public function getTableRow($table, $id, $fields = null)
{
$key = '' . $table . ':' . $id;
if (is_null($fields)) {
$arr = $this->redis->hGetAll($key);
} else {
if (is_array($fields)) {
$arr = $this->redis->hmGet($key, $fields);
foreach ($arr as $key => $value) {
if ($value === false) {
unset($arr[$key]);
}
}
} else {
$arr = $this->redis->hGet($key, $fields);
}
}
return empty($arr) ? null : (is_array($arr) ? $arr : array($fields => $arr));
} // 删除表格的一行数据
public function delTableRow($table, $id)
{
$key = '' . $table . ':' . $id;
$this->redis->del($key);
} public function rpushs($key, $value) {
$this->redis->rpush($key,$value);
} public function lranges($key,$start,$end) {
return $this->redis->lrange($key,$start,$end);
} // 推送一条数据至列表,头部
public function pushList($key, $arr)
{
$key = 'list:' . $key;
$this->redis->lPush($key, json_encode($arr));
} // 从列表拉取一条数据,尾部
public function pullList($key, $timeout = 0)
{
$key = 'list:' . $key;
if ($timeout > 0) {
$val = $this->redis->brPop($key, $timeout); // 该函数返回的是一个数组, 0=key 1=value
} else {
$val = $this->redis->rPop($key);
}
$val = is_array($val) && isset($val[1]) ? $val[1] : $val;
return empty($val) ? null : $this->objectToArray(json_decode($val));
} // 取得列表的数据总条数
public function getListSize($key)
{
$key = $key;
return $this->redis->lSize($key);
} // 删除列表
public function delList($key)
{
$key = 'list:' . $key;
$this->redis->del($key);
} // 使用递归,将stdClass转为array
protected function objectToArray($obj)
{
if (is_object($obj)) {
$arr = (array) $obj;
}
if (is_array($obj)) {
foreach ($obj as $key => $value) {
$arr[$key] = $this->objectToArray($value);
}
}
return !isset($arr) ? $obj : $arr;
} }
redis分页的更多相关文章
- redis 分页
redis 分页 > rpush a (integer) > rpush a (integer) > rpush a (integer) > rpush a (integer) ...
- redis分页摘抄
Redis 笔记与总结8 PHP + Redis 信息管理系统(分页+好友关注) 分页 要对列表页进行分页,需要知道: ①用户总数 $count ② 页大小 $pageSize:用户自定义 ③ 当前页 ...
- 【Redis】redis分页查询理解
偶然在代码中发现一个接口,接口定义说是分页查询,但逻辑实现是Redis.不太理解,Redis怎么分页?后来看到一篇文章,然后了解了. 1.Zrevrange实现 通过SortedSet的zrevran ...
- java redis 分页查询数据
package com.liying.tiger.test; import java.util.List; import org.springframework.context.Application ...
- redis分页获取数据
php代码: 采用哈希类型存储数据,有序集合存储分页数据,进行倒序与正序的排序. $getGoodsInfo = M('goods_test')->select(); for($i=0;$i&l ...
- 分页查询和redis
问题 我在做论坛的是时候遇到了如下的问题.论坛里可以有很多的主题topic,每个topic对应到很多回复reply.现在要查询某个topic下按照replyTime升序排列的第pageNo页的repl ...
- Redis从入门到精通:中级篇
原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...
- spring boot整合reids 然后实现缓存分页(方法之一) 以及RedisTemplate存到reids 里面get 就消失的坑
业务需求 首页 实现缓存分页 spring boot 整合redis (我的是2.0.3版本的) 在pom 文件写上依赖包即可 <dependency><!--依赖包--> ...
- Redis从入门到精通:中级篇(转)
原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...
随机推荐
- 产品研发团队如何融合OKR与Scrum敏捷开发?
「 OKR 」现在非常的火爆,很多公司都在使用,不仅国外的 Google.英特尔等大公司在用,国内的一线知名互联网企业今日头条和一些创业团队也都在使用. 那为什么「 OKR 」这么受欢迎呢,因为把它可 ...
- 使用mpvue开发小程序教程(二)
在上篇文章中,我们介绍了使用mpvue开发小程序所需要的一些开发环境的搭建,并创建了第一个mpvue小程序代码骨架并将其运行起来.在本文中,我们来研究熟悉一下mpvue项目的主要目录和文件结构. 在V ...
- Asp.Net Core 程序部署到Linux(centos)生产环境(一):普通部署
运行环境 照例,先亮底 centos:7.2 cpu:1核 2G内存 1M带宽 辅助工具:xshell xftp 搭建.net core运行环境 .net core 的运行环境我单独写了一篇,请看我的 ...
- docker 复制镜像和复制容器
复制镜像和复制容器都是通过保存为新镜像而进行的. 具体为: 保存镜像 docker save ID > xxx.tar docker load < xxx.tar 保存容器 docker ...
- 是时候给你的产品配一个AI问答助手了!
本文由云+社区发表 | 导语 问答系统是信息检索的一种高级形式,能够更加准确地理解用户用自然语言提出的问题,并通过检索语料库.知识图谱或问答知识库返回简洁.准确的匹配答案.相较于搜索引擎,问答系统能更 ...
- [五] JavaIO之InputStream OutputStream简介 方法列表说明
InputStream 和 OutputStream 对于字节流的输入和输出 是作为协议的存在 所以有必要了解下这两个类提供出来的基本约定 这两个类是抽象类,而且基本上没什么实现,都是依赖于子类具 ...
- 痞子衡嵌入式:ARM Cortex-M文件那些事(8)- 镜像文件(.bin/.hex/.s19)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式开发里的image文件(.bin, .hex, .s19). 今天这节课是痞子衡<ARM Cortex-M文件那些事>主 ...
- C# 判断用户是否对路径拥有访问权限
如何获取当前系统用户对文件/文件夹的操作权限? 1.获取安全信息DirectorySecurity DirectorySecurity fileAcl = Directory.GetAccessCon ...
- [我还会回来的]asp.net core再战iris
废话不多说,直接开干! 硬件配置 处理器: Intel(R) Core(TM) i5-4690k CPU @3.90GHz 内存容量: 8.00 GB 软件版本 OS: Microsoft Windo ...
- ubuntu所有php扩展php-7.0扩展列表
sudo apt-get install php7.0-bcmath sudo apt-get install php7.0-bz2 sudo apt-get install php7.0-calen ...