Enhance thread-safety in ClientSideCaching key retrieval
Issue
Closes #2402
The current implementation of the get method in the ClientSideCaching class does not provide a mechanism to ensure exclusive access by a single thread when retrieving a value from the Redis server for a specific key. As a result, in high concurrent load scenarios involving the same key, the current implementation may cause redundant and unnecessary calls to the Redis server.
Solution
Added a per-key locking mechanism using ReentrantLock to ensure that only a single thread can fetch a value from Redis for a specific key at any given time.
Tests
-
valueLoaderShouldBeInvokedOnceForConcurrentRequests: Verifies that when multiple threads concurrently access the same key using a valueLoader, the loader is called exactly once -
locksShouldBeProperlyCleanedUp: Verifies the proper lifecycle management of locks (creation, cleanup on invalidation, recreation, and final cleanup)
Hey @yybmion could you please format your changes using mvn formatter:format
Hi @tishun. Thank you for pointing out the formatting issues - I was having trouble with CRLF/LF line endings.
I've applied the formatting changes as requested using mvn formatter:format.
Hi @tishun, Just a gentle reminder — PTAL when you have time.