Browse Source

Polishing

pull/30459/head
Juergen Hoeller 3 years ago
parent
commit
ba46d2bf21
  1. 22
      spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java
  2. 4
      spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java

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

@ -69,8 +69,8 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
* given internal {@link com.github.benmanes.caffeine.cache.Cache} to use. * given internal {@link com.github.benmanes.caffeine.cache.Cache} to use.
* @param name the name of the cache * @param name the name of the cache
* @param cache the backing Caffeine Cache instance * @param cache the backing Caffeine Cache instance
* @param allowNullValues whether to accept and convert {@code null} * @param allowNullValues whether to accept and convert {@code null} values
* values for this cache * for this cache
*/ */
public CaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Object, Object> cache, public CaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Object, Object> cache,
boolean allowNullValues) { boolean allowNullValues) {
@ -86,9 +86,9 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
* Create a {@link CaffeineCache} instance with the specified name and the * Create a {@link CaffeineCache} instance with the specified name and the
* given internal {@link AsyncCache} to use. * given internal {@link AsyncCache} to use.
* @param name the name of the cache * @param name the name of the cache
* @param cache the backing Caffeine Cache instance * @param cache the backing Caffeine AsyncCache instance
* @param allowNullValues whether to accept and convert {@code null} * @param allowNullValues whether to accept and convert {@code null} values
* values for this cache * for this cache
* @since 6.1 * @since 6.1
*/ */
public CaffeineCache(String name, AsyncCache<Object, Object> cache, boolean allowNullValues) { public CaffeineCache(String name, AsyncCache<Object, Object> cache, boolean allowNullValues) {
@ -118,6 +118,7 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
/** /**
* Return the internal Caffeine AsyncCache. * Return the internal Caffeine AsyncCache.
* @throws IllegalStateException if no AsyncCache is available * @throws IllegalStateException if no AsyncCache is available
* @since 6.1
* @see #CaffeineCache(String, AsyncCache, boolean) * @see #CaffeineCache(String, AsyncCache, boolean)
* @see CaffeineCacheManager#setAsyncCacheMode * @see CaffeineCacheManager#setAsyncCacheMode
*/ */
@ -130,7 +131,7 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
@Nullable @Nullable
public <T> T get(Object key, final Callable<T> valueLoader) { public <T> T get(Object key, Callable<T> valueLoader) {
return (T) fromStoreValue(this.cache.get(key, new LoadFunction(valueLoader))); return (T) fromStoreValue(this.cache.get(key, new LoadFunction(valueLoader)));
} }
@ -166,7 +167,7 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
@Override @Override
@Nullable @Nullable
public ValueWrapper putIfAbsent(Object key, @Nullable final Object value) { public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
PutIfAbsentFunction callable = new PutIfAbsentFunction(value); PutIfAbsentFunction callable = new PutIfAbsentFunction(value);
Object result = this.cache.get(key, callable); Object result = this.cache.get(key, callable);
return (callable.called ? null : toValueWrapper(result)); return (callable.called ? null : toValueWrapper(result));
@ -200,7 +201,7 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
@Nullable @Nullable
private final Object value; private final Object value;
private boolean called; boolean called;
public PutIfAbsentFunction(@Nullable Object value) { public PutIfAbsentFunction(@Nullable Object value) {
this.value = value; this.value = value;
@ -219,16 +220,17 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
private final Callable<?> valueLoader; private final Callable<?> valueLoader;
public LoadFunction(Callable<?> valueLoader) { public LoadFunction(Callable<?> valueLoader) {
Assert.notNull(valueLoader, "Callable must not be null");
this.valueLoader = valueLoader; this.valueLoader = valueLoader;
} }
@Override @Override
public Object apply(Object o) { public Object apply(Object key) {
try { try {
return toStoreValue(this.valueLoader.call()); return toStoreValue(this.valueLoader.call());
} }
catch (Exception ex) { catch (Exception ex) {
throw new ValueRetrievalException(o, this.valueLoader, ex); throw new ValueRetrievalException(key, this.valueLoader, ex);
} }
} }
} }

4
spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java vendored

@ -276,12 +276,12 @@ public class CaffeineCacheManager implements CacheManager {
* <p>This allows for custom settings per cache (as opposed to all caches * <p>This allows for custom settings per cache (as opposed to all caches
* sharing the common settings in the cache manager's configuration) and * sharing the common settings in the cache manager's configuration) and
* is typically used with the Caffeine builder API: * is typically used with the Caffeine builder API:
* {@code registerCustomCache("myCache", Caffeine.newBuilder().maximumSize(10).build())} * {@code registerCustomCache("myCache", Caffeine.newBuilder().maximumSize(10).buildAsync())}
* <p>Note that any other caches, whether statically specified through * <p>Note that any other caches, whether statically specified through
* {@link #setCacheNames} or dynamically built on demand, still operate * {@link #setCacheNames} or dynamically built on demand, still operate
* with the common settings in the cache manager's configuration. * with the common settings in the cache manager's configuration.
* @param name the name of the cache * @param name the name of the cache
* @param cache the custom Caffeine Cache instance to register * @param cache the custom Caffeine AsyncCache instance to register
* @since 6.1 * @since 6.1
* @see #adaptCaffeineCache(String, AsyncCache) * @see #adaptCaffeineCache(String, AsyncCache)
*/ */

Loading…
Cancel
Save