无法将Spring HATEOAS EntityModel 存储和检索到Redis中。

huangapple go评论102阅读模式
英文:

Unable to store and retrieve Spring HATEOAS EntityModel in Redis

问题

我有一个使用案例,我们正在尝试从Redis缓存中存储和检索内容。我们使用spring-starter-cache来利用底层的Redis缓存存储。

这是我们当前配置的缓存bean。

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

使用这个配置,我们既无法将EntityModel<PerformanceSummary>存储在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 = &quot;cachename&quot;, cacheManager = &quot;dayCacheManager&quot;, key = &quot;{#unitList}&quot;)
	public EntityModel&lt;PerformanceSummary&gt; getWeekPerformanceSummary(String unitList) {
	//call API to get the data
}

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

答案1

得分: 0

根据这个 Github 帖子 https://github.com/spring-projects/spring-hateoas/issues/424,我们不应该缓存 EntityModel 响应,这在根本上是不正确的。我们修改了我们的逻辑,只缓存数据库响应,而不是整个服务响应,以克服这个限制。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月31日 19:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定