在Spring2.X后,配置redis缓存发生了许多变化,经多家搬砖配置成功,记载一下
配置文件
pom配置文件
<!--整合redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
yml配置文件
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database : 0
# Redis服务器地址
spring.redis.host : ENC(3JKUl3E6YNNEy3NMR/ZqaRxtMu8KRYGU)
# Redis服务器连接端口
spring.redis.port : 6379
# Redis服务器连接密码(默认为空)
spring.redis.password : ENC(02/kx8pDSQBHmJC5EvAk2GKxTxneimzq)
# 连接超时时间(毫秒)
spring.redis.timeout : 10000
#redis默认存储时间
redis.valid : 900
#lettuce客户端
spring.redis.lettuce.pool.min-idle : 0
spring.redis.lettuce.pool.max-idle : 8
spring.redis.lettuce.pool.max-wait : -1
spring.redis.lettuce.pool.max-active : 8
spring.redis.lettuce.shutdown-timeout : 100
springboot配置版文件
package club.bookweb.business.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class LettuceRedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.password}")
private String password;
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port);
config.setDatabase(0);
config.setPassword(RedisPassword.of(password));
return new LettuceConnectionFactory(config);
}
/**
* 配置RedisTemplate
* 设置添加序列化器
* key 使用string序列化器
* value 使用Json序列化器
* 还有一种简答的设置方式,改变defaultSerializer对象的实现。
*
* @return redisTemplate
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(lettuceConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
缓存配置文件
package club.bookweb.business.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
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.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* ** spring redis缓存配置文件 **
* </p>
*
* @author douguohai
* @since 2019-04-28
*/
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {
/**
* 此方法为了避免分布式系统对某个类的hash数值不同导致不使用缓存而创建的
* 单机状态下,这个几乎无作用
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@Bean
public CacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(lettuceConnectionFactory),
this.getRedisCacheConfigurationWithTtl(900), // 默认策略,未配置的 key 会使用这个
this.getRedisCacheConfigurationMap() // 指定 key 策略
);
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<String, RedisCacheConfiguration>();
redisCacheConfigurationMap.put("user", this.getRedisCacheConfigurationWithTtl(30)); // 单独设置某些cache的超时时间
redisCacheConfigurationMap.put("user2", this.getRedisCacheConfigurationWithTtl(100)); // 单独设置某些cache的超时时间
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
// 设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,
// 但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key,
// JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration
.disableCachingNullValues()
.computePrefixWith((name) -> {
return name + ":";
})
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.entryTtl(Duration.ofSeconds(seconds));
return redisCacheConfiguration;
}
}
先说遇到的问图
- 在redis中生成的缓存的 key 被默认的多加了一个 :
经查询源代码是这个引起的
package org.springframework.data.redis.cache;
@FunctionalInterface
public interface CacheKeyPrefix {
String compute(String var1);
static CacheKeyPrefix simple() {
return (name) -> {
return name + "::";
};
}
}
所以,做如下修改
redisCacheConfiguration = redisCacheConfiguration
.disableCachingNullValues()
.computePrefixWith((name) -> {
return name + ":";
})
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.entryTtl(Duration.ofSeconds(seconds));
网友评论