You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The existing implementation of the get method in the ClientSideCaching class fails to provide a mechanism that guarantees the exclusive access of a single thread to fetch a value from the Redis server for a specific key.
Consequently, under high concurrent load scenarios involving the same key, the current implementation may cause redundant and unnecessary calls to the Redis server.
@OverridepublicVget(Kkey) {
Vvalue = cacheAccessor.get(key);
if (value == null) {
value = redisCache.get(key);
if (value != null) {
cacheAccessor.put(key, value);
}
}
returnvalue;
}
Describe the solution you'd like
Utilize a local locking mechanism to guarantee that at any given time, only a single thread is permitted to retrieve a value from the Redis cache for a particular key.
privatefinalConcurrentHashMap<K, ReentrantLock> keyLocks = newConcurrentHashMap<>();
publicVget(Kkey) {
Vvalue = cacheAccessor.get(key);
if (value == null) {
// Acquire a lock for this keyReentrantLocklock = keyLocks.computeIfAbsent(key, k -> newReentrantLock());
lock.lock();
try {
// Double-check to see if the value has been added by another threadvalue = cacheAccessor.get(key);
// If the value is not in the cache, fetch it from Redis server and set it in client-cacheif (value == null) {
value = redisCache.get(key);
if (value != null) {
cacheAccessor.put(key, value);
}
}
} finally {
lock.unlock();
}
}
returnvalue;
}
The text was updated successfully, but these errors were encountered:
Added per-key locking mechanism to ensure that only a single thread
fetches a value from Redis cache for a specific key. This prevents
redundant Redis server calls under high concurrent load.
Feature Request
The existing implementation of the get method in the ClientSideCaching class fails to provide a mechanism that guarantees the exclusive access of a single thread to fetch a value from the Redis server for a specific key.
Consequently, under high concurrent load scenarios involving the same key, the current implementation may cause redundant and unnecessary calls to the Redis server.
Current implementation get:
Describe the solution you'd like
Utilize a local locking mechanism to guarantee that at any given time, only a single thread is permitted to retrieve a value from the Redis cache for a particular key.
The text was updated successfully, but these errors were encountered: