Skip to content

Commit

Permalink
update caffeine dependency and fix compile errors
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schneider <johannes.schneider@bosch.io>
  • Loading branch information
jokraehe committed Aug 30, 2021
1 parent 7fb60f5 commit 9f3f8f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
-->
<js.long.version>3.2.0</js.long.version>

<caffeine.version>2.8.8</caffeine.version>
<caffeine.version>3.0.3</caffeine.version>
<classindex.version>3.8</classindex.version>

<!-- ### Testing dependencies versions -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class CaffeineCache<K, V> implements Cache<K, V> {

@Nullable private final MetricsStatsCounter metricStatsCounter;
private final BiFunction<? super K, Executor, CompletableFuture<V>> asyncLoad;
private final BiFunction<K, Executor, CompletableFuture<? extends V>> asyncLoad;
private final AsyncCache<K, V> asyncCache;
private final com.github.benmanes.caffeine.cache.Cache<K, V> synchronousCacheView;

Expand All @@ -54,11 +54,24 @@ private CaffeineCache(final Caffeine<? super K, ? super V> caffeine,
} else {
this.metricStatsCounter = null;
}
asyncLoad = loader != null ? loader::asyncLoad : (k, e) -> CompletableFuture.completedFuture(null);
asyncLoad = getLoaderFunction(loader);
this.asyncCache = loader != null ? caffeine.buildAsync(loader) : caffeine.buildAsync();
this.synchronousCacheView = asyncCache.synchronous();
}

private BiFunction<K, Executor, CompletableFuture<? extends V>> getLoaderFunction(
@Nullable final AsyncCacheLoader<K, V> loader) {
return loader != null ?
(k, e) -> {
try {
return loader.asyncLoad(k, e);
} catch (final Exception ex) {
return CompletableFuture.completedFuture(null);
}
} :
(k, e) -> CompletableFuture.completedFuture(null);
}

@SuppressWarnings({"squid:S2583", "ConstantConditions"})
private Long getCurrentCacheSize() {
if (synchronousCacheView == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.ditto.internal.utils.metrics.instruments.gauge.Gauge;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.PreparedTimer;

import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.github.benmanes.caffeine.cache.stats.StatsCounter;

Expand Down Expand Up @@ -112,7 +113,6 @@ public String toString() {
}
}

private static final int DEFAULT_EVICTION_WEIGHT = 1;
private static final String CACHE_NAME_TAG = "cache_name";

private final Counter hitCount;
Expand Down Expand Up @@ -143,7 +143,8 @@ private MetricsStatsCounter(final String cacheName, final Supplier<Long> maxSize
estimatedInvalidations =
DittoMetrics.counter(MetricName.ESTIMATED_INVALIDATIONS.getValue()).tag(CACHE_NAME_TAG, cacheName);
estimatedInvalidationsWithoutItem =
DittoMetrics.counter(MetricName.ESTIMATED_INVALIDATIONS_WITHOUT_ITEM.getValue()).tag(CACHE_NAME_TAG, cacheName);
DittoMetrics.counter(MetricName.ESTIMATED_INVALIDATIONS_WITHOUT_ITEM.getValue())
.tag(CACHE_NAME_TAG, cacheName);
this.maxSizeSupplier = maxSizeSupplier;
this.estimatedSizeSupplier = estimatedSizeSupplier;
}
Expand Down Expand Up @@ -185,13 +186,7 @@ public void recordLoadFailure(final long loadTimeInNanos) {
}

@Override
public void recordEviction() {
recordEviction(DEFAULT_EVICTION_WEIGHT);
updateCacheSizeMetrics();
}

@Override
public void recordEviction(final int weight) {
public void recordEviction(final int weight, final RemovalCause removalCause) {
evictionCount.increment();
evictionWeight.increment(weight);
updateCacheSizeMetrics();
Expand All @@ -211,7 +206,7 @@ void recordInvalidationWithoutItem() {

@Override
public CacheStats snapshot() {
return new CacheStats(
return CacheStats.of(
hitCount.getCount(),
missCount.getCount(),
loadSuccessCount.getCount(),
Expand Down

0 comments on commit 9f3f8f4

Please sign in to comment.