type_id, @Req"/>

REDIS|11 redis做分页

REDIS|11 redis做分页

之前的数据都加载到了本地java的Map,分页如下

@RequestMapping("/articles")String articles(Model model, HttpServletRequest req, HttpServletResponse rsp, @RequestParam Optional<Integer> type_id, @RequestParam Optional<Integer> page_no) {//文章类型Predicate<Article> typeFilter = type_id.orElse(0)==0?(a)->true: (a)->a.getType_id() == type_id.get();Collection<Article> articles = ArticleManager.inst().getArticles().values();//分页int sum = (int) articles.stream().filter(typeFilter).count();int pages_num = (int) Math.ceil((double)sum/(double)PAGE_SIZE);// magic numint skip_num = page_no.orElse(0) * PAGE_SIZE;    List<Article> list = articles.stream()    .filter(typeFilter)    .sorted(Comparator.comparing(Article::getId).reversed())    .skip(skip_num)    .limit(PAGE_SIZE)    .map(ArticleManager::initArticle)    .collect(Collectors.toList());        model.addAttribute("type_list", ArticleManager.inst().getArticleTypes().values());model.addAttribute("sum", sum);//总文章数model.addAttribute("pages_num", pages_num);//总页数model.addAttribute("page_no", page_no.orElse(0));//第几页model.addAttribute("page_size", PAGE_SIZE);//每页大小model.addAttribute("list", list);model.addAttribute("type_id", type_id.orElse(0));//页码List<Integer> page_nos = new ArrayList<Integer>();for(int i=0; i< pages_num; i++){page_nos.add(i);}model.addAttribute("page_nos", page_nos);return "articles";}


如果对redis的数据分页浏览,思路是是用redis的sort set

比如这是积分商城排行榜的分页

后端:

//分页int _page_size = page_size.orElse(100);//每页大小int _page_no = page_no.orElse(0);int sum = ShopManager.inst().getScoreSetSize(shop_sn).intValue();//信息总条数int pages_num = (int) Math.ceil((double)sum/(double)_page_size);//页数int start = _page_no * _page_size;int end = start + _page_size -1;//下标List<DataVO> result = ShopManager.inst().rankList(shop_sn, start, end);List<Integer> page_nos = new ArrayList<Integer>();for(int i=0; i< pages_num; i++){page_nos.add(i);}model.addAttribute("page_nos", page_nos);model.addAttribute("sum", sum);//总文章数model.addAttribute("uri", "/mg/shop_debug_simple/"+shop_sn);model.addAttribute("pages_num", pages_num);//总页数model.addAttribute("page_no", page_no.orElse(0));//第几页model.addAttribute("page_size", _page_size);//每页大小//分页end

前端:
<div >            <ul >                <li>                    <a th:if= "${(page_no-1)>=0}" th:href="${uri+'?page_no='+(page_no-1)+'&page_size='+page_size}">上一页</a>                </li>                <li th:each="no: ${page_nos}">                    <a th:href="${uri+'?page_no='+no+'&page_size='+page_size}" th:text="${(no+1)+' '}" th: ></a>                </li>                <li>                   <a th:if= "${(page_no+1)<pages_num}" th:href="${uri+'?page_no='+(page_no+1)+'&page_size='+page_size}">下一页</a>                </li>            </ul>        </div>

推荐阅读