Search code examples
spring-data-redisspring-hateoasspring-cache

Unable to store and retrieve Spring HATEOAS EntityModel in Redis


I have a use case where we are trying to store and retrieve content from Redis cache. We are using spring-starter-cache for making use of the underlying redis cache storage.

@Bean
    public RedisCacheManager dayCacheManager() {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(
                        RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()))
                .entryTtl(Duration.ofHours(10));
        return new CustomCacheManager(redisCacheWriter, redisCacheConfiguration);
    }

This is the cache bean we have configured currently.

@Cacheable(value = "cachename", cacheManager = "dayCacheManager", key = "{#unitList}")
    public EntityModel<PerformanceSummary> getWeekPerformanceSummary(String unitList) {
    //call API to get the data
}

With this we are neither able to store the EntityModel<PerformanceSummary> in redis cache nor retrieve it. What would be the correct approach to overcome this problem?


Solution

  • As per this Github post https://github.com/spring-projects/spring-hateoas/issues/424 we shouldn't be caching the EntityModel response which is fundamentally incorrect. We modified our logic to cache only the database response rather than the entire service response in redis to overcome this limitation.