Spring RedisTemplate : Serialise multiple Model classes into JSON.Need to use Multiple RedisTemplates?

后端 未结 3 1227
野性不改
野性不改 2020-12-29 05:38

I am using Spring Redis support to save my objects in Redis.

I have several DAOs which handle different Model classes:

For example, ShopperHistoryDao

相关标签:
3条回答
  • 2020-12-29 05:49

    GenericJackson2JsonRedisSerializer should do the job

        @Bean
        public RedisTemplate<String, Object> redisTemplate() {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
            redisTemplate.setConnectionFactory(jedisConnectionFactory());
            redisTemplate.setKeySerializer(new StringRedisSerializer());                                           
            redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return redisTemplate;
        }
    

    This will add @Class property to the JSON to understand the type, which helps Jackson to deserialize, so no need to explicitly map the model on the configuration class.

    "{\"@class\":\"com.prnv.model.WhitePaper\",\"title\":\"Hey\",\"author\":{\"@class\":\"com.prnv.model.Author\",\"name\":\"Hello\"},\"description\":\"Description\"}"
    

    In the service you can cache the model using

        @Cacheable(value = "whitePaper", key = "#title")
        public WhitePaper findWhitePaperByTitle(String title) 
        {
            WhitePaper whitePaper = repository.findByTitle(title);
            return whitePaper;
        }
    

    Check this article: http://blog.pranavek.com/2016/12/25/integrating-redis-with-spring-application

    0 讨论(0)
  • 2020-12-29 06:01

    Yes, the RedisTemplate seems to be designed to have a single instance of the value serializer.

    I was going to suggest the possible workaround of having a RedisSerializer which contains a Map of inner serializers so you can use one RedisTemplate with a serializer that can handle multiple types - but since RedisSerializer does not offer methods like boolean canDeserialize(..) (as the HTTP MessageConverters in Spring MVC have) this doesn't seem possible.

    So it seems that you are stuck with having multiple RedisTemplate instances.

    0 讨论(0)
  • 2020-12-29 06:02

    A bit of old thread, but you can do something like this:

    <bean id="RedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="valueSerializer">
            <bean id="jackson2JsonRedisSerializer" 
                            class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
                <constructor-arg type="java.lang.Class" value="Object.class" />
            </bean>   
        </property>
    </bean>
    

    Then in your Java class

    @Autowire
    private RedisTemplate redisTemplate;
    
    public void save(Model model) {
        ObjectMapper obmap = new ObjectMapper();
        redisTemplate.opsForHash().putAll(mode.getId(), obmap.convertValue(model, Map.class));
    }
    
    0 讨论(0)
提交回复
热议问题