Browse Source

Polishing

(cherry picked from commit 3a9e0ea8a7)
pull/30971/head
Juergen Hoeller 3 years ago
parent
commit
c9849d6972
  1. 17
      spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,8 +59,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,7 +86,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)));
} }
@ -106,7 +106,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));
@ -140,7 +140,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;
@ -159,16 +159,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);
} }
} }
} }

Loading…
Cancel
Save