spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.19k stars 37.96k forks source link

Is there a way to cache http response on redis with WebClient #24569

Closed taylancapraz closed 4 years ago

taylancapraz commented 4 years ago

Hi

I decided to migrate existing Spring Boot projects to webflux. The Spring Boot version I update to is 2.2.4.RELEASE.

I have many microservices. I was caching http responses using in memory cache, and redis cache. I found a workaround to cache incoming responses in the current state. However, I need to use redis cache in some projects. Up to now, I couldn't find any way to do this.

Is there anyone who use redis cache with webflux? Or do you have any recommendation to do this?

In addition, will there be support for using @Cacheable annotation with webflux. If not, I would ask you to take it into planning.

bclozel commented 4 years ago

Hello @taylancapraz

Spring Data Redis supports reactive types, so this should be possible. Now depending on your use case (what should be the keys, what should be stored in Redis and how it should be serialized), the answer to your question may differ greatly.

Could you ask this question on StackOverflow? We're trying to keep the issue tracker for bugs and enhancements only. My only recommendation would be to pay attention to data size (avoid buffering in memory too large amounts of data) and to leverage, if possible, HTTP cache headers like ETags.

Now Spring Framework does not support reactive types with @Cacheable, but we have a dedicated issue for that (that you can subscribe to). The bad news is, right now there are very few cache implementations accepting those, so it's hard for us to design a solution that works for many implementations. See #17920

agisbert commented 4 years ago

Did anyone find any solution to this? We have a setup based on Caffeine, but we were hoping to migrate to Redis...

As far as I know, the solution would go into the direction of modifying/custom implementing the current RedisCacheManager, RedisCacheWriter, and RedisCache, so it would use the reactive Redis template to serialize/deserialize reactive types as if the whole cache thingy was a repository.

Ended up in -> https://jira.spring.io/browse/DATAREDIS-967 and to be honest, I cannot agree with what these guys state as reason. The cache implementations are blocking, not @Cacheable itself...

Does someone know of any persisting cache implementation (like Redis) that would support reactive types?

snicoll commented 4 years ago

@agisbert the Spring Data issue you've referenced is pointing to #17920 so please subscribe to that if you're interested in support of @Cacheable with reactive types.