gpt4 book ai didi

spring-mvc - 在spring mvc项目中使用@CachePut,但是在redis中获取key和value两个单独的数据

转载 作者:可可西里 更新时间:2023-11-01 11:25:24 25 4
gpt4 key购买 nike

在spring MVC项目中,我尝试使用@CachePut来缓存数据,但是在redis中,key和value有两个独立的数据: result in springmvc同时对springboot项目做了同样的操作,得到了正常的结果: result in springbootspringmvc项目中的配置:

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${spring.redis.pool.maxTotal}"></property>
<property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property>
<property name="minIdle" value="${spring.redis.pool.minIdle}"></property>
<property name="maxWaitMillis" value="${spring.redis.pool.maxWait}"></property>
</bean>

<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig"></property>
<property name="hostName" value="${spring.redis.host}"></property>
<property name="port" value="${spring.redis.port}"></property>
<property name="password" value="${spring.redis.password}"></property>
<property name="database" value="${spring.redis.database}"></property>
<property name="timeout" value="${spring.redis.timeout}"></property>
</bean>

<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate" />
</bean>

<bean class="redis.clients.jedis.JedisPool">
<constructor-arg ref="poolConfig"/>
<constructor-arg value="${spring.redis.host}"/>
<constructor-arg type="int" value="${spring.redis.port}"/>
<constructor-arg type="int" value="${spring.redis.timeout}"/>
<constructor-arg type="java.lang.String" value="${spring.redis.password}"/>
<constructor-arg type="int" value="${spring.redis.database}"/>
</bean>

@Bean
public RedisTemplate<String , Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper mapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();

javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(properties.dateFormatter()));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(properties.timeFormatter()));
javaTimeModule.addSerializer(YearMonth.class, new YearMonthSerializer(properties.yearMonthFormatter()));
javaTimeModule.addSerializer(MonthDay.class, new MonthDaySerializer(properties.monthDayFormatter()));
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(properties.dateTimeFormatter()));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(properties.dateFormatter()));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(properties.timeFormatter()));
javaTimeModule.addDeserializer(YearMonth.class, new YearMonthDeserializer(properties.yearMonthFormatter()));
javaTimeModule.addDeserializer(MonthDay.class, new MonthDayDeserializer(properties.monthDayFormatter()));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(properties.dateTimeFormatter()));
mapper.registerModule(javaTimeModule);
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}

是不是配置有问题?

最佳答案

RedisCacheManager中,属性usePrefix默认值为false,所以我们应该设置usePrefix= JavaConfig 中的 true:

@Bean
public RedisCacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setUsePrefix(true);
return cacheManager;
}

关于spring-mvc - 在spring mvc项目中使用@CachePut,但是在redis中获取key和value两个单独的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45940780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com