redis初体验啦!!!
学了这么久终于在项目中用上了redis了呜呜呜~~~
- 尚硅谷永远的神yyds
雷神不是人是神
不说了上代码兄弟们好好看看有意思
//1.首先呢,你的创建一个缓存查询的封装方法
public List<CategoryEntity> cachelistWithTree() {
//缓存中呢,最好存json字符串,因为json是跨语言,跨平台的
//2.咱们先在缓存中去找数据,看能不能找到
String listWithTree = stringRedisTemplate.opsForValue().get("listWithTree");
//3.咱们就来判断这个字符串是不是空
if (StringUtils.isEmpty(listWithTree)){
//3.1,如果是空,咱们就来进行调用操作数据库的方法
List<CategoryEntity> categoryEntities = listWithTree();
//3.2,咱们查到了这个集合,就往缓存里面放,但是不要放list,要放json字符串,所以就转换一下
String s = JSON.toJSONString(categoryEntities);
//咱们就往缓存里一放,下次查的时候就有的
stringRedisTemplate.opsForValue().set("listWithTree",s);
return categoryEntities;
}
//4.如果不是空,那咱们就把这个json字符串搞成list集合
List<CategoryEntity> categoryEntities = JSON.parseObject(listWithTree, new TypeReference<List<CategoryEntity>>() {
});
return categoryEntities;
}