Skip to content

Commit f99faac

Browse files
committed
Add caching annotation support for CompletableFuture and reactive return values
Includes CompletableFuture-based retrieve operations on Spring's Cache interface. Includes support for retrieve operations on CaffeineCache and ConcurrentMapCache. Includes async cache mode option on CaffeineCacheManager. Closes gh-17559 Closes gh-17920 Closes gh-30122
1 parent d65d285 commit f99faac

File tree

9 files changed

+949
-142
lines changed

9 files changed

+949
-142
lines changed

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

+61-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package org.springframework.cache.caffeine;
1818

1919
import java.util.concurrent.Callable;
20+
import java.util.concurrent.CompletableFuture;
2021
import java.util.function.Function;
22+
import java.util.function.Supplier;
2123

24+
import com.github.benmanes.caffeine.cache.AsyncCache;
2225
import com.github.benmanes.caffeine.cache.LoadingCache;
2326

2427
import org.springframework.cache.support.AbstractValueAdaptingCache;
@@ -29,7 +32,11 @@
2932
* Spring {@link org.springframework.cache.Cache} adapter implementation
3033
* on top of a Caffeine {@link com.github.benmanes.caffeine.cache.Cache} instance.
3134
*
32-
* <p>Requires Caffeine 2.1 or higher.
35+
* <p>Supports the {@link #retrieve(Object)} and {@link #retrieve(Object, Supplier)}
36+
* operations through Caffeine's {@link AsyncCache}, when provided via the
37+
* {@link #CaffeineCache(String, AsyncCache, boolean)} constructor.
38+
*
39+
* <p>Requires Caffeine 3.0 or higher, as of Spring Framework 6.1.
3340
*
3441
* @author Ben Manes
3542
* @author Juergen Hoeller
@@ -43,6 +50,9 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
4350

4451
private final com.github.benmanes.caffeine.cache.Cache<Object, Object> cache;
4552

53+
@Nullable
54+
private AsyncCache<Object, Object> asyncCache;
55+
4656

4757
/**
4858
* Create a {@link CaffeineCache} instance with the specified name and the
@@ -72,24 +82,74 @@ public CaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Objec
7282
this.cache = cache;
7383
}
7484

85+
/**
86+
* Create a {@link CaffeineCache} instance with the specified name and the
87+
* given internal {@link AsyncCache} to use.
88+
* @param name the name of the cache
89+
* @param cache the backing Caffeine Cache instance
90+
* @param allowNullValues whether to accept and convert {@code null}
91+
* values for this cache
92+
* @since 6.1
93+
*/
94+
public CaffeineCache(String name, AsyncCache<Object, Object> cache, boolean allowNullValues) {
95+
super(allowNullValues);
96+
Assert.notNull(name, "Name must not be null");
97+
Assert.notNull(cache, "Cache must not be null");
98+
this.name = name;
99+
this.cache = cache.synchronous();
100+
this.asyncCache = cache;
101+
}
102+
75103

76104
@Override
77105
public final String getName() {
78106
return this.name;
79107
}
80108

109+
/**
110+
* Return the internal Caffeine Cache
111+
* (possibly an adapter on top of an {@link #getAsyncCache()}).
112+
*/
81113
@Override
82114
public final com.github.benmanes.caffeine.cache.Cache<Object, Object> getNativeCache() {
83115
return this.cache;
84116
}
85117

118+
/**
119+
* Return the internal Caffeine AsyncCache.
120+
* @throws IllegalStateException if no AsyncCache is available
121+
* @see #CaffeineCache(String, AsyncCache, boolean)
122+
* @see CaffeineCacheManager#setAsyncCacheMode
123+
*/
124+
public final AsyncCache<Object, Object> getAsyncCache() {
125+
Assert.state(this.asyncCache != null,
126+
"No Caffeine AsyncCache available: set CaffeineCacheManager.setAsyncCacheMode(true)");
127+
return this.asyncCache;
128+
}
129+
86130
@SuppressWarnings("unchecked")
87131
@Override
88132
@Nullable
89133
public <T> T get(Object key, final Callable<T> valueLoader) {
90134
return (T) fromStoreValue(this.cache.get(key, new LoadFunction(valueLoader)));
91135
}
92136

137+
@Override
138+
@Nullable
139+
public CompletableFuture<?> retrieve(Object key) {
140+
CompletableFuture<?> result = getAsyncCache().getIfPresent(key);
141+
if (result != null && isAllowNullValues()) {
142+
result = result.handle((value, ex) -> fromStoreValue(value));
143+
}
144+
return result;
145+
}
146+
147+
@SuppressWarnings("unchecked")
148+
@Override
149+
public <T> CompletableFuture<T> retrieve(Object key, Supplier<CompletableFuture<T>> valueLoader) {
150+
return (CompletableFuture<T>) getAsyncCache().get(key, (k, e) -> valueLoader.get());
151+
}
152+
93153
@Override
94154
@Nullable
95155
protected Object lookup(Object key) {

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

+119-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import java.util.Map;
2323
import java.util.concurrent.ConcurrentHashMap;
2424
import java.util.concurrent.CopyOnWriteArrayList;
25+
import java.util.function.Supplier;
2526

27+
import com.github.benmanes.caffeine.cache.AsyncCache;
28+
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
2629
import com.github.benmanes.caffeine.cache.CacheLoader;
2730
import com.github.benmanes.caffeine.cache.Caffeine;
2831
import com.github.benmanes.caffeine.cache.CaffeineSpec;
@@ -45,7 +48,11 @@
4548
* A {@link CaffeineSpec}-compliant expression value can also be applied
4649
* via the {@link #setCacheSpecification "cacheSpecification"} bean property.
4750
*
48-
* <p>Requires Caffeine 2.1 or higher.
51+
* <p>Supports the {@link Cache#retrieve(Object)} and
52+
* {@link Cache#retrieve(Object, Supplier)} operations through Caffeine's
53+
* {@link AsyncCache}, when configured via {@link #setAsyncCacheMode}.
54+
*
55+
* <p>Requires Caffeine 3.0 or higher, as of Spring Framework 6.1.
4956
*
5057
* @author Ben Manes
5158
* @author Juergen Hoeller
@@ -54,13 +61,18 @@
5461
* @author Brian Clozel
5562
* @since 4.3
5663
* @see CaffeineCache
64+
* @see #setCaffeineSpec
65+
* @see #setCacheSpecification
66+
* @see #setAsyncCacheMode
5767
*/
5868
public class CaffeineCacheManager implements CacheManager {
5969

6070
private Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
6171

6272
@Nullable
63-
private CacheLoader<Object, Object> cacheLoader;
73+
private AsyncCacheLoader<Object, Object> cacheLoader;
74+
75+
private boolean asyncCacheMode = false;
6476

6577
private boolean allowNullValues = true;
6678

@@ -110,7 +122,7 @@ public void setCacheNames(@Nullable Collection<String> cacheNames) {
110122
* Set the Caffeine to use for building each individual
111123
* {@link CaffeineCache} instance.
112124
* @see #createNativeCaffeineCache
113-
* @see com.github.benmanes.caffeine.cache.Caffeine#build()
125+
* @see Caffeine#build()
114126
*/
115127
public void setCaffeine(Caffeine<Object, Object> caffeine) {
116128
Assert.notNull(caffeine, "Caffeine must not be null");
@@ -121,7 +133,7 @@ public void setCaffeine(Caffeine<Object, Object> caffeine) {
121133
* Set the {@link CaffeineSpec} to use for building each individual
122134
* {@link CaffeineCache} instance.
123135
* @see #createNativeCaffeineCache
124-
* @see com.github.benmanes.caffeine.cache.Caffeine#from(CaffeineSpec)
136+
* @see Caffeine#from(CaffeineSpec)
125137
*/
126138
public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
127139
doSetCaffeine(Caffeine.from(caffeineSpec));
@@ -132,7 +144,7 @@ public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
132144
* individual {@link CaffeineCache} instance. The given value needs to
133145
* comply with Caffeine's {@link CaffeineSpec} (see its javadoc).
134146
* @see #createNativeCaffeineCache
135-
* @see com.github.benmanes.caffeine.cache.Caffeine#from(String)
147+
* @see Caffeine#from(String)
136148
*/
137149
public void setCacheSpecification(String cacheSpecification) {
138150
doSetCaffeine(Caffeine.from(cacheSpecification));
@@ -149,7 +161,7 @@ private void doSetCaffeine(Caffeine<Object, Object> cacheBuilder) {
149161
* Set the Caffeine CacheLoader to use for building each individual
150162
* {@link CaffeineCache} instance, turning it into a LoadingCache.
151163
* @see #createNativeCaffeineCache
152-
* @see com.github.benmanes.caffeine.cache.Caffeine#build(CacheLoader)
164+
* @see Caffeine#build(CacheLoader)
153165
* @see com.github.benmanes.caffeine.cache.LoadingCache
154166
*/
155167
public void setCacheLoader(CacheLoader<Object, Object> cacheLoader) {
@@ -159,6 +171,45 @@ public void setCacheLoader(CacheLoader<Object, Object> cacheLoader) {
159171
}
160172
}
161173

174+
/**
175+
* Set the Caffeine AsyncCacheLoader to use for building each individual
176+
* {@link CaffeineCache} instance, turning it into a LoadingCache.
177+
* <p>This implicitly switches the {@link #setAsyncCacheMode "asyncCacheMode"}
178+
* flag to {@code true}.
179+
* @since 6.1
180+
* @see #createAsyncCaffeineCache
181+
* @see Caffeine#buildAsync(AsyncCacheLoader)
182+
* @see com.github.benmanes.caffeine.cache.LoadingCache
183+
*/
184+
public void setAsyncCacheLoader(AsyncCacheLoader<Object, Object> cacheLoader) {
185+
if (!ObjectUtils.nullSafeEquals(this.cacheLoader, cacheLoader)) {
186+
this.cacheLoader = cacheLoader;
187+
this.asyncCacheMode = true;
188+
refreshCommonCaches();
189+
}
190+
}
191+
192+
/**
193+
* Set the common cache type that this cache manager builds to async.
194+
* This applies to {@link #setCacheNames} as well as on-demand caches.
195+
* <p>Individual cache registrations (such as {@link #registerCustomCache(String, AsyncCache)}
196+
* and {@link #registerCustomCache(String, com.github.benmanes.caffeine.cache.Cache)}
197+
* are not dependent on this setting.
198+
* <p>By default, this cache manager builds regular native Caffeine caches.
199+
* To switch to async caches which can also be used through the synchronous API
200+
* but come with support for {@code Cache#retrieve}, set this flag to {@code true}.
201+
* @since 6.1
202+
* @see Caffeine#buildAsync()
203+
* @see Cache#retrieve(Object)
204+
* @see Cache#retrieve(Object, Supplier)
205+
*/
206+
public void setAsyncCacheMode(boolean asyncCacheMode) {
207+
if (this.asyncCacheMode != asyncCacheMode) {
208+
this.asyncCacheMode = asyncCacheMode;
209+
refreshCommonCaches();
210+
}
211+
}
212+
162213
/**
163214
* Specify whether to accept and convert {@code null} values for all caches
164215
* in this cache manager.
@@ -211,27 +262,62 @@ public Cache getCache(String name) {
211262
* @param name the name of the cache
212263
* @param cache the custom Caffeine Cache instance to register
213264
* @since 5.2.8
214-
* @see #adaptCaffeineCache
265+
* @see #adaptCaffeineCache(String, com.github.benmanes.caffeine.cache.Cache)
215266
*/
216267
public void registerCustomCache(String name, com.github.benmanes.caffeine.cache.Cache<Object, Object> cache) {
217268
this.customCacheNames.add(name);
218269
this.cacheMap.put(name, adaptCaffeineCache(name, cache));
219270
}
220271

272+
/**
273+
* Register the given Caffeine AsyncCache instance with this cache manager,
274+
* adapting it to Spring's cache API for exposure through {@link #getCache}.
275+
* Any number of such custom caches may be registered side by side.
276+
* <p>This allows for custom settings per cache (as opposed to all caches
277+
* sharing the common settings in the cache manager's configuration) and
278+
* is typically used with the Caffeine builder API:
279+
* {@code registerCustomCache("myCache", Caffeine.newBuilder().maximumSize(10).build())}
280+
* <p>Note that any other caches, whether statically specified through
281+
* {@link #setCacheNames} or dynamically built on demand, still operate
282+
* with the common settings in the cache manager's configuration.
283+
* @param name the name of the cache
284+
* @param cache the custom Caffeine Cache instance to register
285+
* @since 6.1
286+
* @see #adaptCaffeineCache(String, AsyncCache)
287+
*/
288+
public void registerCustomCache(String name, AsyncCache<Object, Object> cache) {
289+
this.customCacheNames.add(name);
290+
this.cacheMap.put(name, adaptCaffeineCache(name, cache));
291+
}
292+
221293
/**
222294
* Adapt the given new native Caffeine Cache instance to Spring's {@link Cache}
223295
* abstraction for the specified cache name.
224296
* @param name the name of the cache
225297
* @param cache the native Caffeine Cache instance
226298
* @return the Spring CaffeineCache adapter (or a decorator thereof)
227299
* @since 5.2.8
228-
* @see CaffeineCache
300+
* @see CaffeineCache#CaffeineCache(String, com.github.benmanes.caffeine.cache.Cache, boolean)
229301
* @see #isAllowNullValues()
230302
*/
231303
protected Cache adaptCaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Object, Object> cache) {
232304
return new CaffeineCache(name, cache, isAllowNullValues());
233305
}
234306

307+
/**
308+
* Adapt the given new Caffeine AsyncCache instance to Spring's {@link Cache}
309+
* abstraction for the specified cache name.
310+
* @param name the name of the cache
311+
* @param cache the Caffeine AsyncCache instance
312+
* @return the Spring CaffeineCache adapter (or a decorator thereof)
313+
* @since 6.1
314+
* @see CaffeineCache#CaffeineCache(String, AsyncCache, boolean)
315+
* @see #isAllowNullValues()
316+
*/
317+
protected Cache adaptCaffeineCache(String name, AsyncCache<Object, Object> cache) {
318+
return new CaffeineCache(name, cache, isAllowNullValues());
319+
}
320+
235321
/**
236322
* Build a common {@link CaffeineCache} instance for the specified cache name,
237323
* using the common Caffeine configuration specified on this cache manager.
@@ -244,7 +330,8 @@ protected Cache adaptCaffeineCache(String name, com.github.benmanes.caffeine.cac
244330
* @see #createNativeCaffeineCache
245331
*/
246332
protected Cache createCaffeineCache(String name) {
247-
return adaptCaffeineCache(name, createNativeCaffeineCache(name));
333+
return (this.asyncCacheMode ? adaptCaffeineCache(name, createAsyncCaffeineCache(name)) :
334+
adaptCaffeineCache(name, createNativeCaffeineCache(name)));
248335
}
249336

250337
/**
@@ -255,7 +342,29 @@ protected Cache createCaffeineCache(String name) {
255342
* @see #createCaffeineCache
256343
*/
257344
protected com.github.benmanes.caffeine.cache.Cache<Object, Object> createNativeCaffeineCache(String name) {
258-
return (this.cacheLoader != null ? this.cacheBuilder.build(this.cacheLoader) : this.cacheBuilder.build());
345+
if (this.cacheLoader != null) {
346+
if (this.cacheLoader instanceof CacheLoader<Object, Object> regularCacheLoader) {
347+
return this.cacheBuilder.build(regularCacheLoader);
348+
}
349+
else {
350+
throw new IllegalStateException(
351+
"Cannot create regular Caffeine Cache with async-only cache loader: " + this.cacheLoader);
352+
}
353+
}
354+
return this.cacheBuilder.build();
355+
}
356+
357+
/**
358+
* Build a common Caffeine AsyncCache instance for the specified cache name,
359+
* using the common Caffeine configuration specified on this cache manager.
360+
* @param name the name of the cache
361+
* @return the Caffeine AsyncCache instance
362+
* @since 6.1
363+
* @see #createCaffeineCache
364+
*/
365+
protected AsyncCache<Object, Object> createAsyncCaffeineCache(String name) {
366+
return (this.cacheLoader != null ? this.cacheBuilder.buildAsync(this.cacheLoader) :
367+
this.cacheBuilder.buildAsync());
259368
}
260369

261370
/**

spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheDecorator.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package org.springframework.cache.transaction;
1818

1919
import java.util.concurrent.Callable;
20+
import java.util.concurrent.CompletableFuture;
21+
import java.util.function.Supplier;
2022

2123
import org.springframework.cache.Cache;
2224
import org.springframework.lang.Nullable;
@@ -91,6 +93,17 @@ public <T> T get(Object key, Callable<T> valueLoader) {
9193
return this.targetCache.get(key, valueLoader);
9294
}
9395

96+
@Override
97+
@Nullable
98+
public CompletableFuture<?> retrieve(Object key) {
99+
return this.targetCache.retrieve(key);
100+
}
101+
102+
@Override
103+
public <T> CompletableFuture<T> retrieve(Object key, Supplier<CompletableFuture<T>> valueLoader) {
104+
return this.targetCache.retrieve(key, valueLoader);
105+
}
106+
94107
@Override
95108
public void put(final Object key, @Nullable final Object value) {
96109
if (TransactionSynchronizationManager.isSynchronizationActive()) {

0 commit comments

Comments
 (0)