Redis实现分页查询

 在我们开发项目的过程中,经常会对数据做分页展示,如果每次请求都去查询数据库,当访问量增大时,势必会加重数据库的负载,降低数据库性能。然而,有些数据的是极少变动的,或者说变动的频率不是很高,这时如果将这些数据进行缓存,不仅可以提高程序性能,还能降低数据库的负载。下面就给出高并发下的分页数据缓存方案。

一、需要了解的一些知识点

1、redis的hmset(key, value)方法,将key值设置为value,value是map类型的数据结构

2、redis的hgetAll(key)方法,获取key的值,改值的类型为map类型的

3、关键字synchronized

4、key值包含的一些关键信息,前缀+当前的页数+每页数据的大小等

二、代码实现

          

 
  1. /* (non-Javadoc)

  2. * 默认为60秒刷新一次;采用加锁模式应对高并发

  3. */

  4. @Override

  5. public Page<StrategySummary> getAdvisorStrategys(AdvisorCondition condition) throws IOException{

  6. Pageable pageable=condition.getPage();

  7. int currentPage = pageable.getPageNumber();

  8. int pageSize = pageable.getPageSize();

  9. Map<String,String> result=RedisUtil.getMap("advisor:"+condition.getId()+"_number:"+currentPage+"_size:"+pageSize, MARKET);

  10. if (result!=null&&result.size()>0) {

  11. List<StrategySummary> dataList=JsonUtils.toJavaBeanList(result.get("data"), new TypeReference<List<StrategySummary>>() {});

  12. long total=Long.valueOf(result.get("total"));

  13. return new PageImplBean<StrategySummary>(dataList, pageable, total);

  14. } else {

  15. Page<StrategySummary> page=null;

  16. synchronized (MobileAppServiceImpl.class) { //当首个线程将数据缓存后,后面的线程需再次检查,防止重复查询数据库和缓存数据

  17. int count = 0;

  18. for (int i = 0; i < 3; i++) { //取3次,防止获取失败

  19. Map<String,String> map =RedisUtil.getMap("advisor:"+condition.getId()+"_number:"+currentPage+"_size:"+pageSize, MARKET);

  20. if (map==null||map.size()==0) {

  21. continue;

  22. }

  23. if (map!=null&&map.size()>0) {

  24. count++;

  25. break;

  26. }

  27. }

  28. if (count==0) {

  29. page= mobileAppDao.getAdvisorStrategys(condition);

  30. List<StrategySummary> dataList=page.getContent();

  31. long total=page.getTotalElements();

  32. if(dataList.size()>0){

  33. Map<String, String> map=new HashMap<String,String>();

  34. map.put("data", JsonUtils.toJsonString(dataList));

  35. map.put("total", String.valueOf(total));

  36. RedisUtil.setMapWithExpire("advisor:"+condition.getId()+"_number:"+currentPage+"_size:"+pageSize, map, MARKET);

  37. }

  38. }else{

  39. Map<String,String> map =RedisUtil.getMap("advisor:"+condition.getId()+"_number:"+currentPage+"_size:"+pageSize, MARKET);

  40. List<StrategySummary> dataList=JsonUtils.toJavaBeanList(map.get("data"), new TypeReference<List<StrategySummary>>() {});

  41. long total=Long.valueOf(map.get("total"));

  42. page= new PageImplBean<StrategySummary>(dataList, pageable, total);

  43. }

  44. }

  45. return page;

  46. }

  47.  
  48. }

          

猜你喜欢

转载自blog.csdn.net/xiaoyutongxue6/article/details/81394963