From 3a238a2b6104f129915fc46cb3c62e6a08348a3c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 18 Dec 2015 15:46:56 +0100 Subject: [PATCH] Polish contribution Closes gh-921 Issue: SPR-13690 --- .../cache/caffeine/CaffeineCache.java | 8 ++-- .../cache/caffeine/CaffeineCacheManager.java | 14 +++---- .../caffeine/CaffeineCacheManagerTests.java | 6 +-- .../cache/caffeine/CaffeineCacheTests.java | 5 ++- .../concurrent/ConcurrentMapCacheManager.java | 9 +++-- src/asciidoc/integration.adoc | 39 ++++++++++++++++++- 6 files changed, 60 insertions(+), 21 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java index 3623e300357..2bc076cc180 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCache.java @@ -18,21 +18,21 @@ package org.springframework.cache.caffeine; import java.util.function.Function; +import com.github.benmanes.caffeine.cache.LoadingCache; + import org.springframework.cache.support.AbstractValueAdaptingCache; import org.springframework.util.Assert; -import com.github.benmanes.caffeine.cache.LoadingCache; - /** * Spring {@link org.springframework.cache.Cache} adapter implementation * on top of a Caffeine {@link com.github.benmanes.caffeine.cache.Cache} instance. * *

Requires Caffeine 2.0 or higher. * + * @author Ben Manes * @author Juergen Hoeller * @author Stephane Nicoll - * @author Ben Manes - * @since 4.0 + * @since 4.3 */ public class CaffeineCache extends AbstractValueAdaptingCache { diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java index 74ceca77fc9..c7a18f4a4cc 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,14 +23,14 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import com.github.benmanes.caffeine.cache.CacheLoader; +import com.github.benmanes.caffeine.cache.Caffeine; + import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; -import com.github.benmanes.caffeine.cache.CacheLoader; -import com.github.benmanes.caffeine.cache.Caffeine; - /** * {@link CacheManager} implementation that lazily builds {@link CaffeineCache} * instances for each {@link #getCache} request. Also supports a 'static' mode @@ -43,10 +43,10 @@ import com.github.benmanes.caffeine.cache.Caffeine; * *

Requires Caffeine 2.0 or higher. * + * @author Ben Manes * @author Juergen Hoeller * @author Stephane Nicoll - * @author Ben Manes - * @since 4.0 + * @since 4.3 * @see CaffeineCache */ public class CaffeineCacheManager implements CacheManager { @@ -101,7 +101,7 @@ public class CaffeineCacheManager implements CacheManager { * Set the Caffeine to use for building each individual * {@link CaffeineCache} instance. * @see #createNativeCaffeineCache - * @see com.github.benmanes.caffeine.cache.CacheBuilder#build() + * @see com.github.benmanes.caffeine.cache.Caffeine#build() */ public void setCaffeine(Caffeine cacheBuilder) { Assert.notNull(cacheBuilder, "Caffeine must not be null"); diff --git a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java index 0298e717f7a..e8a7c76110b 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java @@ -16,8 +16,8 @@ package org.springframework.cache.caffeine; -import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.CacheLoader; +import com.github.benmanes.caffeine.cache.Caffeine; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -29,9 +29,9 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; /** + * @author Ben Manes * @author Juergen Hoeller * @author Stephane Nicoll - * @author Ben Manes */ public class CaffeineCacheManagerTests { @@ -138,7 +138,7 @@ public class CaffeineCacheManagerTests { CaffeineCacheManager cm = new CaffeineCacheManager("c1"); Cache cache1 = cm.getCache("c1"); - CacheLoader loader = mockCacheLoader(); + CacheLoader loader = mockCacheLoader(); cm.setCacheLoader(loader); Cache cache1x = cm.getCache("c1"); assertTrue(cache1x != cache1); diff --git a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java index 34671acd461..57aad25908c 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,13 @@ import org.springframework.cache.Cache; import static org.junit.Assert.*; /** - * @author Stephane Nicoll * @author Ben Manes + * @author Stephane Nicoll */ public class CaffeineCacheTests extends AbstractCacheTests { private com.github.benmanes.caffeine.cache.Cache nativeCache; + private CaffeineCache cache; @Before diff --git a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java index efe43fe3925..cb2a117e208 100644 --- a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java +++ b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,9 +35,10 @@ import org.springframework.cache.CacheManager; *

Note: This is by no means a sophisticated CacheManager; it comes with no * cache configuration options. However, it may be useful for testing or simple * caching scenarios. For advanced local caching needs, consider - * {@link com.github.benmanes.caffeine.cache.CaffeineCacheManager}, - * {@link org.springframework.cache.guava.GuavaCacheManager}, or - * {@link org.springframework.cache.ehcache.EhCacheCacheManager}. + * {@link org.springframework.cache.jcache.JCacheCacheManager}, + * {@link org.springframework.cache.ehcache.EhCacheCacheManager}, + * {@link com.github.benmanes.caffeine.cache.CaffeineCacheManager} or + * {@link org.springframework.cache.guava.GuavaCacheManager}. * * @author Juergen Hoeller * @since 3.1 diff --git a/src/asciidoc/integration.adoc b/src/asciidoc/integration.adoc index febfb656b7f..88dc7478819 100644 --- a/src/asciidoc/integration.adoc +++ b/src/asciidoc/integration.adoc @@ -8160,6 +8160,7 @@ materialized by the `org.springframework.cache.Cache` and There are <> of that abstraction available out of the box: JDK `java.util.concurrent.ConcurrentMap` based caches, http://ehcache.org/[EhCache], Gemfire cache, +https://github.com/ben-manes/caffeine/wiki[Caffeine], https://code.google.com/p/guava-libraries/wiki/CachesExplained[Guava caches] and JSR-107 compliant caches. See <> for more information on plugging in other cache stores/providers. @@ -8996,7 +8997,7 @@ we did in the example above by defining the target cache through the `cache:defi [[cache-store-configuration]] === Configuring the cache storage -Out of the box, the cache abstraction provides several storages integration. To use +Out of the box, the cache abstraction provides several storage integration. To use them, one needs to simply declare an appropriate `CacheManager` - an entity that controls and manages ++Cache++s and can be used to retrieve these for storage. @@ -9054,6 +9055,42 @@ This setup bootstraps the ehcache library inside Spring IoC (through the `ehcach is then wired into the dedicated `CacheManager` implementation. Note the entire ehcache-specific configuration is read from `ehcache.xml`. +[[cache-store-configuration-caffeine]] +==== Caffeine Cache + +Caffeine is a Java 8 rewrite of Guava's cache and its implementation is located under +`org.springframework.cache.caffeine` package and provides access to several features +of Caffeine. + +Configuring a `CacheManager` that creates the cache on demand is straightforward: + +[source,xml,indent=0] +[subs="verbatim,quotes"] +---- + +---- + +It is also possible to provide the caches to use explicitly. In that case, only those +will be made available by the manager: + +[source,xml,indent=0] +[subs="verbatim,quotes"] +---- + + + + default + books + + + +---- + +The Caffeine `CacheManager` also supports customs `Caffeine` and `CacheLoader`. See +the https://github.com/ben-manes/caffeine/wiki[Caffeine documentation] for more +information about those. + [[cache-store-configuration-guava]] ==== Guava Cache