SpringBoot集成缓存功能

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设置。
    在这里插入图片描述

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);
}
  • 第一次调用:先查询缓存,此时缓存中不存在数据,执行目标方法后将目标方法返回数据存入缓存
    在这里插入图片描述

  • 第二次调用:先查询缓存,此时缓存存在数据不执行目标方法
    在这里插入图片描述

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:类级注解,配置该类缓存策略的一些公共属性,比如缓存名称、缓存管理器、缓存索引生成策略

相关推荐

  1. 探索 Spring Boot 集成缓存功能的最佳实践

    2024-06-10 07:24:04       9 阅读
  2. SpringBoot3】Spring Boot 3.0 集成 Redis 缓存

    2024-06-10 07:24:04       23 阅读
  3. springboot项目集成Redis,使用redis各项功能

    2024-06-10 07:24:04       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-10 07:24:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-10 07:24:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-10 07:24:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-10 07:24:04       20 阅读

热门阅读

  1. 正态分布公式

    2024-06-10 07:24:04       8 阅读
  2. 使用 AES 算法在 C# 中实现安全字符串加密和解密

    2024-06-10 07:24:04       11 阅读
  3. 使用Spring Cloud设计电商系统架构

    2024-06-10 07:24:04       10 阅读
  4. Spring RestClient报错:400 Bad Request : [no body]

    2024-06-10 07:24:04       10 阅读
  5. 临近空间飞艇技术

    2024-06-10 07:24:04       8 阅读
  6. IO流(字符流)

    2024-06-10 07:24:04       8 阅读