缓存可以缓解数据库访问的压力,Spring自身不提供缓存的存储实现,需要借助第三方,比如JCache、EhCache、Hazelcast、Redis、Guava等。Spring Boot可以自动化配置合适的缓存管理器(CacheManager),默认采用的是ConcurrentMapCacheManager(java.util.concurrent.ConcurrentHashMap)。
(1)添加 spring-boot-starter-cache 依赖
Xml代码
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
(2)开启缓存功能
Java代码
@Configuration
@EnableCaching
public class CacheConfig {
}
(3)缓存数据
对于缓存的操作,主要有:@Cacheable、@CachePut、@Cache