Prevent ValueLoaderEntryProcessor to be created for each get call
Closes gh-31250
This commit is contained in:
parent
f79bc7b14d
commit
2fbcff8919
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.cache.jcache;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.cache.Cache;
|
||||
import javax.cache.processor.EntryProcessor;
|
||||
|
|
@ -42,6 +43,8 @@ public class JCacheCache extends AbstractValueAdaptingCache {
|
|||
|
||||
private final Cache<Object, Object> cache;
|
||||
|
||||
private final ValueLoaderEntryProcessor valueLoaderEntryProcessor;
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@code JCacheCache} instance.
|
||||
|
|
@ -60,6 +63,8 @@ public class JCacheCache extends AbstractValueAdaptingCache {
|
|||
super(allowNullValues);
|
||||
Assert.notNull(jcache, "Cache must not be null");
|
||||
this.cache = jcache;
|
||||
this.valueLoaderEntryProcessor = new ValueLoaderEntryProcessor(
|
||||
this::fromStoreValue, this::toStoreValue);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -81,9 +86,10 @@ public class JCacheCache extends AbstractValueAdaptingCache {
|
|||
|
||||
@Override
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(Object key, Callable<T> valueLoader) {
|
||||
try {
|
||||
return this.cache.invoke(key, new ValueLoaderEntryProcessor<T>(), valueLoader);
|
||||
return (T) this.cache.invoke(key, this.valueLoaderEntryProcessor, valueLoader);
|
||||
}
|
||||
catch (EntryProcessorException ex) {
|
||||
throw new ValueRetrievalException(key, valueLoader, ex.getCause());
|
||||
|
|
@ -141,18 +147,29 @@ public class JCacheCache extends AbstractValueAdaptingCache {
|
|||
}
|
||||
|
||||
|
||||
private class ValueLoaderEntryProcessor<T> implements EntryProcessor<Object, Object, T> {
|
||||
private static final class ValueLoaderEntryProcessor implements EntryProcessor<Object, Object, Object> {
|
||||
|
||||
private final Function<Object, Object> fromStoreValue;
|
||||
|
||||
private final Function<Object, Object> toStoreValue;
|
||||
|
||||
private ValueLoaderEntryProcessor(Function<Object, Object> fromStoreValue,
|
||||
Function<Object, Object> toStoreValue) {
|
||||
|
||||
this.fromStoreValue = fromStoreValue;
|
||||
this.toStoreValue = toStoreValue;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
@Nullable
|
||||
public T process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
|
||||
Callable<T> valueLoader = (Callable<T>) arguments[0];
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
|
||||
Callable<Object> valueLoader = (Callable<Object>) arguments[0];
|
||||
if (entry.exists()) {
|
||||
return (T) fromStoreValue(entry.getValue());
|
||||
return this.fromStoreValue.apply(entry.getValue());
|
||||
}
|
||||
else {
|
||||
T value;
|
||||
Object value;
|
||||
try {
|
||||
value = valueLoader.call();
|
||||
}
|
||||
|
|
@ -160,7 +177,7 @@ public class JCacheCache extends AbstractValueAdaptingCache {
|
|||
throw new EntryProcessorException("Value loader '" + valueLoader + "' failed " +
|
||||
"to compute value for key '" + entry.getKey() + "'", ex);
|
||||
}
|
||||
entry.setValue(toStoreValue(value));
|
||||
entry.setValue(this.toStoreValue.apply(value));
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue