Use RedisTemplate template

一、Map =======boundHashOps

(1) a main method, the value of the Map storage among redis

@Resource
private RedisTemplate<String, Object> redisTemplate;
 
private static final Long EXPIRED_MINUTE = new Long(1);
private static final String HUM_NUM_KEY = "HUM_NUM_";    //统计医师人文医学测评次数
 
  @RequestMapping("/hum_num")
    public 
    @ResponseBody
    ApiResult getDoctorHumnum(@RequestParam Map<String,Object> paramMap) {
        
        paramMap.put("orgs", this.getOrganization());
        
        Map<String, Object> result = Maps.newHashMap();
        String key = this.generateKey(HUM_NUM_KEY, paramMap);
        if(this.isExistInRedis(key)) {
            result = this.getMapFromRedis(key);
        }else {
            result = doctorInfoService.selectDoctorHumNum(paramMap);
            this.putMapToRedis(key, result);
        }
        return ApiResult.success(result);
    }

(2) Set Key value

private String generateKey(String prefix, Map<String,Object> paramMap) {
        return prefix 
                + (paramMap.get("orgUuid")==null?"": paramMap.get("orgUuid").toString())
                + "_"
                + (paramMap.get("cycleId")==null?"": paramMap.get("cycleId").toString());
    }

Note: The above set identification key is actually a string, this string is stored as the key inside redis

(3) determining whether there is a key

Private  boolean isExistInRedis (String Key) {
         return ? redisTemplate.boundHashOps (Key) .size () == 0 false : to true ;
            // Note that because we insert a Map so that storage is the Hash content. 
    }

(4) Get value among the stored redis

Private the Map <String, Object> getMapFromRedis (String Key) { 
        
        // herein guava use of tools, simplifies the creation of generic HasMap erroneous operation when using a certain extent, 
        the Map <String, Object> Result = Maps.newHashMap (); 
        Map <Object, Object> = redisTemplate.boundHashOps Map (Key) .entries (); // Key the Get Map 
   
        // will traverse Map out      
        for (of Map.Entry <Object, Object> entry: EnumMap.entrySet ( )) { 
            result.put (entry.getKey () toString (), entry.getValue ());. 
        } 
        
        return Result; 
    }

(5) The value of the Map to insertion method among Redis

Private  void putMapToRedis (String Key, the Map <? ?,> the Map) { 
        
        redisTemplate.boundHashOps (Key) .putAll (the Map); 
        // set the expiration time, in minutes 
        redisTemplate.boundHashOps (key) .expire (EXPIRED_MINUTE, TimeUnit. MINUTES); 
    }

二、Object========boundVauleOps 

(1) Main Method

@Resource
private RedisTemplate<String, Object> redisTemplate;
 
@RequestMapping("/statistic")
    @ResponseBody
    public ApiResult statistic(@RequestParam Map<String, Object> paramMap) {
        String key = this.generateKey(PERIDIC_STATISTIC_TAB3_KEY, paramMap);
        StatisticGroupResult statistic = null;
        if (isExistStringInRedis(key)) {
            statistic =(StatisticGroupResult) redisTemplate.boundValueOps(key).get();
        } else {
            statistic = operateService.selectStatisticGroup(paramMap);
            redisTemplate.boundValueOps(key).set(statistic,EXPIRED_MINUTE, TimeUnit.MINUTES);
        }
        return ApiResult.success(statistic);
    }

(2) determining whether there is a key value

 // determines whether the value of type String Key exists 
    Private  Boolean isExistStringInRedis (String Key) {
         return ? RedisTemplate.boundValueOps (Key) .size () == 0 to false : to true ; 
    }

Note that there is something there and Map is used in common

三、List  ============boundListOps

(1) Main Method

    // 使用的都是String,Object方式
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
 
    @RequestMapping("/periodic_list")
    @ResponseBody
    public ApiResult periodicList(@RequestParam Map<String,Object> paramMap) {
        String key = this.generateKey(PERIDIC_TAB3_KEY ,paramMap);
        List<PeriodicExamineResult> result = new ArrayList<>();
        if (isExistListInRedis(key)) {
            List<Object> list = redisTemplate.boundListOps(key).range(0,-1);
            for (int i = 0; i < list.size(); i++) {
                result.add((PeriodicExamineResult) list.get(i));
            }
        }else {
            result = operateService.queryPeriodicExamineList(paramMap);
            for (PeriodicExamineResult temp:result) {
                redisTemplate.boundListOps(key).leftPush(temp);
                redisTemplate.boundListOps(key).expire(EXPIRED_MINUTE, TimeUnit.MINUTES);
            }
        }
        return ApiResult.success(result);
    }

Value (2) determines whether there is among List

 // determines the type of the list of key value exists 
    Private  Boolean isExistListInRedis (String key) {
         return redisTemplate.boundListOps (key) .size () == 0? To false : to true ; 
    }

四、当RedisTemplate<String,List<....>> redisTemplate;

    @Resource
    private RedisTemplate<String, List<PeriodicExamineResult>> redisTemplate;
 
     @org.junit.Test
    public void Test4() {
        String key = "LI_HAO"; // 设置key
        List<PeriodicExamineResult> result = new ArrayList<>();
        Map<String,Object> paramMap = Maps.newHashMap();
        result = operateService.queryPeriodicExamineList(paramMap);
        
        redisTemplate.boundListOps(key).leftPush(result); // 将获取的result放到redis当中
        redisTemplate.boundListOps(key).expire(10, TimeUnit.MINUTES);// Set Time 
 
        List <List <>> List PeriodicExamineResult = redisTemplate.boundListOps (Key) .Range (. 1, -1);   // value of which is stored into redis acquired out 
        for (PeriodicExamineResult RES: List.get (0 ) ) { 
            System.out.println (res.getTotal ()); 
        } 
    }

Note: range (1, -1) indicates traverse all items in the list. List Because we are inserted so it should be received in the List. Therefore, using list.get (0) so as to acquire a list of inserted 

Fifth, if you encounter this problem when using paging, then we can manually write a page, to undertake List

    //它所使用的模板同上面使用的一样为:
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
 
    @RequestMapping("/examine_result_search")
    @ResponseBody
    public JQGirdPageResult examineResultSearch(@RequestParam Map<String,Object> paramMap, @ModelAttribute PageBean pageBean) {
 
        String key = this.generateKey(PERIDIC_EXAMINE_TAB4_KEY, paramMap);
        List<PeriodicExaminePageResult> result = new ArrayList<>();
        if (isExistListInRedis(key)) {
            List<Object> list = redisTemplate.boundListOps(key).range(0,-1);
            for (int i = 0; i < list.size(); i++) {
                result.add((PeriodicExaminePageResult) list.get(i));
            }
            Long total =Long.valueOf(result.size());
            Integer pageSize = pageBean.getRows();
            Integer page = pageBean.getPage();
            JQGirdPageResult jqGirdPageResult = new JQGirdPageResult();
            Integer totalPage = total % pageSize == 0? total.intValue() / pageSize: total.intValue() / pageSize + 1;
            jqGirdPageResult.setTotal(totalPage == 0? 1: totalPage);
            jqGirdPageResult.setPage(page);
            jqGirdPageResult.setRows(result);
            jqGirdPageResult.setRecords(total);
            return jqGirdPageResult;
 
        } else {
            result = operateService.queryPeriodicPageList(paramMap);
            for (PeriodicExaminePageResult temp:result) {
                redisTemplate.boundListOps(key).leftPush(temp);
                redisTemplate.boundListOps(key).expire(EXPIRED_MINUTE, TimeUnit.MINUTES);
            }
            Long total =Long.valueOf(result.size());
            Integer pageSize = pageBean.getRows();
            Integer page = pageBean.getPage();
            JQGirdPageResult jqGirdPageResult = new JQGirdPageResult();
            Integer totalPage = total % pageSize == 0? total.intValue() / pageSize: total.intValue() / pageSize + 1;
            jqGirdPageResult.setTotal(totalPage == 0? 1: totalPage);
            jqGirdPageResult.setPage(page);
            jqGirdPageResult.setRows(result);
            jqGirdPageResult.setRecords(total);
            return jqGirdPageResult;
        }
 
    }

 

Guess you like

Origin www.cnblogs.com/xinghaonan/p/11900544.html
Recommended