看了幾天 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的時間復雜度的計算方式.