Skip to content

Commit 790abed

Browse files
committed
Polishing
1 parent 340b32a commit 790abed

File tree

7 files changed

+56
-44
lines changed

7 files changed

+56
-44
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void setCacheNames(@Nullable Collection<String> cacheNames) {
110110
* Set the Caffeine to use for building each individual
111111
* {@link CaffeineCache} instance.
112112
* @see #createNativeCaffeineCache
113-
* @see com.github.benmanes.caffeine.cache.Caffeine#build()
113+
* @see Caffeine#build()
114114
*/
115115
public void setCaffeine(Caffeine<Object, Object> caffeine) {
116116
Assert.notNull(caffeine, "Caffeine must not be null");
@@ -121,7 +121,7 @@ public void setCaffeine(Caffeine<Object, Object> caffeine) {
121121
* Set the {@link CaffeineSpec} to use for building each individual
122122
* {@link CaffeineCache} instance.
123123
* @see #createNativeCaffeineCache
124-
* @see com.github.benmanes.caffeine.cache.Caffeine#from(CaffeineSpec)
124+
* @see Caffeine#from(CaffeineSpec)
125125
*/
126126
public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
127127
doSetCaffeine(Caffeine.from(caffeineSpec));
@@ -132,7 +132,7 @@ public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
132132
* individual {@link CaffeineCache} instance. The given value needs to
133133
* comply with Caffeine's {@link CaffeineSpec} (see its javadoc).
134134
* @see #createNativeCaffeineCache
135-
* @see com.github.benmanes.caffeine.cache.Caffeine#from(String)
135+
* @see Caffeine#from(String)
136136
*/
137137
public void setCacheSpecification(String cacheSpecification) {
138138
doSetCaffeine(Caffeine.from(cacheSpecification));
@@ -149,7 +149,7 @@ private void doSetCaffeine(Caffeine<Object, Object> cacheBuilder) {
149149
* Set the Caffeine CacheLoader to use for building each individual
150150
* {@link CaffeineCache} instance, turning it into a LoadingCache.
151151
* @see #createNativeCaffeineCache
152-
* @see com.github.benmanes.caffeine.cache.Caffeine#build(CacheLoader)
152+
* @see Caffeine#build(CacheLoader)
153153
* @see com.github.benmanes.caffeine.cache.LoadingCache
154154
*/
155155
public void setCacheLoader(CacheLoader<Object, Object> cacheLoader) {

spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@ public class CaffeineCacheManagerTests {
3838
@Test
3939
public void testDynamicMode() {
4040
CacheManager cm = new CaffeineCacheManager();
41+
4142
Cache cache1 = cm.getCache("c1");
42-
boolean condition2 = cache1 instanceof CaffeineCache;
43-
assertThat(condition2).isTrue();
43+
assertThat(cache1).isInstanceOf(CaffeineCache.class);
4444
Cache cache1again = cm.getCache("c1");
4545
assertThat(cache1).isSameAs(cache1again);
4646
Cache cache2 = cm.getCache("c2");
47-
boolean condition1 = cache2 instanceof CaffeineCache;
48-
assertThat(condition1).isTrue();
47+
assertThat(cache2).isInstanceOf(CaffeineCache.class);
4948
Cache cache2again = cm.getCache("c2");
5049
assertThat(cache2).isSameAs(cache2again);
5150
Cache cache3 = cm.getCache("c3");
52-
boolean condition = cache3 instanceof CaffeineCache;
53-
assertThat(condition).isTrue();
51+
assertThat(cache3).isInstanceOf(CaffeineCache.class);
5452
Cache cache3again = cm.getCache("c3");
5553
assertThat(cache3).isSameAs(cache3again);
5654

@@ -62,19 +60,23 @@ public void testDynamicMode() {
6260
assertThat(cache1.get("key3").get()).isNull();
6361
cache1.evict("key3");
6462
assertThat(cache1.get("key3")).isNull();
63+
assertThat(cache1.get("key3", () -> "value3")).isEqualTo("value3");
64+
assertThat(cache1.get("key3", () -> "value3")).isEqualTo("value3");
65+
cache1.evict("key3");
66+
assertThat(cache1.get("key3", () -> (String) null)).isNull();
67+
assertThat(cache1.get("key3", () -> (String) null)).isNull();
6568
}
6669

6770
@Test
6871
public void testStaticMode() {
6972
CaffeineCacheManager cm = new CaffeineCacheManager("c1", "c2");
73+
7074
Cache cache1 = cm.getCache("c1");
71-
boolean condition3 = cache1 instanceof CaffeineCache;
72-
assertThat(condition3).isTrue();
75+
assertThat(cache1).isInstanceOf(CaffeineCache.class);
7376
Cache cache1again = cm.getCache("c1");
7477
assertThat(cache1).isSameAs(cache1again);
7578
Cache cache2 = cm.getCache("c2");
76-
boolean condition2 = cache2 instanceof CaffeineCache;
77-
assertThat(condition2).isTrue();
79+
assertThat(cache2).isInstanceOf(CaffeineCache.class);
7880
Cache cache2again = cm.getCache("c2");
7981
assertThat(cache2).isSameAs(cache2again);
8082
Cache cache3 = cm.getCache("c3");
@@ -91,13 +93,11 @@ public void testStaticMode() {
9193

9294
cm.setAllowNullValues(false);
9395
Cache cache1x = cm.getCache("c1");
94-
boolean condition1 = cache1x instanceof CaffeineCache;
95-
assertThat(condition1).isTrue();
96-
assertThat(cache1x != cache1).isTrue();
96+
assertThat(cache1x).isInstanceOf(CaffeineCache.class);
97+
assertThat(cache1x).isNotSameAs(cache1);
9798
Cache cache2x = cm.getCache("c2");
98-
boolean condition = cache2x instanceof CaffeineCache;
99-
assertThat(condition).isTrue();
100-
assertThat(cache2x != cache2).isTrue();
99+
assertThat(cache2x).isInstanceOf(CaffeineCache.class);
100+
assertThat(cache2x).isNotSameAs(cache2);
101101
Cache cache3x = cm.getCache("c3");
102102
assertThat(cache3x).isNull();
103103

@@ -190,7 +190,7 @@ public void cacheLoaderUseLoadingCache() {
190190
assertThat(value.get()).isEqualTo("pong");
191191

192192
assertThatIllegalArgumentException().isThrownBy(() -> assertThat(cache1.get("foo")).isNull())
193-
.withMessageContaining("I only know ping");
193+
.withMessageContaining("I only know ping");
194194
}
195195

196196
@Test

spring-context/src/main/java/org/springframework/cache/Cache.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -23,14 +23,20 @@
2323
/**
2424
* Interface that defines common cache operations.
2525
*
26-
* <b>Note:</b> Due to the generic use of caching, it is recommended that
27-
* implementations allow storage of {@code null} values (for example to
28-
* cache methods that return {@code null}).
26+
* <p>Serves as an SPI for Spring's annotation-based caching model
27+
* ({@link org.springframework.cache.annotation.Cacheable} and co)
28+
* as well as an API for direct usage in applications.
29+
*
30+
* <p><b>Note:</b> Due to the generic use of caching, it is recommended
31+
* that implementations allow storage of {@code null} values
32+
* (for example to cache methods that return {@code null}).
2933
*
3034
* @author Costin Leau
3135
* @author Juergen Hoeller
3236
* @author Stephane Nicoll
3337
* @since 3.1
38+
* @see CacheManager
39+
* @see org.springframework.cache.annotation.Cacheable
3440
*/
3541
public interface Cache {
3642

spring-context/src/main/java/org/springframework/cache/interceptor/AbstractCacheInvoker.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -82,12 +82,12 @@ protected Cache.ValueWrapper doGet(Cache cache, Object key) {
8282
* Execute {@link Cache#put(Object, Object)} on the specified {@link Cache}
8383
* and invoke the error handler if an exception occurs.
8484
*/
85-
protected void doPut(Cache cache, Object key, @Nullable Object result) {
85+
protected void doPut(Cache cache, Object key, @Nullable Object value) {
8686
try {
87-
cache.put(key, result);
87+
cache.put(key, value);
8888
}
8989
catch (RuntimeException ex) {
90-
getErrorHandler().handleCachePutError(ex, cache, key, result);
90+
getErrorHandler().handleCachePutError(ex, cache, key, value);
9191
}
9292
}
9393

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,11 @@ private Object execute(final CacheOperationInvoker invoker, Method method, Cache
397397
processCacheEvicts(contexts.get(CacheEvictOperation.class), true,
398398
CacheOperationExpressionEvaluator.NO_RESULT);
399399

400-
// Check if we have a cached item matching the conditions
400+
// Check if we have a cached value matching the conditions
401401
Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class));
402402

403-
// Collect puts from any @Cacheable miss, if no cached item is found
404-
List<CachePutRequest> cachePutRequests = new ArrayList<>();
403+
// Collect puts from any @Cacheable miss, if no cached value is found
404+
List<CachePutRequest> cachePutRequests = new ArrayList<>(1);
405405
if (cacheHit == null) {
406406
collectPutRequests(contexts.get(CacheableOperation.class),
407407
CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests);
@@ -468,7 +468,7 @@ private Object unwrapReturnValue(@Nullable Object returnValue) {
468468
private boolean hasCachePut(CacheOperationContexts contexts) {
469469
// Evaluate the conditions *without* the result object because we don't have it yet...
470470
Collection<CacheOperationContext> cachePutContexts = contexts.get(CachePutOperation.class);
471-
Collection<CacheOperationContext> excluded = new ArrayList<>();
471+
Collection<CacheOperationContext> excluded = new ArrayList<>(1);
472472
for (CacheOperationContext context : cachePutContexts) {
473473
try {
474474
if (!context.isConditionPassing(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE)) {
@@ -521,9 +521,9 @@ private void logInvalidating(CacheOperationContext context, CacheEvictOperation
521521
}
522522

523523
/**
524-
* Find a cached item only for {@link CacheableOperation} that passes the condition.
524+
* Find a cached value only for {@link CacheableOperation} that passes the condition.
525525
* @param contexts the cacheable operations
526-
* @return a {@link Cache.ValueWrapper} holding the cached item,
526+
* @return a {@link Cache.ValueWrapper} holding the cached value,
527527
* or {@code null} if none is found
528528
*/
529529
@Nullable
@@ -548,9 +548,9 @@ private Cache.ValueWrapper findCachedItem(Collection<CacheOperationContext> cont
548548

549549
/**
550550
* Collect the {@link CachePutRequest} for all {@link CacheOperation} using
551-
* the specified result item.
551+
* the specified result value.
552552
* @param contexts the contexts to handle
553-
* @param result the result item (never {@code null})
553+
* @param result the result value (never {@code null})
554554
* @param putRequests the collection to update
555555
*/
556556
private void collectPutRequests(Collection<CacheOperationContext> contexts,
@@ -722,7 +722,7 @@ public CacheOperationContext(CacheOperationMetadata metadata, Object[] args, Obj
722722
this.args = extractArgs(metadata.method, args);
723723
this.target = target;
724724
this.caches = CacheAspectSupport.this.getCaches(this, metadata.cacheResolver);
725-
this.cacheNames = createCacheNames(this.caches);
725+
this.cacheNames = prepareCacheNames(this.caches);
726726
}
727727

728728
@Override
@@ -810,8 +810,8 @@ protected Collection<String> getCacheNames() {
810810
return this.cacheNames;
811811
}
812812

813-
private Collection<String> createCacheNames(Collection<? extends Cache> caches) {
814-
Collection<String> names = new ArrayList<>();
813+
private Collection<String> prepareCacheNames(Collection<? extends Cache> caches) {
814+
Collection<String> names = new ArrayList<>(caches.size());
815815
for (Cache cache : caches) {
816816
names.add(cache.getName());
817817
}

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

Lines changed: 5 additions & 3 deletions
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.
@@ -24,14 +24,16 @@
2424
/**
2525
* Simple cache manager working against a given collection of caches.
2626
* Useful for testing or simple caching declarations.
27-
* <p>
28-
* When using this implementation directly, i.e. not via a regular
27+
*
28+
* <p>When using this implementation directly, i.e. not via a regular
2929
* bean registration, {@link #initializeCaches()} should be invoked
3030
* to initialize its internal state once the
3131
* {@linkplain #setCaches(Collection) caches have been provided}.
3232
*
3333
* @author Costin Leau
3434
* @since 3.1
35+
* @see NoOpCache
36+
* @see org.springframework.cache.concurrent.ConcurrentMapCache
3537
*/
3638
public class SimpleCacheManager extends AbstractCacheManager {
3739

spring-context/src/test/java/org/springframework/cache/CacheReproTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ void spr13081ConfigNoCacheNameIsRequired() {
118118
assertThat(cacheResolver.getCache("foo").get("foo")).isNull();
119119
Object result = bean.getSimple("foo"); // cache name = id
120120
assertThat(cacheResolver.getCache("foo").get("foo").get()).isEqualTo(result);
121+
121122
context.close();
122123
}
123124

@@ -127,7 +128,7 @@ void spr13081ConfigFailIfCacheResolverReturnsNullCacheName() {
127128
Spr13081Service bean = context.getBean(Spr13081Service.class);
128129

129130
assertThatIllegalStateException().isThrownBy(() -> bean.getSimple(null))
130-
.withMessageContaining(MyCacheResolver.class.getName());
131+
.withMessageContaining(MyCacheResolver.class.getName());
131132
context.close();
132133
}
133134

@@ -146,6 +147,7 @@ void spr14230AdaptsToOptional() {
146147
TestBean tb2 = bean.findById("tb1").get();
147148
assertThat(tb2).isNotSameAs(tb);
148149
assertThat(cache.get("tb1").get()).isSameAs(tb2);
150+
149151
context.close();
150152
}
151153

@@ -177,6 +179,7 @@ void spr15271FindsOnInterfaceWithInterfaceProxy() {
177179
bean.insertItem(tb);
178180
assertThat(bean.findById("tb1").get()).isSameAs(tb);
179181
assertThat(cache.get("tb1").get()).isSameAs(tb);
182+
180183
context.close();
181184
}
182185

@@ -190,6 +193,7 @@ void spr15271FindsOnInterfaceWithCglibProxy() {
190193
bean.insertItem(tb);
191194
assertThat(bean.findById("tb1").get()).isSameAs(tb);
192195
assertThat(cache.get("tb1").get()).isSameAs(tb);
196+
193197
context.close();
194198
}
195199

0 commit comments

Comments
 (0)