Skip to content

Commit f3cedf2

Browse files
committed
Consistently support LoadingCache
This commits make sure that CaffeineCache handles consistently the contract of LoadingCache. Closes gh-25173
1 parent b6ef3cf commit f3cedf2

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

Diff for: spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ public final com.github.benmanes.caffeine.cache.Cache<Object, Object> getNativeC
8383
return this.cache;
8484
}
8585

86-
@Override
87-
@Nullable
88-
public ValueWrapper get(Object key) {
89-
if (this.cache instanceof LoadingCache) {
90-
Object value = ((LoadingCache<Object, Object>) this.cache).get(key);
91-
return toValueWrapper(value);
92-
}
93-
return super.get(key);
94-
}
95-
9686
@SuppressWarnings("unchecked")
9787
@Override
9888
@Nullable
@@ -103,6 +93,9 @@ public <T> T get(Object key, final Callable<T> valueLoader) {
10393
@Override
10494
@Nullable
10595
protected Object lookup(Object key) {
96+
if (this.cache instanceof LoadingCache) {
97+
return ((LoadingCache<Object, Object>) this.cache).get(key);
98+
}
10699
return this.cache.getIfPresent(key);
107100
}
108101

Diff for: spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,9 +21,11 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.cache.Cache;
24+
import org.springframework.cache.Cache.ValueWrapper;
2425
import org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2729

2830
/**
2931
* @author Ben Manes
@@ -61,6 +63,34 @@ protected Object getNativeCache() {
6163
return nativeCache;
6264
}
6365

66+
@Test
67+
void testLoadingCacheGet() {
68+
Object value = new Object();
69+
CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder()
70+
.build(key -> value));
71+
ValueWrapper valueWrapper = loadingCache.get(new Object());
72+
assertThat(valueWrapper).isNotNull();
73+
assertThat(valueWrapper.get()).isEqualTo(value);
74+
}
75+
76+
@Test
77+
void testLoadingCacheGetWithType() {
78+
String value = "value";
79+
CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder()
80+
.build(key -> value));
81+
String valueWrapper = loadingCache.get(new Object(), String.class);
82+
assertThat(valueWrapper).isNotNull();
83+
assertThat(valueWrapper).isEqualTo(value);
84+
}
85+
86+
@Test
87+
void testLoadingCacheGetWithWrongType() {
88+
String value = "value";
89+
CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder()
90+
.build(key -> value));
91+
assertThatIllegalStateException().isThrownBy(() -> loadingCache.get(new Object(), Long.class));
92+
}
93+
6494
@Test
6595
public void testPutIfAbsentNullValue() throws Exception {
6696
CaffeineCache cache = getCache();

0 commit comments

Comments
 (0)