diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java index a19884b19c..ef8c3b03e5 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java @@ -83,16 +83,6 @@ public class CaffeineCache extends AbstractValueAdaptingCache { return this.cache; } - @Override - @Nullable - public ValueWrapper get(Object key) { - if (this.cache instanceof LoadingCache) { - Object value = ((LoadingCache) this.cache).get(key); - return toValueWrapper(value); - } - return super.get(key); - } - @SuppressWarnings("unchecked") @Override @Nullable @@ -103,6 +93,9 @@ public class CaffeineCache extends AbstractValueAdaptingCache { @Override @Nullable protected Object lookup(Object key) { + if (this.cache instanceof LoadingCache) { + return ((LoadingCache) this.cache).get(key); + } return this.cache.getIfPresent(key); } diff --git a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java index 48e1fd5a93..9c66abc646 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,9 +21,11 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.cache.Cache; +import org.springframework.cache.Cache.ValueWrapper; import org.springframework.context.testfixture.cache.AbstractValueAdaptingCacheTests; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * @author Ben Manes @@ -61,6 +63,34 @@ public class CaffeineCacheTests extends AbstractValueAdaptingCacheTests value)); + ValueWrapper valueWrapper = loadingCache.get(new Object()); + assertThat(valueWrapper).isNotNull(); + assertThat(valueWrapper.get()).isEqualTo(value); + } + + @Test + void testLoadingCacheGetWithType() { + String value = "value"; + CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder() + .build(key -> value)); + String valueWrapper = loadingCache.get(new Object(), String.class); + assertThat(valueWrapper).isNotNull(); + assertThat(valueWrapper).isEqualTo(value); + } + + @Test + void testLoadingCacheGetWithWrongType() { + String value = "value"; + CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder() + .build(key -> value)); + assertThatIllegalStateException().isThrownBy(() -> loadingCache.get(new Object(), Long.class)); + } + @Test public void testPutIfAbsentNullValue() throws Exception { CaffeineCache cache = getCache();