赞
踩
springboot3 + redis缓存整合。
还可以自定义指定缓存的缓存时间,以及所有缓存的默认的缓存时间。
网上大部分是通过自定义CacheManager来配置的,其实springboot支持自定义配置,通过实现 RedisCacheManagerBuilderCustomizer
来自定义。
下面是一套配置,这套内容支持自定义单个缓存的缓存时间,以及所有缓存的默认缓存时间。
引入依赖,无需指定版本,项目一般继承了springboot的spring-boot-starter-parent
,那里面已经写明了版本,不需要自己额外在写,省的版本冲突。
<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>
代码如下:
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import java.time.Duration; import java.util.List; /** * @author */ @Data @ConfigurationProperties("myservice.redis") public class MyRedisConfig { private Duration defaultExpireTime; private List<CacheAndExpire> cacheAndExpires; }
import lombok.Data;
import java.time.Duration;
/**
* @author
*/
@Data
public class CacheAndExpire {
private String cacheName;
private Duration expireTime;
}
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; /** * @author */ @Slf4j public class MyRedisCacheManagerBuilderCustomizer implements RedisCacheManagerBuilderCustomizer { private final MyRedisConfig myRedisConfig; public MyRedisCacheManagerBuilderCustomizer(MyRedisConfig myRedisConfig) { this.myRedisConfig = myRedisConfig; } @Override public void customize(RedisCacheManager.RedisCacheManagerBuilder builder) { log.info("my redis expire-time config!"); builder.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig().entryTtl(myRedisConfig.getDefaultExpireTime())); for (CacheAndExpire cacheAndExpire : myRedisConfig.getCacheAndExpires()) { builder.withCacheConfiguration(cacheAndExpire.getCacheName(), RedisCacheConfiguration.defaultCacheConfig().entryTtl(cacheAndExpire.getExpireTime())); } } }
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.io.Serializable; /** * @author */ @Configuration @Slf4j @EnableConfigurationProperties({MyRedisConfig.class}) public class RedisAutoConfig { /** * redisTemplate 序列化默认使用的jdk Serializable, 存储二进制字节码, 所以自定义序列化类 * 这里是参考的其他博客,也可以去掉,使用默认的jdk序列化类。 */ @Bean public RedisTemplate<Serializable, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.activateDefaultTyping(new LaissezFaireSubTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING); var jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class); // 设置value的序列化规则和key的序列化规则 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean MyRedisCacheManagerBuilderCustomizer myRedisCacheManagerBuilderCustomizer(MyRedisConfig myRedisConfig) { return new MyRedisCacheManagerBuilderCustomizer(myRedisConfig); } }
使用时,可以在配置文件中自定义,方法如下:
myservice:
redis:
# 采用ISO-8601时间格式。格式为:PnDTnHnMnS (n为个数)
#例如:P1Y2M3DT4H5M6.7S = 1年2个月3天4小时5分钟6.7秒
default-expire-time: PT30M
cache-and-expires:
- cache-name: mycachename
expire-time: PT10M
- cache-name: mycachename1
expire-time: PT1H
结束。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。