Consistently support LoadingCache
This commits make sure that CaffeineCache handles consistently the contract of LoadingCache. Closes gh-25173
This commit is contained in:
parent
b6ef3cfad5
commit
f3cedf268b
|
@ -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<Object, Object>) 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<Object, Object>) this.cache).get(key);
|
||||
}
|
||||
return this.cache.getIfPresent(key);
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Caffeine
|
|||
return nativeCache;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoadingCacheGet() {
|
||||
Object value = new Object();
|
||||
CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder()
|
||||
.build(key -> 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();
|
||||
|
|
Loading…
Reference in New Issue