Browse Source

Consistently support LoadingCache

This commits make sure that CaffeineCache handles consistently the
contract of LoadingCache.

Closes gh-25173
pull/25566/head
Stephane Nicoll 5 years ago
parent
commit
f3cedf268b
  1. 13
      spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java
  2. 32
      spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java

13
spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java vendored

@ -83,16 +83,6 @@ public class CaffeineCache extends AbstractValueAdaptingCache { @@ -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 { @@ -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);
}

32
spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java vendored

@ -1,5 +1,5 @@ @@ -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; @@ -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 @@ -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…
Cancel
Save