SpringBoot集成缓存功能

作者 : admin 本文共3548个字,预计阅读时间需要9分钟 发布时间: 2024-06-9 共1人阅读

1. 缓存规范

Java Caching定义了五个核心接口,分别是:CachingProvider、CacheManager、Cache、Entry和Expiry。

  • CachingProvider:定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可以在运行期访问多个CachingProvider。
  • CacheManager:定义了创建、配置、获取、管理和控制多个唯一命名的Cache,这些Cache存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有。
  • Cache:是一个类似Map的数据结构并临时存储以key为索引的值,一个Cache仅被一个CacheManager所拥有。
  • Entry:是一个储存在Cache中的key-value对。
  • Expiry:每一个储存在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期的状态。一旦过期,条目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。
    SpringBoot集成缓存功能插图

2. 缓存抽象

Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术,并支持使用JCache(JSR-107)注解简化开发。

  • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合。
  • Cache接口下Spring提供了各种xxxCache的实现,如RedisCache、EhcacheCache、ConcurrentMapCache等。
描述
Cache缓存接口,定义缓存操作,实现有RedisCache、EhcacheCache、ConcurrentMapCache等
CacheManager缓存管理器,管理各种缓存(Cache)组件
@Cacheable主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@CacheEvict清空缓存
@CachePut保证方法被调用,又希望结果被缓存
@EnableCaching开启基于注解的缓存
keyGenerator缓存数据时key的生成策略
serialize缓存数据时value的序列化策略
@Cacheing指定多个缓存策略

3. 缓存装配

Spring Boot提供的缓存自动配置都记录在CacheAutoConfiguration中,其中CacheConfigurationImportSelector最为关键,它帮助我们导入常用缓存组件,组件如下:

  • org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration
  • org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration

使用缓存时只需要导入对应的实现即可,Spring Boot自动帮我们装配。

4. 简单缓存

使用Spring Boot提供的基于Map的内存级缓存

4.1 依赖管理

引入Spring Boot缓存场景启动器,默认导入的就是SimpleCacheConfiguration配置,是基于Map的内存级缓存

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

4.2 开启缓存

通过@EnableCacheing注解开启基于注解的缓存

@EnableCaching  // 开启基于注解的缓存
@SpringBootApplication
public class SimpleCacheServiceApplication {
    
    public static void main(String[] args) {
        // 启动应用程序
        SpringApplication.run(SimpleCacheServiceApplication.class, args);
    }
}

4.3 Cacheable

@Cacheable:先查询缓存数据,如果存在直接取出缓存数据返回,如果不存在就执行目标方法,将方法返回值存入缓存,常用于查询数据。

  • 注解属性
属性描述
cacheNames缓存的名字
key缓存索引,默认使用方法参数值
keyGenerator缓存索引生成器,与key属性二选一
cacheManager缓存管理器
condition满足条件进行缓存
unless满足条件不进行缓存
sync是否使用异步,默认为false
@Override
@Cacheable(cacheNames = "user", key = "#userId")
public User findUserInformationByUserId(Integer userId) {
    log.info("通过用户编号查询用户信息......");
    return new User(userId, "Jack", LocalDateTime.now(), true);
}
  • 第一次调用:先查询缓存,此时缓存中不存在数据,执行目标方法后将目标方法返回数据存入缓存
    SpringBoot集成缓存功能插图(1)

  • 第二次调用:先查询缓存,此时缓存存在数据不执行目标方法
    SpringBoot集成缓存功能插图(2)

4.4 CachePut

@CachePut:每次都先执行目标方法,将方法返回值存入缓存,常用于添加数据和修改数据。

  • 注解属性
属性描述
cacheNames缓存的名字
key缓存索引,默认使用方法参数值
keyGenerator缓存索引生成器,与key属性二选一
cacheManager缓存管理器
condition满足条件进行缓存
unless满足条件不进行缓存
@Override
@CachePut(cacheNames = "user", key = "#user.userId")
public Boolean insertUserInformation(User user) {
    log.info("业务层 - 添加用户信息......");
    return true;
}

4.5 CacheEvict

@CacheEvict:在执行目标方法之后清除缓存,也可以指定在执行目标方法之前清除缓存,常用于删除数据。

  • 注解属性
属性描述
cacheNames缓存的名字
key需要清除缓存的索引,默认使用方法参数值
keyGenerator缓存索引生成器,与key属性二选一
cacheManager缓存管理器
condition满足条件进行清除缓存
allEntries是否清除全部缓存,默认为false
beforeInvocation是否在目标方法执行之前清除缓存,默认为false
@Override
@CacheEvict(cacheNames = "user", key = "#userId")
public Boolean deleteUserInformationByUserId(Integer userId) {
    log.info("业务层 - 删除用户信息......");
    return true;
}

4.6 Caching

@Caching:方法级注解,可以指定复杂的缓存策略。

4.7 CacheConfig

@CacheConfig:类级注解,配置该类缓存策略的一些公共属性,比如缓存名称、缓存管理器、缓存索引生成策略

本站无任何商业行为
个人在线分享 » SpringBoot集成缓存功能
E-->