@@ -261,31 +261,26 @@ public V get(ICacheKey<K> key) {
261
261
public void put (ICacheKey <K > key , V value ) {
262
262
// First check in case the key is already present in either of tiers.
263
263
Tuple <V , String > cacheValueTuple = getValueFromTieredCache (true ).apply (key );
264
- if (cacheValueTuple == null ) {
265
- // In case it is not present in any tier, put it inside onHeap cache by default.
266
- if ( evaluatePoliciesList ( value , policies )) {
264
+ if (evaluatePoliciesList ( value , policies ) ) {
265
+ if ( cacheValueTuple == null ) {
266
+ // In case it is not present in any tier, put it inside onHeap cache by default.
267
267
try (ReleasableLock ignore = writeLock .acquire ()) {
268
268
onHeapCache .put (key , value );
269
269
}
270
270
updateStatsOnPut (TIER_DIMENSION_VALUE_ON_HEAP , key , value );
271
271
} else {
272
- // Signal to the caller that the key didn't enter the cache by sending a removal notification.
273
- removalListener .onRemoval (new RemovalNotification <>(key , value , RemovalReason .EXPLICIT ));
274
- }
275
- } else {
276
- // Put it inside desired tier.
277
- if (evaluatePoliciesList (value , policies )) {
278
272
try (ReleasableLock ignore = writeLock .acquire ()) {
279
273
for (Map .Entry <ICache <K , V >, TierInfo > entry : this .caches .entrySet ()) {
280
274
if (cacheValueTuple .v2 ().equals (entry .getValue ().tierName )) {
281
275
entry .getKey ().put (key , value );
282
276
}
283
277
}
284
- updateStatsOnPut (cacheValueTuple .v2 (), key , value );
285
278
}
286
- } else {
287
- removalListener .onRemoval (new RemovalNotification <>(key , value , RemovalReason .EXPLICIT ));
279
+ updateStatsOnPut (cacheValueTuple .v2 (), key , value );
288
280
}
281
+ } else {
282
+ // Signal to the caller that the key didn't enter the cache by sending a removal notification.
283
+ removalListener .onRemoval (new RemovalNotification <>(key , value , RemovalReason .EXPLICIT ));
289
284
}
290
285
}
291
286
@@ -350,15 +345,19 @@ private Tuple<V, Boolean> compute(
350
345
boolean wasCacheMiss = false ;
351
346
BiFunction <Tuple <ICacheKey <K >, V >, Throwable , Void > handler = (pair , ex ) -> {
352
347
if (pair != null ) {
348
+ boolean didAddToCache = false ;
353
349
try (ReleasableLock ignore = writeLock .acquire ()) {
354
350
onHeapCache .put (pair .v1 (), pair .v2 ());
351
+ didAddToCache = true ;
355
352
} catch (Exception e ) {
356
353
// TODO: Catch specific exceptions to know whether this resulted from cache or underlying removal
357
354
// listeners/stats. Needs better exception handling at underlying layers.For now swallowing
358
355
// exception.
359
356
logger .warn ("Exception occurred while putting item onto heap cache" , e );
360
357
}
361
- updateStatsOnPut (TIER_DIMENSION_VALUE_ON_HEAP , key , pair .v2 ());
358
+ if (didAddToCache ) {
359
+ updateStatsOnPut (TIER_DIMENSION_VALUE_ON_HEAP , key , pair .v2 ());
360
+ }
362
361
} else {
363
362
if (ex != null ) {
364
363
logger .warn ("Exception occurred while trying to compute the value" , ex );
0 commit comments