@Autowired
private RedisOperation redisOperationl;
public final static String VQ = "vqTemplate:list";
@ApiOperation(value = "查询所有数据列表")
@GetMapping("/list")
public RespBean list() {
List<VqTemplateEntity> list = new ArrayList<>();
//先查缓存
List<String> redisList = redisOperationl.lGet(VQ,0,-1);
if(redisList == null || redisList.size() <=0){
//缓存没有就去数据库取
list = vqTemplateService.list();
// 将数据库中的数据添加到缓存里
if(list.size() != 0){
List<String> jsonStrList = new ArrayList<>();
for (VqTemplateEntity vqTemplateEntity:list) {
jsonStrList.add(JSON.toJSONString(vqTemplateEntity));
}
redisOperationl.lSetALL(VQ,jsonStrList);
}
}else {
//处理缓存中的数据
for (String jsonStr:redisList) {
//将json转换成对象
VqTemplateEntity vqTemplateEntity = JSONObject.parseObject(jsonStr,VqTemplateEntity.class);
list.add(vqTemplateEntity);
}
}
if (list.size() != 0) {
return RespBean.ok("查询成功", list);
} else {
return RespBean.error("无数据");
}
}