SpringBoot整合redis实现注解@Cacheable 缓存
本帖最后由 御坂主机 于 2024-6-6 20:56 编辑1. 简介
在现代应用开发中,缓存是提升性能和减轻数据库负载的重要手段。SpringBoot通过Spring Cache模块提供了便捷的缓存支持,而Redis作为高性能的内存数据库,常常被用作缓存存储。本文将介绍如何在SpringBoot中整合Redis,并使用@Cacheable注解实现缓存功能。
1.1 环境准备
在开始之前,需要确保已经安装了以下环境:
1. JDK 8或以上版本
2. Maven或Gradle构建工具
3. Redis服务器
2. 引入依赖
首先,需要在SpringBoot项目中引入相关依赖。以Maven为例,在pom.xml中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
3. 配置Redis
在application.properties文件中添加Redis的相关配置:
spring.redis.host=localhost
spring.redis.port=6379
spring.cache.type=redis
4. 启用缓存
在SpringBoot的主类(通常是带有@SpringBootApplication注解的类)上添加@EnableCaching注解,以启用缓存支持:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5. 配置Redis缓存管理器
创建一个配置类,用于配置Redis缓存管理器:
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(redisCacheConfiguration)
.build();
}
}
6. 使用@Cacheable注解
在需要缓存的方法上使用@Cacheable注解。假设有一个Service类,提供了获取用户信息的方法:
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Cacheable(value = "user", key = "#userId")
public User getUserById(Long userId) {
// 模拟从数据库中获取用户信息
User user = new User();
user.setId(userId);
user.setName("User" + userId);
return user;
}
}
在上述代码中,@Cacheable(value = "user", key = "#userId")表示将方法的返回结果缓存到名称为"user"的缓存中,缓存的键是userId。
7. 测试缓存功能
可以通过编写测试用例或运行应用程序进行测试。多次调用getUserById方法时,第一次调用会从数据库中获取数据,之后的调用将直接从缓存中获取数据。
8. 总结
本文介绍了如何在SpringBoot中整合Redis,并使用@Cacheable注解实现缓存功能。通过引入依赖、配置Redis和缓存管理器、启用缓存和使用注解,可以轻松实现缓存功能,从而提升应用性能。希望本文对您理解和应用SpringBoot缓存有所帮助。
------------------------------------------------------------------------------------------------------------------------------------------
========御 坂 主 机========
>> VPS主机 服务器 前沿资讯 行业发布 技术杂谈 <<
>> 推广/合作/找我玩TG号 : @Misaka_Offical <<
-------------------------------------------------------------------------------------------------------------------------------------------
页:
[1]