Lookups from computeIfAbsent should propogate now

This commit is contained in:
Jason Tedor 2015-10-01 00:47:08 +02:00
parent 30bfea741e
commit 7a5e90fc45
1 changed files with 6 additions and 3 deletions

View File

@ -257,7 +257,10 @@ public class Cache<K, V> {
* @return the value to which the specified key is mapped, or null if this map contains no mapping for the key * @return the value to which the specified key is mapped, or null if this map contains no mapping for the key
*/ */
public V get(K key) { public V get(K key) {
long now = now(); return get(key, now());
}
private V get(K key, long now) {
CacheSegment<K, V> segment = getCacheSegment(key); CacheSegment<K, V> segment = getCacheSegment(key);
Entry<K, V> entry = segment.get(key, now); Entry<K, V> entry = segment.get(key, now);
if (entry == null || isExpired(entry, now)) { if (entry == null || isExpired(entry, now)) {
@ -279,11 +282,11 @@ public class Cache<K, V> {
*/ */
public V computeIfAbsent(K key, Function<K, V> mappingFunction) { public V computeIfAbsent(K key, Function<K, V> mappingFunction) {
long now = now(); long now = now();
V value = get(key); V value = get(key, now);
if (value == null) { if (value == null) {
CacheSegment<K, V> segment = getCacheSegment(key); CacheSegment<K, V> segment = getCacheSegment(key);
try (ReleasableLock ignored = segment.writeLock.acquire()) { try (ReleasableLock ignored = segment.writeLock.acquire()) {
value = get(key); value = get(key, now);
if (value == null) { if (value == null) {
value = mappingFunction.apply(key); value = mappingFunction.apply(key);
} }