SpringBoot的Cacheable缓存注解

当我们的应用程序需要频繁地读取和写入数据时,为了提高应用程序的性能,我们通常会使用缓存技术 。Spring Boot 提供了一种简单而强大的缓存框架,它可以轻松地将数据缓存到 redis 中 。
在 Spring Boot 中可以在方法上简单的加上注解实现缓存 。
Redis 缓存配置首先,您需要在您的项目中添加 Redis 的依赖 。您可以将以下依赖添加到您的项目的 pom.xml 文件中:
xml复制代码<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>一旦 Redis 的依赖被添加,您需要配置 Redis 的相关信息 。以下是一个示例 Redis 配置:
yaml复制代码spring:redis:host: 127.0.0.1port: 6379password:database: 0在上述配置文件中,host 和 port 属性指定了 Redis 服务器的主机名和端口号,password 属性用于指定 Redis 服务器的密码(如果有的话),而 database 属性则指定了 Redis 服务器使用的数据库编号 。
Redis 的默认序列化器是
JdkSerializationRedisSerializer,但是在实际使用中,由于其序列化后的大小通常比较大,因此我们通常使用 StringRedisSerializer 或者 Jackson2JsonRedisSerializer 将缓存值序列化为字符串或者 JSON 格式 。以下是一个自定义序列化器的示例:
JAVA复制代码@Configurationpublic class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(connectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));return template;}}在此示例中,我们通过自定义 Bean 配置了 RedisTemplate,使用 StringRedisSerializer 序列化 Redis 键,并使用
Jackson2JsonRedisSerializer 序列化 Redis 值为 JSON 格式 。
Cacheable 注解【SpringBoot的Cacheable缓存注解】使用 Cacheable 注解来标记需要进行缓存的方法 。以下是一个具有 Cacheable 注解的示例方法:
java复制代码@Servicepublic class UserService {@Cacheable(value = https://www.isolves.com/it/cxkf/jiagou/2023-08-31/"users", key = "#id")public User getUserById(Long id) {// 查询用户并返回}}在这个例子中,@Cacheable 注解用于标记 getUserById 方法,而 value 属性则用于指定缓存的存储区域的名称 。由于我们正在使用 Redis 作为缓存,因此 Redis 中的 key 将由 Cacheable 注解中的 key 属性指定 。在此示例中,key 属性设置为 "#id",这意味着我们将使用方法参数 id 作为 Redis 缓存的键 。
多参数 Cacheable 注解在某些情况下,我们需要以多个参数作为 key 来缓存数据 。此时,我们可以对 key 属性使用表达式 language(SpEL)来设置多个参数:
java复制代码@Servicepublic class UserService {@Cacheable(value = https://www.isolves.com/it/cxkf/jiagou/2023-08-31/"users", key = "#id + '_' + #name")public User getUserByIdAndName(Long id, String name) {// 查询用户并返回}}在上述示例中,我们使用了表达式语言(SpEL)将 id 和 name 两个参数组合成了一个 Redis 缓存键 。
缓存的有效期缓存的有效期实际上是一个非常重要的问题,对于缓存的性能和可靠性都有很大的影响 。可以使用 @Cacheable 注解上的 expire 属性来设置缓存的过期时间 。以下是一个设置缓存时效的示例:
java复制代码@Servicepublic class UserService {@Cacheable(value = https://www.isolves.com/it/cxkf/jiagou/2023-08-31/"users", key = "#id", expire = 600)public User getUserById(Long id) {// 查询用户并返回}}在此示例中,我们添加了一个名为 expire 的属性,该属性用于指定缓存的过期时间(以秒为单位) 。在此示例中,我们设置了缓存过期时间为 600 秒,也就是 10 分钟 。
缓存的清除 @CacheEvict有时候,您需要清除 Redis 缓存中的某些数据,以便在下一次访问时重建缓存 。在 Spring Boot 中,可以使用 @CacheEvict 注解来清除 Redis 缓存中的数据 。以下是一个使用 @CacheEvict 注解的示例:
java复制代码@Servicepublic class UserService {@Cacheable(value = https://www.isolves.com/it/cxkf/jiagou/2023-08-31/"users", key = "#id")public User getUserById(Long id) {// 查询用户并返回}@CacheEvict(value = "users", key = "#id")public void deleteUserById(Long id) {// 删除用户并返回}@CacheEvict(value = "users", allEntries = true)public void deleteAllUsers() {// 删除所有用户并返回}}在此示例中,我们添加了删除单个用户和删除所有用户的两个方法,使用 @CacheEvict 注解来删除 Redis 缓存中的相应数据 。请注意,我们设置了 allEntries 属性为 true,以删除所有缓存中的数据 。


推荐阅读