Skip to content

Commit

Permalink
Prevent ValueLoaderEntryProcessor to be created for each get call
Browse files Browse the repository at this point in the history
Closes gh-31250
  • Loading branch information
snicoll committed Sep 18, 2023
1 parent f79bc7b commit 2fbcff8
Showing 1 changed file with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +43,8 @@ public class JCacheCache extends AbstractValueAdaptingCache {

private final Cache<Object, Object> cache;

private final ValueLoaderEntryProcessor valueLoaderEntryProcessor;


/**
* Create a {@code JCacheCache} instance.
Expand All @@ -60,6 +63,8 @@ public JCacheCache(Cache<Object, Object> jcache, boolean allowNullValues) {
super(allowNullValues);
Assert.notNull(jcache, "Cache must not be null");
this.cache = jcache;
this.valueLoaderEntryProcessor = new ValueLoaderEntryProcessor(
this::fromStoreValue, this::toStoreValue);
}


Expand All @@ -81,9 +86,10 @@ protected Object lookup(Object key) {

@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());
Expand Down Expand Up @@ -141,26 +147,37 @@ public Object process(MutableEntry<Object, Object> entry, Object... arguments) t
}


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();
}
catch (Exception ex) {
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;
}
}
Expand Down

0 comments on commit 2fbcff8

Please sign in to comment.