Fix cache instance divergence when custom ConnectionProvider is used with client builders - #4664
Fix cache instance divergence when custom ConnectionProvider is used with client builders#4664AmariahAK wants to merge 3 commits into
Conversation
…with client builders When a custom ConnectionProvider is passed to a client builder (e.g. MultiDbClient.builder().connectionProvider(myProvider).build()), and that provider internally carries a Cache, UnifiedJedis.getCache() returned null because the builder never learned about the cache. The connections used client-side caching but the client API reported none. This fix: - Adds default Cache getCache() to the ConnectionProvider interface - Stores and exposes the Cache in all four provider implementations (PooledConnectionProvider, MultiDbConnectionProvider, ClusterConnectionProvider, SentineledConnectionProvider) - Bridges the cache in AbstractClientBuilder.build() so the builder picks up the cache from a custom provider when none was set explicitly - Adds unit tests for RedisClient and MultiDbClient with custom providers - Adds cache assertions to existing MultiDbCSCTest integration tests Co-authored-by: atlarix-agent <agent@atlarix.dev>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 361bf2029f
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
- Add @SInCE 8.0 Javadoc to ConnectionProvider.getCache() - Expose cache from ConnectionFactory so factory-backed PooledConnectionProvider constructors preserve it - PooledConnectionProvider factory constructors now extract cache from ConnectionFactory via instanceof check Fixes the two reviewer comments on PR redis#4664: 1. Missing @SInCE tag on new public API 2. getCache() returning null when PooledConnectionProvider is built from a cache-enabled ConnectionFactory Co-authored-by: atlarix-agent <agent@atlarix.dev>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c8132f3c21
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Codex bot flagged the new public method on the public class as missing the @SInCE tag required by AGENTS.md for all new public API. Co-authored-by: atlarix-agent <agent@atlarix.dev>
|
The team is stretched right now and may take some time before we get to it. Adding @atakavci to take a look as well. |
|
@ggivo no worries, thanks for taking time to review regardless. |
Fixes #4588
What was the issue
When a custom
ConnectionProvideris passed to a client builder (e.g.MultiDbClient.builder().connectionProvider(myProvider).build()orRedisClient.builder().connectionProvider(p).build()), and that provider internally carries aCache,UnifiedJedis.getCache()returnsnull. The connections use client-side caching, but the client API reports none — the cache instance diverges.Where was the issue
In
AbstractClientBuilder.build(). The builder'scachefield is only populated via.cache()/.cacheConfig(). When a custom provider is injected via.connectionProvider(...), the builder skipscreateDefaultConnectionProvider()(which would have passed the builder's cache) and passesthis.cache(stillnull) directly to the client constructor. Meanwhile, the custom provider — built externally with a cache — creates cache-enabled connections, creating a mismatch.How it was fixed
Three small, orthogonal changes:
ConnectionProviderinterface — addeddefault Cache getCache()returningnull(safe for all existing implementations that don't use cache).Four provider implementations — each now stores and exposes its cache via
@Override getCache():MultiDbConnectionProvider— already had the field, just added the getterSentineledConnectionProvider— already had the field, just added the getterPooledConnectionProvider— now stores the cache (was previously passing it to the pool but discarding the reference)ClusterConnectionProvider— now stores a separateclientSideCachefield alongside the existingJedisClusterInfoCache cacheAbstractClientBuilder.build()— after the provider is finalised, ifthis.cacheis stillnull, pull it fromconnectionProvider.getCache(). This bridges the gap: a custom provider's cache now reachesUnifiedJedis.Why this approach
defaultmethod means all existingConnectionProviderimplementations compile unchanged.ConnectionProvideralready hasdefaultmethods (getConnectionMap,getPrimaryNodesConnectionMap); this follows the same pattern. The builder already has type-specific knowledge of providers; thegetCache()bridge is in that same spirit.ConnectionProviderimplementation that holds a cache can overridegetCache()and the builder will pick it up automatically.How to test locally