Skip to content

Commit 9172a6d

Browse files
committed
Restore proper use of CacheLoader
Since Guava 11, CacheLoader is only invoked with a LoadingCache but the GuavaCache wrapper is always invoking getIfPresent(), available on the main Guava Cache interface. Update GuavaCache#get to check for the presence of a LoadingCache and call the appropriate method. Issue: SPR-12842
1 parent 7513bd5 commit 9172a6d

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

spring-context-support/src/main/java/org/springframework/cache/guava/GuavaCache.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -20,6 +20,9 @@
2020
import java.util.concurrent.Callable;
2121
import java.util.concurrent.ExecutionException;
2222

23+
import com.google.common.cache.LoadingCache;
24+
import com.google.common.util.concurrent.UncheckedExecutionException;
25+
2326
import org.springframework.cache.Cache;
2427
import org.springframework.cache.support.SimpleValueWrapper;
2528
import org.springframework.util.Assert;
@@ -88,8 +91,16 @@ public final boolean isAllowNullValues() {
8891

8992
@Override
9093
public ValueWrapper get(Object key) {
91-
Object value = this.cache.getIfPresent(key);
92-
return toWrapper(value);
94+
if (this.cache instanceof LoadingCache) {
95+
try {
96+
Object value = ((LoadingCache<Object, Object>) this.cache).get(key);
97+
return toWrapper(value);
98+
}
99+
catch (ExecutionException ex) {
100+
throw new UncheckedExecutionException(ex.getMessage(), ex);
101+
}
102+
}
103+
return toWrapper(this.cache.getIfPresent(key));
93104
}
94105

95106
@Override

spring-context-support/src/test/java/org/springframework/cache/guava/GuavaCacheManagerTests.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -18,7 +18,10 @@
1818

1919
import com.google.common.cache.CacheBuilder;
2020
import com.google.common.cache.CacheLoader;
21+
import com.google.common.util.concurrent.UncheckedExecutionException;
22+
import org.junit.Rule;
2123
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
2225

2326
import org.springframework.cache.Cache;
2427
import org.springframework.cache.CacheManager;
@@ -32,6 +35,9 @@
3235
*/
3336
public class GuavaCacheManagerTests {
3437

38+
@Rule
39+
public final ExpectedException thrown = ExpectedException.none();
40+
3541
@Test
3642
public void testDynamicMode() {
3743
CacheManager cm = new GuavaCacheManager();
@@ -150,6 +156,28 @@ public void setCacheNameNullRestoreDynamicMode() {
150156
assertNotNull(cm.getCache("someCache"));
151157
}
152158

159+
@Test
160+
public void cacheLoaderUseLoadingCache() {
161+
GuavaCacheManager cm = new GuavaCacheManager("c1");
162+
cm.setCacheLoader(new CacheLoader<Object, Object>() {
163+
@Override
164+
public Object load(Object key) throws Exception {
165+
if ("ping".equals(key)) {
166+
return "pong";
167+
}
168+
throw new IllegalArgumentException("I only know ping");
169+
}
170+
});
171+
Cache cache1 = cm.getCache("c1");
172+
Cache.ValueWrapper value = cache1.get("ping");
173+
assertNotNull(value);
174+
assertEquals("pong", value.get());
175+
176+
thrown.expect(UncheckedExecutionException.class);
177+
thrown.expectMessage("I only know ping");
178+
assertNull(cache1.get("foo"));
179+
}
180+
153181
@SuppressWarnings("unchecked")
154182
private CacheLoader<Object, Object> mockCacheLoader() {
155183
return mock(CacheLoader.class);

0 commit comments

Comments
 (0)