Skip to content

Add Redis cache support for Spring Cloud LoadBalancer - #1783

Open
yashdotdev13 wants to merge 2 commits into
spring-cloud:mainfrom
yashdotdev13:feat/1187-redis-loadbalancer-cache
Open

yashdotdev13 wants to merge 2 commits into
spring-cloud:mainfrom
yashdotdev13:feat/1187-redis-loadbalancer-cache

Conversation

@yashdotdev13

Copy link
Copy Markdown
Contributor

Summary

This PR adds support for reusing an existing Spring Cache CacheManager when Redis is configured as the cache type for Spring Cloud LoadBalancer.

Previously, Spring Cloud LoadBalancer created its own cache manager using Caffeine or the default implementation, regardless of the configured Spring cache type.

Changes

  • Added SpringCacheBasedLoadBalancerCacheManager to delegate cache operations to an existing Spring CacheManager.
  • Added Redis-specific load balancer cache auto-configuration when spring.cache.type=redis.
  • Prevented Caffeine and default load balancer cache configurations from activating when Redis is selected.
  • Added unit tests for the new cache manager and auto-configuration behavior.

Testing

  • Targeted tests passed.
  • Full spring-cloud-loadbalancer module tests passed (195 tests, 3 skipped).
  • Spring Java Format validation passed.
  • git diff --check passed.

Fixes #1187

Signed-off-by: 98001yash <yashchauhan.gaya@gmail.com>

@ryanjbaxter ryanjbaxter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServiceInstance lists can't be written to a default RedisCacheManager because they are not serializable. An integration test using a Redis TestContainer would have caught this so it would be good to add.

Also docs will need to be added

Signed-off-by: 98001yash <yashchauhan.gaya@gmail.com>
@yashdotdev13

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review!

I have updated the implementation to address the feedback:

  • Replaced the application CacheManager delegation with a dedicated Redis-backed LoadBalancerCacheManager, including custom serialization and TTL handling.
  • Added Redis cache key handling and cache operations.
  • Updated auto-configuration to select Redis only when spring.cache.type=redis and a RedisConnectionFactory is available, with fallback to Caffeine or the default cache.
  • Consolidated the Redis selection conditions using AllNestedConditions and NoneNestedConditions.
  • Added null validation for the CacheManager delegate.
  • Added negative tests for Redis selection without a RedisConnectionFactory, including fallback behavior, and tests covering multiple application CacheManager beans and the absence of an application CacheManager.
  • Added Redis cache tests and documentation for configuring Redis-backed LoadBalancer caching.

The targeted updated test classes pass: 32 tests, 0 failures, 0 errors.

I've pushed these changes in the latest commit. Please let me know if anything else needs adjustment. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add loadbalancer Cache support for redis

3 participants