一、Map =======boundHashOps
(1)主方法,向redis当中存储Map值
@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)设置Key值
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());
}
注意: 上面的设置key其实就是一个识别的字符串,将此字符串存储到redis里面作为key
(3)判断key是否存在
private boolean isExistInRedis(String key) {
return redisTemplate.boundHashOps(key).size()==0? false: true;
// 注意的是因为我们插入的是一个Map所以存储的是Hash的内容。
}
(4)获取redis当中存储的值
private Map<String, Object> getMapFromRedis(String key){
// 这里使用guava工具类中的方法,在一定程度上简化了使用HasMap时创建泛型的错误操作
Map<String, Object> result = Maps.newHashMap();
Map<Object, Object> map = redisTemplate.boundHashOps(key).entries();// 根据key获取Map
// 将Map遍历出来
for(Map.Entry<Object, Object> entry: map.entrySet()) {
result.put(entry.getKey().toString(), entry.getValue());
}
return result;
}
(5)将Map中的值插入到Redis当中的方法
private void putMapToRedis(String key, Map<?, ?> map) {
redisTemplate.boundHashOps(key).putAll(map);
// 设置过期时间,单位是分钟
redisTemplate.boundHashOps(key).expire(EXPIRED_MINUTE, TimeUnit.MINUTES);
}
二、Object========boundVauleOps
(1)主方法
@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)判断key值是否存在
// 判断字符串类型的Key值是否存在
private boolean isExistStringInRedis(String key) {
return redisTemplate.boundValueOps(key).size()==0? false: true;
}
注意这里面存在一些内容和Map中使用的是公共的
三、List ============boundListOps
(1)主方法
// 使用的都是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);
}
(2)判断List当中的值是否存在
// 判断列表类型的key值是否存在
private boolean isExistListInRedis(String key) {
return redisTemplate.boundListOps(key).size() == 0 ? false : 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);// 设置时间
List<List<PeriodicExamineResult>> list =redisTemplate.boundListOps(key).range(1,-1); //将存储到redis当中的值获取出来
for (PeriodicExamineResult res:list.get(0)) {
System.out.println(res.getTotal());
}
}
注意: range(1,-1) 表示遍历出列表中的所有内容。因为我们是以List插入的所以也应该以List进行接收。所以使用list.get(0)从而获取到插入的列表
五、如果在使用分页时遇到这个问题,那么我们可以手动的写一个分页,用来承接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;
}
}