Search code examples
spring-mvcredisspring-cachespring-data-redis

Use @CachePut in springmvc project,but get the key and the value two separate data in redis


In the spring MVC project, I'm try to cache data with @CachePut, but in redis, there are two separate data for key and value: result in springmvc At the same time, I did the same thing with the springboot project and got the normal results: result in springboot The configuration in springmvc project:

<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>

and

@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;
}

Is there a problem with the configuration ?


Solution

  • In RedisCacheManager ,property usePrefix default value is false,so we should set usePrefix=true in JavaConfig:

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