启动缓存:SpringBoot启动程序中添加@EnableCaching
配置缓存:在yml上加配置
spring:
cache:
type: redis #表名用redis做缓存
在业务实现类添加注解
@Cacheable("shCatalogList")
public List<ShCatalog> cacheTestFindCatalog(String id) {
return shCatalogRepository.findByParentId(id);
}
/**
* 添加了数据之后就清除之前的缓存 指定名字清除
*/
@CacheEvict("shCatalogList")
public void addShCatalog() {
// 假装添加一行数据
shCatalogRepository.save(new ShCatalog());
}
这样的话第一次是查的数据库 第二次就是到redis缓存里面了 这就是redis外置二级缓存