Skip to content

Commit 2fbcff8

Browse files
committed
Prevent ValueLoaderEntryProcessor to be created for each get call
Closes gh-31250
1 parent f79bc7b commit 2fbcff8

File tree

1 file changed

+25
-8
lines changed
  • spring-context-support/src/main/java/org/springframework/cache/jcache

1 file changed

+25
-8
lines changed

spring-context-support/src/main/java/org/springframework/cache/jcache/JCacheCache.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.cache.jcache;
1818

1919
import java.util.concurrent.Callable;
20+
import java.util.function.Function;
2021

2122
import javax.cache.Cache;
2223
import javax.cache.processor.EntryProcessor;
@@ -42,6 +43,8 @@ public class JCacheCache extends AbstractValueAdaptingCache {
4243

4344
private final Cache<Object, Object> cache;
4445

46+
private final ValueLoaderEntryProcessor valueLoaderEntryProcessor;
47+
4548

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

6570

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

8287
@Override
8388
@Nullable
89+
@SuppressWarnings("unchecked")
8490
public <T> T get(Object key, Callable<T> valueLoader) {
8591
try {
86-
return this.cache.invoke(key, new ValueLoaderEntryProcessor<T>(), valueLoader);
92+
return (T) this.cache.invoke(key, this.valueLoaderEntryProcessor, valueLoader);
8793
}
8894
catch (EntryProcessorException ex) {
8995
throw new ValueRetrievalException(key, valueLoader, ex.getCause());
@@ -141,26 +147,37 @@ public Object process(MutableEntry<Object, Object> entry, Object... arguments) t
141147
}
142148

143149

144-
private class ValueLoaderEntryProcessor<T> implements EntryProcessor<Object, Object, T> {
150+
private static final class ValueLoaderEntryProcessor implements EntryProcessor<Object, Object, Object> {
151+
152+
private final Function<Object, Object> fromStoreValue;
153+
154+
private final Function<Object, Object> toStoreValue;
155+
156+
private ValueLoaderEntryProcessor(Function<Object, Object> fromStoreValue,
157+
Function<Object, Object> toStoreValue) {
158+
159+
this.fromStoreValue = fromStoreValue;
160+
this.toStoreValue = toStoreValue;
161+
}
145162

146-
@SuppressWarnings("unchecked")
147163
@Override
148164
@Nullable
149-
public T process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
150-
Callable<T> valueLoader = (Callable<T>) arguments[0];
165+
@SuppressWarnings("unchecked")
166+
public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
167+
Callable<Object> valueLoader = (Callable<Object>) arguments[0];
151168
if (entry.exists()) {
152-
return (T) fromStoreValue(entry.getValue());
169+
return this.fromStoreValue.apply(entry.getValue());
153170
}
154171
else {
155-
T value;
172+
Object value;
156173
try {
157174
value = valueLoader.call();
158175
}
159176
catch (Exception ex) {
160177
throw new EntryProcessorException("Value loader '" + valueLoader + "' failed " +
161178
"to compute value for key '" + entry.getKey() + "'", ex);
162179
}
163-
entry.setValue(toStoreValue(value));
180+
entry.setValue(this.toStoreValue.apply(value));
164181
return value;
165182
}
166183
}

0 commit comments

Comments
 (0)