@@ -206,34 +206,33 @@ private static class CacheSegment<K, V> {
206
206
*/
207
207
Entry <K , V > get (K key , long now , Predicate <Entry <K , V >> isExpired , Consumer <Entry <K , V >> onExpiration ) {
208
208
CompletableFuture <Entry <K , V >> future ;
209
- Entry <K , V > entry = null ;
210
209
try (ReleasableLock ignored = readLock .acquire ()) {
211
210
future = map .get (key );
212
211
}
213
212
if (future != null ) {
213
+ Entry <K , V > entry ;
214
214
try {
215
- entry = future .handle ((ok , ex ) -> {
216
- if (ok != null && !isExpired .test (ok )) {
217
- segmentStats .hit ();
218
- ok .accessTime = now ;
219
- return ok ;
220
- } else {
221
- segmentStats .miss ();
222
- if (ok != null ) {
223
- assert isExpired .test (ok );
224
- onExpiration .accept (ok );
225
- }
226
- return null ;
227
- }
228
- }).get ();
229
- } catch (ExecutionException | InterruptedException e ) {
215
+ entry = future .get ();
216
+ } catch (ExecutionException e ) {
217
+ assert future .isCompletedExceptionally ();
218
+ segmentStats .miss ();
219
+ return null ;
220
+ } catch (InterruptedException e ) {
230
221
throw new IllegalStateException (e );
231
222
}
232
- }
233
- else {
223
+ if (isExpired .test (entry )) {
224
+ segmentStats .miss ();
225
+ onExpiration .accept (entry );
226
+ return null ;
227
+ } else {
228
+ segmentStats .hit ();
229
+ entry .accessTime = now ;
230
+ return entry ;
231
+ }
232
+ } else {
234
233
segmentStats .miss ();
234
+ return null ;
235
235
}
236
- return entry ;
237
236
}
238
237
239
238
/**
@@ -269,30 +268,18 @@ Tuple<Entry<K, V>, Entry<K, V>> put(K key, V value, long now) {
269
268
/**
270
269
* remove an entry from the segment
271
270
*
272
- * @param key the key of the entry to remove from the cache
273
- * @return the removed entry if there was one, otherwise null
271
+ * @param key the key of the entry to remove from the cache
272
+ * @param onRemoval a callback for the removed entry
274
273
*/
275
- Entry <K , V > remove ( K key ) {
274
+ void remove ( K key , Consumer < CompletableFuture < Entry <K , V >>> onRemoval ) {
276
275
CompletableFuture <Entry <K , V >> future ;
277
- Entry <K , V > entry = null ;
278
276
try (ReleasableLock ignored = writeLock .acquire ()) {
279
277
future = map .remove (key );
280
278
}
281
279
if (future != null ) {
282
- try {
283
- entry = future .handle ((ok , ex ) -> {
284
- if (ok != null ) {
285
- segmentStats .eviction ();
286
- return ok ;
287
- } else {
288
- return null ;
289
- }
290
- }).get ();
291
- } catch (ExecutionException | InterruptedException e ) {
292
- throw new IllegalStateException (e );
293
- }
280
+ segmentStats .eviction ();
281
+ onRemoval .accept (future );
294
282
}
295
- return entry ;
296
283
}
297
284
298
285
private static class SegmentStats {
@@ -476,12 +463,18 @@ private void put(K key, V value, long now) {
476
463
*/
477
464
public void invalidate (K key ) {
478
465
CacheSegment <K , V > segment = getCacheSegment (key );
479
- Entry <K , V > entry = segment .remove (key );
480
- if (entry != null ) {
481
- try (ReleasableLock ignored = lruLock .acquire ()) {
482
- delete (entry , RemovalNotification .RemovalReason .INVALIDATED );
466
+ segment .remove (key , f -> {
467
+ try {
468
+ Entry <K , V > entry = f .get ();
469
+ try (ReleasableLock ignored = lruLock .acquire ()) {
470
+ delete (entry , RemovalNotification .RemovalReason .INVALIDATED );
471
+ }
472
+ } catch (ExecutionException e ) {
473
+ // ok
474
+ } catch (InterruptedException e ) {
475
+ throw new IllegalStateException (e );
483
476
}
484
- }
477
+ });
485
478
}
486
479
487
480
/**
@@ -632,7 +625,7 @@ public void remove() {
632
625
Entry <K , V > entry = current ;
633
626
if (entry != null ) {
634
627
CacheSegment <K , V > segment = getCacheSegment (entry .key );
635
- segment .remove (entry .key );
628
+ segment .remove (entry .key , f -> {} );
636
629
try (ReleasableLock ignored = lruLock .acquire ()) {
637
630
current = null ;
638
631
delete (entry , RemovalNotification .RemovalReason .INVALIDATED );
@@ -717,7 +710,7 @@ private void evictEntry(Entry<K, V> entry) {
717
710
718
711
CacheSegment <K , V > segment = getCacheSegment (entry .key );
719
712
if (segment != null ) {
720
- segment .remove (entry .key );
713
+ segment .remove (entry .key , f -> {} );
721
714
}
722
715
delete (entry , RemovalNotification .RemovalReason .EVICTED );
723
716
}
0 commit comments