添加依赖
在项目的 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
这将引入 Spring Boot 的 Redis 启动器。
配置 Redis 连接信息
在 application.properties 文件中添加 Redis 连接信息:
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
可以根据实际情况修改 Redis 的连接信息。
配置 Redis 缓存管理器
在 Spring Boot 应用程序中,可以使用 RedisCacheManager 来管理 Redis 缓存。可以在配置类中创建 RedisCacheManager 实例,如下所示:
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import java.time.Duration;
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30)) // 设置缓存过期时间
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(cacheConfiguration);
return builder.build();
}
}
在上述示例中,创建了一个 RedisCacheManager 实例,并设置了缓存项的过期时间为 30 分钟,使用了 GenericJackson2JsonRedisSerializer 序列化器序列化缓存值。也可以使用其他序列化器来序列化缓存值。
使用 Redis 缓存
在需要进行缓存的方法上添加 @Cacheable 注解,并设置 cacheNames 和 key 属性。例如:
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Cacheable(cacheNames = "myCache", key = "#id")
public String getFromCache(String id) {
String value = // 从数据库中获取数据
return value;
}
}
在上述示例中,MyService 类使用了 @Cacheable 注解,并设置了 cacheNames 属性为 “myCache”,key 属性为方法的参数 id。当调用 getFromCache 方法时,Spring 会自动将结果缓存到 Redis 中,并在下一次调用时从缓存中返回结果。
移除 Redis 缓存
可以使用 CacheEvict 注解来从 Redis 缓存中移除缓存项。例如:
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
@Service
public class MyService {
可以使用 CacheEvict 注解来从 Redis 缓存中移除缓存项。例如:
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Cacheable(cacheNames = "myCache", key = "#id")
public String getFromCache(String id) {
String value = // 从数据库中获取数据
return value;
}
@CacheEvict(cacheNames = "myCache", key = "#id")
public void removeFromCache(String id) {
// 从 Redis 缓存中移除指定的缓存项
}
@CacheEvict(cacheNames = "myCache", allEntries = true)
public void clearCache() {
// 从 Redis 缓存中移除所有的缓存项
}
}
MyService 类使用了 @CacheEvict 注解,并设置了 cacheNames 属性为 “myCache”,key 属性为方法的参数 id。当调用 removeFromCache 方法时,Spring 会自动从 Redis 中移除指定的缓存项。另外,可以设置 allEntries 属性为 true,以移除指定缓存名称下的所有缓存项,例如在 clearCache 方法中。
可以根据需要进行适当的修改和扩展,例如可以使用 RedisTemplate 和 ValueOperations 接口来直接访问 Redis 缓存。
序列化注解
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)