redis新手入门,摸不着头脑可以看看<三>——lrange分页


看了几天 redis开发与运维,写了个小demo练练手,直接上代码。

1.首先是数据库,本地要有redis,具体的如何安装redis,官网下个就好了,sososo。

2.启动redis

注意启动命令。另,我的redis数据是通过单元测试直接写到数据库里的,贴一下

 1 @Test  2 public void testJedisPool1(){  3 //create a simple and not-safe pool  4 GenericObjectPoolConfig config = new GenericObjectPoolConfig();  5 //init connect pool  6 JedisPool jedisPool = new JedisPool(config,"127.0.0.1",6379);  7 Jedis jedis = null;  8 try {  9 jedis = jedisPool.getResource(); 10 for (int i = 1; i <= 100000; i++) { 11 jedis.rpush("nameList","zl"+i); 12  }//代码还是自己敲的为好 13 out.println("write ok"); 14 } catch (Exception e) { 15  e.printStackTrace(); 16 } finally { 17 if (jedis != null){ 18  jedis.close(); 19  } 20  } 21 }

3.controller

 1 /**
 2  * create by zl on 2018/2/23  3  *  4  */
 5 @RequestMapping("/milu")  6 @Controller  7 public class PagingController {  8     @RequestMapping("/paging")  9     public String paging(Model model,Long currentPage){ 10 
11 
12         //create a simple and not-safe pool
13         GenericObjectPoolConfig config = new GenericObjectPoolConfig(); 14         //init connect pool
15         JedisPool jedisPool = new JedisPool(config,"127.0.0.1",6379); 16         Jedis jedis = null; 17         try {//别偷懒 18             jedis = jedisPool.getResource(); 19             //total
20             long total = jedis.llen("nameList"); 21             //size
22             long size = 399L; 23             if (total/size==0){ 24                 total = total/size; 25             }else { 26                 total = total/size + 1; 27  } 28             // set currentPage
29             currentPage = currentPage==null?0L:currentPage; 30  out.println(total); 31             List<String> nameList = jedis.lrange("nameList",currentPage*size,(currentPage+1)*size); 32             model.addAttribute("nameList",nameList); 33             model.addAttribute("total",total); 34             model.addAttribute("currentPage",currentPage); 35             for (String name : nameList) { 36  out.println(name); 37  } 38         } catch (Exception e) { 39  e.printStackTrace(); 40         } finally { 41             if (jedis != null){ 42  jedis.close(); 43  } 44  } 45         return "paging"; 46  } 47 }

4,.页面就很简单了

 1 <%--
 2  Created by zl.  3   Date: 2018/2/23
 4   Time: 17:53
 5   To change this template use File | Settings | File and Code Templates.  6 --%>
 7 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 8 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 9 <%@ page isELIgnored="false" %>
10 <html>
11     <head>
12         <title>测试</title>
13     </head>
14     <style>
15  ul{
16  list-style: none;
17  float: left;
18         }
19  li{
20  width: 50px;
21  height: 50px;
22         }
23     </style>
24     <script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/jquery.min.js"></script>
25     <body>
26         <form action="${pageContext.request.contextPath}/milu/paging">
27             按页数查询:<input class="pageNum" name="currentPage" maxlength="10" value="输入要查询的页数">
28             <input type="submit" value="查询"><br><hr>
29         </form>
30         <strong>用户名称:</strong><br><hr>
31         <ul>
32             <c:forEach items="${nameList}" var="n">
33                 <li>${n}</li>
34             </c:forEach>
35         </ul>
36         <br><hr>
37         <a href="${pageContext.request.contextPath}/milu/paging?currentPage=${currentPage-1}">上一页</a>
38  当前第${currentPage+1}页,共${total}页 39         <a href="${pageContext.request.contextPath}/milu/paging?currentPage=${currentPage+1}">下一页</a>
40     </body>
41 </html>

5.效果图

 

 写在最后的,书中好像有写到lrange在高并发下可能会造成redis阻塞,应该用scan啥啥来着,忘了,有时间再补上

这里要纠正一下,redis开发与运维 中写的是 lrange在列表的两端性能较好,若列表较大,获取中间范围的元素性能会变差

从上图也可以看的出来lrange的时间复杂度的计算方式.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM