文章目录
一、什么是缓存


二、添加Redis缓存

---------------------------------------------------Controller
@RestController
@RequestMapping("/shop-type")
public class ShopTypeController {
@Resource
private IShopTypeService typeService;
@GetMapping("list")
public Result queryTypeList() {
return Result.ok(typeService.queryShopTypeList());
}
}
@GetMapping("/{id}")
public Result queryShopById(@PathVariable("id") Long id) {
return shopService.queryById(id);
}
---------------------------------------------------Service
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public List<ShopType> queryShopTypeList() {
String cache_shop_type = stringRedisTemplate.opsForList().index(CACHE_SHOP_TYPE_KEY,0);
if (StrUtil.isNotBlank(cache_shop_type)) {
List<ShopType> arrayLists = JSONUtil.toList(cache_shop_type, ShopType.class);
return arrayLists;
}
List<ShopType> shopTypeList = query().orderByAsc("sort").list();
if (shopTypeList == null) {
return null;
}
stringRedisTemplate.opsForList().leftPush(CACHE_SHOP_TYPE_KEY,JSONUtil.toJsonStr(shopTypeList));
return shopTypeList;
}
}
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public Result

本文介绍如何使用Redis缓存优化应用性能,包括缓存更新策略、解决缓存穿透、缓存雪崩及缓存击穿等问题的方法。
最低0.47元/天 解锁文章
1233

被折叠的 条评论
为什么被折叠?



