Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main/java/io/lettuce/core/support/caching/CacheAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
* @author Mark Paluch, Ivar Henckel
* @since 6.0
*/
public interface CacheAccessor<K, V> {
Expand All @@ -22,7 +22,7 @@ public interface CacheAccessor<K, V> {
* @return a {@link CacheAccessor} backed by a {@link Map} implementation.
*/
static <K, V> CacheAccessor<K, V> forMap(Map<K, V> map) {
return new MapCacheAccessor<>(map);
return new PendingAwareCacheAccessor<>(map);
}

/**
Expand Down Expand Up @@ -59,4 +59,16 @@ static <K, V> CacheAccessor<K, V> forMap(Map<K, V> map) {
*/
void evict(K key);

/**
* Mark a key as pending.
* <p>
* By marking keys as pending, i.e. Redis get operation in progress, it's possible to block cache updates that would
* otherwise overwrite simultaneous invalidates.
*
* @param key the key to be marked as pending.
*/
default void setPending(K key) {
// Do nothing.
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public V get(K key) {
V value = cacheAccessor.get(key);

if (value == null) {
cacheAccessor.setPending(key);
value = redisCache.get(key);

if (value != null) {
Expand All @@ -140,6 +141,7 @@ public V get(K key, Callable<V> valueLoader) {
V value = cacheAccessor.get(key);

if (value == null) {
cacheAccessor.setPending(key);
value = redisCache.get(key);

if (value == null) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.lettuce.core.support.caching;

import java.util.Map;

/**
* {@link CacheAccessor} implementation for {@link Map}-based cache implementations.
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch, Ivar Henckel
* @since 6.0
*/
class PendingAwareCacheAccessor<K, V> implements CacheAccessor<K, V> {

private static final Object REDIS_IN_PROGRESS = new Object();

private final Map<K, V> map;

PendingAwareCacheAccessor(Map<K, V> map) {
this.map = map;
}

@Override
public V get(K key) {
V value = map.get(key);
if (value == REDIS_IN_PROGRESS) {
return null;
}
return value;
}

@Override
public void put(K key, V value) {
map.replace(key, (V) REDIS_IN_PROGRESS, value);
}

@Override
public void evict(K key) {
map.remove(key);
}

@Override
public void setPending(K key) {
map.put(key, (V) REDIS_IN_PROGRESS);
}

}