英文:
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 = "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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论