parent
13aabeef37
commit
3a238a2b61
|
@ -18,21 +18,21 @@ package org.springframework.cache.caffeine;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||||
|
|
||||||
import org.springframework.cache.support.AbstractValueAdaptingCache;
|
import org.springframework.cache.support.AbstractValueAdaptingCache;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring {@link org.springframework.cache.Cache} adapter implementation
|
* Spring {@link org.springframework.cache.Cache} adapter implementation
|
||||||
* on top of a Caffeine {@link com.github.benmanes.caffeine.cache.Cache} instance.
|
* on top of a Caffeine {@link com.github.benmanes.caffeine.cache.Cache} instance.
|
||||||
*
|
*
|
||||||
* <p>Requires Caffeine 2.0 or higher.
|
* <p>Requires Caffeine 2.0 or higher.
|
||||||
*
|
*
|
||||||
|
* @author Ben Manes
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Ben Manes
|
* @since 4.3
|
||||||
* @since 4.0
|
|
||||||
*/
|
*/
|
||||||
public class CaffeineCache extends AbstractValueAdaptingCache {
|
public class CaffeineCache extends AbstractValueAdaptingCache {
|
||||||
|
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
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.Cache;
|
||||||
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ObjectUtils;
|
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}
|
* {@link CacheManager} implementation that lazily builds {@link CaffeineCache}
|
||||||
* instances for each {@link #getCache} request. Also supports a 'static' mode
|
* instances for each {@link #getCache} request. Also supports a 'static' mode
|
||||||
|
@ -43,10 +43,10 @@ import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
*
|
*
|
||||||
* <p>Requires Caffeine 2.0 or higher.
|
* <p>Requires Caffeine 2.0 or higher.
|
||||||
*
|
*
|
||||||
|
* @author Ben Manes
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Ben Manes
|
* @since 4.3
|
||||||
* @since 4.0
|
|
||||||
* @see CaffeineCache
|
* @see CaffeineCache
|
||||||
*/
|
*/
|
||||||
public class CaffeineCacheManager implements CacheManager {
|
public class CaffeineCacheManager implements CacheManager {
|
||||||
|
@ -101,7 +101,7 @@ public class CaffeineCacheManager implements CacheManager {
|
||||||
* Set the Caffeine to use for building each individual
|
* Set the Caffeine to use for building each individual
|
||||||
* {@link CaffeineCache} instance.
|
* {@link CaffeineCache} instance.
|
||||||
* @see #createNativeCaffeineCache
|
* @see #createNativeCaffeineCache
|
||||||
* @see com.github.benmanes.caffeine.cache.CacheBuilder#build()
|
* @see com.github.benmanes.caffeine.cache.Caffeine#build()
|
||||||
*/
|
*/
|
||||||
public void setCaffeine(Caffeine<Object, Object> cacheBuilder) {
|
public void setCaffeine(Caffeine<Object, Object> cacheBuilder) {
|
||||||
Assert.notNull(cacheBuilder, "Caffeine must not be null");
|
Assert.notNull(cacheBuilder, "Caffeine must not be null");
|
||||||
|
|
|
@ -16,8 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.cache.caffeine;
|
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.CacheLoader;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
|
@ -29,9 +29,9 @@ import static org.junit.Assert.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @author Ben Manes
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Ben Manes
|
|
||||||
*/
|
*/
|
||||||
public class CaffeineCacheManagerTests {
|
public class CaffeineCacheManagerTests {
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ public class CaffeineCacheManagerTests {
|
||||||
CaffeineCacheManager cm = new CaffeineCacheManager("c1");
|
CaffeineCacheManager cm = new CaffeineCacheManager("c1");
|
||||||
Cache cache1 = cm.getCache("c1");
|
Cache cache1 = cm.getCache("c1");
|
||||||
|
|
||||||
CacheLoader<Object,Object> loader = mockCacheLoader();
|
CacheLoader<Object, Object> loader = mockCacheLoader();
|
||||||
cm.setCacheLoader(loader);
|
cm.setCacheLoader(loader);
|
||||||
Cache cache1x = cm.getCache("c1");
|
Cache cache1x = cm.getCache("c1");
|
||||||
assertTrue(cache1x != cache1);
|
assertTrue(cache1x != cache1);
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Stephane Nicoll
|
|
||||||
* @author Ben Manes
|
* @author Ben Manes
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class CaffeineCacheTests extends AbstractCacheTests<CaffeineCache> {
|
public class CaffeineCacheTests extends AbstractCacheTests<CaffeineCache> {
|
||||||
|
|
||||||
private com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCache;
|
private com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCache;
|
||||||
|
|
||||||
private CaffeineCache cache;
|
private CaffeineCache cache;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -35,9 +35,10 @@ import org.springframework.cache.CacheManager;
|
||||||
* <p>Note: This is by no means a sophisticated CacheManager; it comes with no
|
* <p>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
|
* cache configuration options. However, it may be useful for testing or simple
|
||||||
* caching scenarios. For advanced local caching needs, consider
|
* caching scenarios. For advanced local caching needs, consider
|
||||||
* {@link com.github.benmanes.caffeine.cache.CaffeineCacheManager},
|
* {@link org.springframework.cache.jcache.JCacheCacheManager},
|
||||||
* {@link org.springframework.cache.guava.GuavaCacheManager}, or
|
* {@link org.springframework.cache.ehcache.EhCacheCacheManager},
|
||||||
* {@link org.springframework.cache.ehcache.EhCacheCacheManager}.
|
* {@link com.github.benmanes.caffeine.cache.CaffeineCacheManager} or
|
||||||
|
* {@link org.springframework.cache.guava.GuavaCacheManager}.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
|
|
|
@ -8160,6 +8160,7 @@ materialized by the `org.springframework.cache.Cache` and
|
||||||
There are <<cache-store-configuration,a few implementations>> of that abstraction
|
There are <<cache-store-configuration,a few implementations>> of that abstraction
|
||||||
available out of the box: JDK `java.util.concurrent.ConcurrentMap` based caches,
|
available out of the box: JDK `java.util.concurrent.ConcurrentMap` based caches,
|
||||||
http://ehcache.org/[EhCache], Gemfire cache,
|
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
|
https://code.google.com/p/guava-libraries/wiki/CachesExplained[Guava caches] and
|
||||||
JSR-107 compliant caches. See <<cache-plug>> for more information on plugging in
|
JSR-107 compliant caches. See <<cache-plug>> for more information on plugging in
|
||||||
other cache stores/providers.
|
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]]
|
[[cache-store-configuration]]
|
||||||
=== Configuring the cache storage
|
=== 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
|
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.
|
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
|
is then wired into the dedicated `CacheManager` implementation. Note the entire
|
||||||
ehcache-specific configuration is read from `ehcache.xml`.
|
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"]
|
||||||
|
----
|
||||||
|
<bean id="cacheManager"
|
||||||
|
class="org.springframework.cache.caffeine.CaffeineCacheManager"/>
|
||||||
|
----
|
||||||
|
|
||||||
|
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"]
|
||||||
|
----
|
||||||
|
<bean id="cacheManager" class="org.springframework.cache.caffeine.CaffeineCacheManager">
|
||||||
|
<property name="caches">
|
||||||
|
<set>
|
||||||
|
<value>default</value>
|
||||||
|
<value>books</value>
|
||||||
|
</set>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
----
|
||||||
|
|
||||||
|
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]]
|
[[cache-store-configuration-guava]]
|
||||||
==== Guava Cache
|
==== Guava Cache
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue