Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
6741f05a
Commit
6741f05a
authored
Feb 29, 2016
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish contribution
Closes gh-4903
parent
98cc6836
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
118 additions
and
60 deletions
+118
-60
CacheStatisticsAutoConfiguration.java
...tuate/autoconfigure/CacheStatisticsAutoConfiguration.java
+11
-11
CacheStatisticsAutoConfigurationTests.java
.../autoconfigure/CacheStatisticsAutoConfigurationTests.java
+8
-8
CaffeineCacheConfiguration.java
.../boot/autoconfigure/cache/CaffeineCacheConfiguration.java
+25
-11
CacheAutoConfigurationTests.java
...boot/autoconfigure/cache/CacheAutoConfigurationTests.java
+35
-30
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+26
-0
README.adoc
spring-boot-samples/spring-boot-sample-cache/README.adoc
+7
-0
application.properties
...ot-sample-cache/src/main/resources/application.properties
+6
-0
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfiguration.java
View file @
6741f05a
...
@@ -76,17 +76,6 @@ public class CacheStatisticsAutoConfiguration {
...
@@ -76,17 +76,6 @@ public class CacheStatisticsAutoConfiguration {
}
}
@Configuration
@ConditionalOnClass
({
Caffeine
.
class
,
CaffeineCacheManager
.
class
})
static
class
CaffeineCacheStatisticsProviderConfiguration
{
@Bean
public
CaffeineCacheStatisticsProvider
caffeineCacheStatisticsProvider
()
{
return
new
CaffeineCacheStatisticsProvider
();
}
}
@Configuration
@Configuration
@ConditionalOnClass
({
EhCacheCache
.
class
,
Ehcache
.
class
,
StatisticsGateway
.
class
})
@ConditionalOnClass
({
EhCacheCache
.
class
,
Ehcache
.
class
,
StatisticsGateway
.
class
})
static
class
EhCacheCacheStatisticsProviderConfiguration
{
static
class
EhCacheCacheStatisticsProviderConfiguration
{
...
@@ -119,6 +108,17 @@ public class CacheStatisticsAutoConfiguration {
...
@@ -119,6 +108,17 @@ public class CacheStatisticsAutoConfiguration {
}
}
@Configuration
@ConditionalOnClass
({
Caffeine
.
class
,
CaffeineCacheManager
.
class
})
static
class
CaffeineCacheStatisticsProviderConfiguration
{
@Bean
public
CaffeineCacheStatisticsProvider
caffeineCacheStatisticsProvider
()
{
return
new
CaffeineCacheStatisticsProvider
();
}
}
@Configuration
@Configuration
@ConditionalOnClass
({
com
.
google
.
common
.
cache
.
Cache
.
class
,
GuavaCache
.
class
})
@ConditionalOnClass
({
com
.
google
.
common
.
cache
.
Cache
.
class
,
GuavaCache
.
class
})
static
class
GuavaCacheStatisticsConfiguration
{
static
class
GuavaCacheStatisticsConfiguration
{
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CacheStatisticsAutoConfigurationTests.java
View file @
6741f05a
...
@@ -118,6 +118,14 @@ public class CacheStatisticsAutoConfigurationTests {
...
@@ -118,6 +118,14 @@ public class CacheStatisticsAutoConfigurationTests {
doTestCoreStatistics
(
provider
,
true
);
doTestCoreStatistics
(
provider
,
true
);
}
}
@Test
public
void
baseCaffeineCacheStatistics
()
{
load
(
CaffeineCacheConfig
.
class
);
CacheStatisticsProvider
provider
=
this
.
context
.
getBean
(
"caffeineCacheStatisticsProvider"
,
CacheStatisticsProvider
.
class
);
doTestCoreStatistics
(
provider
,
true
);
}
@Test
@Test
public
void
concurrentMapCacheStatistics
()
{
public
void
concurrentMapCacheStatistics
()
{
load
(
ConcurrentMapConfig
.
class
);
load
(
ConcurrentMapConfig
.
class
);
...
@@ -148,14 +156,6 @@ public class CacheStatisticsAutoConfigurationTests {
...
@@ -148,14 +156,6 @@ public class CacheStatisticsAutoConfigurationTests {
assertCoreStatistics
(
updatedCacheStatistics
,
null
,
null
,
null
);
assertCoreStatistics
(
updatedCacheStatistics
,
null
,
null
,
null
);
}
}
@Test
public
void
caffeineCacheStatistics
()
{
load
(
CaffeineCacheConfig
.
class
);
CacheStatisticsProvider
provider
=
this
.
context
.
getBean
(
"caffeineCacheStatisticsProvider"
,
CacheStatisticsProvider
.
class
);
doTestCoreStatistics
(
provider
,
true
);
}
private
void
doTestCoreStatistics
(
CacheStatisticsProvider
provider
,
private
void
doTestCoreStatistics
(
CacheStatisticsProvider
provider
,
boolean
supportSize
)
{
boolean
supportSize
)
{
Cache
books
=
getCache
(
"books"
);
Cache
books
=
getCache
(
"books"
);
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CaffeineCacheConfiguration.java
View file @
6741f05a
...
@@ -16,6 +16,8 @@
...
@@ -16,6 +16,8 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
cache
;
package
org
.
springframework
.
boot
.
autoconfigure
.
cache
;
import
java.util.List
;
import
com.github.benmanes.caffeine.cache.CacheLoader
;
import
com.github.benmanes.caffeine.cache.CacheLoader
;
import
com.github.benmanes.caffeine.cache.Caffeine
;
import
com.github.benmanes.caffeine.cache.Caffeine
;
import
com.github.benmanes.caffeine.cache.CaffeineSpec
;
import
com.github.benmanes.caffeine.cache.CaffeineSpec
;
...
@@ -23,10 +25,12 @@ import com.github.benmanes.caffeine.cache.CaffeineSpec;
...
@@ -23,10 +25,12 @@ import com.github.benmanes.caffeine.cache.CaffeineSpec;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
import
org.springframework.cache.caffeine.CaffeineCacheManager
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
/**
/**
...
@@ -37,13 +41,16 @@ import org.springframework.util.StringUtils;
...
@@ -37,13 +41,16 @@ import org.springframework.util.StringUtils;
*/
*/
@Configuration
@Configuration
@ConditionalOnClass
({
Caffeine
.
class
,
CaffeineCacheManager
.
class
})
@ConditionalOnClass
({
Caffeine
.
class
,
CaffeineCacheManager
.
class
})
@ConditionalOnMissingBean
(
org
.
springframework
.
cache
.
CacheManager
.
class
)
@ConditionalOnMissingBean
(
CacheManager
.
class
)
@Conditional
({
CacheCondition
.
class
})
@Conditional
({
CacheCondition
.
class
})
class
CaffeineCacheConfiguration
{
class
CaffeineCacheConfiguration
{
@Autowired
@Autowired
private
CacheProperties
cacheProperties
;
private
CacheProperties
cacheProperties
;
@Autowired
private
CacheManagerCustomizers
customizers
;
@Autowired
(
required
=
false
)
@Autowired
(
required
=
false
)
private
Caffeine
<
Object
,
Object
>
caffeine
;
private
Caffeine
<
Object
,
Object
>
caffeine
;
...
@@ -54,27 +61,34 @@ class CaffeineCacheConfiguration {
...
@@ -54,27 +61,34 @@ class CaffeineCacheConfiguration {
private
CacheLoader
<
Object
,
Object
>
cacheLoader
;
private
CacheLoader
<
Object
,
Object
>
cacheLoader
;
@Bean
@Bean
@ConditionalOnMissingBean
public
CaffeineCacheManager
caffeineCacheManager
()
{
public
CaffeineCacheManager
caffeineCacheManager
()
{
CaffeineCacheManager
caffeineCacheManager
=
new
CaffeineCacheManager
();
CaffeineCacheManager
cacheManager
=
createCacheManager
();
setCacheBuilder
(
caffeineCacheManager
);
List
<
String
>
cacheNames
=
this
.
cacheProperties
.
getCacheNames
();
if
(!
CollectionUtils
.
isEmpty
(
cacheNames
))
{
cacheManager
.
setCacheNames
(
cacheNames
);
}
return
this
.
customizers
.
customize
(
cacheManager
);
}
private
CaffeineCacheManager
createCacheManager
()
{
CaffeineCacheManager
cacheManager
=
new
CaffeineCacheManager
();
setCacheBuilder
(
cacheManager
);
if
(
this
.
cacheLoader
!=
null
)
{
if
(
this
.
cacheLoader
!=
null
)
{
ca
ffeineCa
cheManager
.
setCacheLoader
(
this
.
cacheLoader
);
cacheManager
.
setCacheLoader
(
this
.
cacheLoader
);
}
}
caffeineCacheManager
.
setCacheNames
(
this
.
cacheProperties
.
getCacheNames
());
return
cacheManager
;
return
caffeineCacheManager
;
}
}
private
void
setCacheBuilder
(
CaffeineCacheManager
ca
ffeineCa
cheManager
)
{
private
void
setCacheBuilder
(
CaffeineCacheManager
cacheManager
)
{
String
specification
=
this
.
cacheProperties
.
getCaffeine
().
getSpec
();
String
specification
=
this
.
cacheProperties
.
getCaffeine
().
getSpec
();
if
(
StringUtils
.
hasText
(
specification
))
{
if
(
StringUtils
.
hasText
(
specification
))
{
ca
ffeineCacheManager
.
setCaffeine
(
Caffeine
.
from
(
specification
)
);
ca
cheManager
.
setCacheSpecification
(
specification
);
}
}
else
if
(
this
.
caffeineSpec
!=
null
)
{
else
if
(
this
.
caffeineSpec
!=
null
)
{
ca
ffeineCacheManager
.
setCaffeine
(
Caffeine
.
from
(
this
.
caffeineSpec
)
);
ca
cheManager
.
setCaffeineSpec
(
this
.
caffeineSpec
);
}
}
else
if
(
this
.
caffeine
!=
null
)
{
else
if
(
this
.
caffeine
!=
null
)
{
ca
ffeineCa
cheManager
.
setCaffeine
(
this
.
caffeine
);
cacheManager
.
setCaffeine
(
this
.
caffeine
);
}
}
}
}
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java
View file @
6741f05a
...
@@ -584,45 +584,52 @@ public class CacheAutoConfigurationTests {
...
@@ -584,45 +584,52 @@ public class CacheAutoConfigurationTests {
validateGuavaCacheWithStats
();
validateGuavaCacheWithStats
();
}
}
private
void
validateGuavaCacheWithStats
()
{
GuavaCacheManager
cacheManager
=
validateCacheManager
(
GuavaCacheManager
.
class
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"foo"
,
"bar"
);
Cache
foo
=
cacheManager
.
getCache
(
"foo"
);
foo
.
get
(
"1"
);
assertThat
(((
GuavaCache
)
foo
).
getNativeCache
().
stats
().
missCount
()).
isEqualTo
(
1L
);
}
@Test
@Test
public
void
caffeineCacheWithCaches
()
{
public
void
caffeineCacheWith
Explicit
Caches
()
{
load
(
DefaultCacheConfiguration
.
class
,
"spring.cache.type=caffeine"
,
load
(
DefaultCacheConfiguration
.
class
,
"spring.cache.type=caffeine"
,
"spring.cache.cacheNames
[0]=spring"
,
"spring.cache.cacheNames[1]=boot
"
);
"spring.cache.cacheNames
=foo
"
);
CaffeineCacheManager
cacheManager
=
validateCacheManager
(
CaffeineCacheManager
cacheManager
=
validateCacheManager
(
CaffeineCacheManager
.
class
);
CaffeineCacheManager
.
class
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"spring"
,
"boot"
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"foo"
);
Cache
foo
=
cacheManager
.
getCache
(
"foo"
);
foo
.
get
(
"1"
);
// See next tests: no spec given so stats should be disabled
assertThat
(((
CaffeineCache
)
foo
).
getNativeCache
().
stats
().
missCount
()).
isEqualTo
(
0L
);
}
}
@Test
@Test
public
void
caffeineCacheNotAllowNullValues
()
{
public
void
caffeineCacheWithCustomizers
()
{
load
(
DefaultCacheConfiguration
.
class
,
"spring.cache.type=caffeine"
,
testCustomizers
(
DefaultCacheAndCustomizersConfiguration
.
class
,
"caffeine"
,
"spring.cache.caffeine.allow-null-values=false"
);
"allCacheManagerCustomizer"
,
"caffeineCacheManagerCustomizer"
);
CaffeineCacheManager
cacheManager
=
validateCacheManager
(
CaffeineCacheManager
.
class
);
assertThat
(
cacheManager
.
isAllowNullValues
()).
isFalse
();
}
}
@Test
@Test
public
void
caffeineCacheWith
NullCaches
()
{
public
void
caffeineCacheWith
ExplicitCacheBuilder
()
{
load
(
CaffeineCacheBuilderConfiguration
.
class
,
"spring.cache.type=caffeine"
,
load
(
CaffeineCacheBuilderConfiguration
.
class
,
"spring.cache.type=caffeine"
,
"spring.cache.cacheNames[0]=caffeine"
,
"spring.cache.cacheNames[1]=cache"
);
"spring.cache.cacheNames=foo,bar"
);
CaffeineCacheManager
cacheManager
=
validateCacheManager
(
validateCaffeineCacheWithStats
();
CaffeineCacheManager
.
class
);
assertThat
(
cacheManager
.
isAllowNullValues
()).
isTrue
();
}
}
@Test
@Test
public
void
caffeineCacheExplicitWithSpec
()
{
public
void
caffeineCacheExplicitWithSpec
()
{
load
(
DefaultCacheConfiguration
.
class
,
"spring.cache.type=caffeine"
,
load
(
CaffeineCacheSpecConfiguration
.
class
,
"spring.cache.type=caffeine"
,
"spring.cache.caffeine.spec=recordStats"
,
"spring.cache.cacheNames[0]=foo"
,
"spring.cache.cacheNames[0]=foo"
,
"spring.cache.cacheNames[1]=bar"
);
"spring.cache.cacheNames[1]=bar"
);
validateCaffeineCacheWithStats
();
validateCaffeineCacheWithStats
();
}
}
@Test
@Test
public
void
caffeineCacheExplicitWithCacheBuilder
()
{
public
void
caffeineCacheExplicitWithSpecString
()
{
load
(
CaffeineCacheBuilderConfiguration2
.
class
,
"spring.cache.type=caffeine"
,
load
(
DefaultCacheConfiguration
.
class
,
"spring.cache.type=caffeine"
,
"spring.cache.cacheNames[0]=foo"
,
"spring.cache.cacheNames[1]=bar"
);
"spring.cache.caffeine.spec=recordStats"
,
"spring.cache.cacheNames[0]=foo"
,
"spring.cache.cacheNames[1]=bar"
);
validateCaffeineCacheWithStats
();
validateCaffeineCacheWithStats
();
}
}
...
@@ -634,14 +641,6 @@ public class CacheAutoConfigurationTests {
...
@@ -634,14 +641,6 @@ public class CacheAutoConfigurationTests {
assertThat
(((
CaffeineCache
)
foo
).
getNativeCache
().
stats
().
missCount
()).
isEqualTo
(
1L
);
assertThat
(((
CaffeineCache
)
foo
).
getNativeCache
().
stats
().
missCount
()).
isEqualTo
(
1L
);
}
}
private
void
validateGuavaCacheWithStats
()
{
GuavaCacheManager
cacheManager
=
validateCacheManager
(
GuavaCacheManager
.
class
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"foo"
,
"bar"
);
Cache
foo
=
cacheManager
.
getCache
(
"foo"
);
foo
.
get
(
"1"
);
assertThat
(((
GuavaCache
)
foo
).
getNativeCache
().
stats
().
missCount
()).
isEqualTo
(
1L
);
}
private
<
T
extends
CacheManager
>
T
validateCacheManager
(
Class
<
T
>
type
)
{
private
<
T
extends
CacheManager
>
T
validateCacheManager
(
Class
<
T
>
type
)
{
CacheManager
cacheManager
=
this
.
context
.
getBean
(
CacheManager
.
class
);
CacheManager
cacheManager
=
this
.
context
.
getBean
(
CacheManager
.
class
);
assertThat
(
cacheManager
).
as
(
"Wrong cache manager type"
).
isInstanceOf
(
type
);
assertThat
(
cacheManager
).
as
(
"Wrong cache manager type"
).
isInstanceOf
(
type
);
...
@@ -897,14 +896,14 @@ public class CacheAutoConfigurationTests {
...
@@ -897,14 +896,14 @@ public class CacheAutoConfigurationTests {
@Bean
@Bean
Caffeine
<
Object
,
Object
>
cacheBuilder
()
{
Caffeine
<
Object
,
Object
>
cacheBuilder
()
{
return
Caffeine
.
newBuilder
().
maximumSize
(
10
);
return
Caffeine
.
newBuilder
().
recordStats
(
);
}
}
}
}
@Configuration
@Configuration
@EnableCaching
@EnableCaching
static
class
CaffeineCache
BuilderConfiguration2
{
static
class
CaffeineCache
SpecConfiguration
{
@Bean
@Bean
CaffeineSpec
caffeineSpec
()
{
CaffeineSpec
caffeineSpec
()
{
...
@@ -964,6 +963,12 @@ public class CacheAutoConfigurationTests {
...
@@ -964,6 +963,12 @@ public class CacheAutoConfigurationTests {
};
};
}
}
@Bean
public
CacheManagerCustomizer
<
CaffeineCacheManager
>
caffeineCacheManagerCustomizer
()
{
return
new
CacheManagerTestCustomizer
<
CaffeineCacheManager
>()
{
};
}
}
}
static
abstract
class
CacheManagerTestCustomizer
<
T
extends
CacheManager
>
static
abstract
class
CacheManagerTestCustomizer
<
T
extends
CacheManager
>
...
...
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
6741f05a
...
@@ -3249,6 +3249,7 @@ Spring Boot tries to detect the following providers (in this order):
...
@@ -3249,6 +3249,7 @@ Spring Boot tries to detect the following providers (in this order):
* <<boot-features-caching-provider-hazelcast,Hazelcast>>
* <<boot-features-caching-provider-hazelcast,Hazelcast>>
* <<boot-features-caching-provider-infinispan,Infinispan>>
* <<boot-features-caching-provider-infinispan,Infinispan>>
* <<boot-features-caching-provider-redis,Redis>>
* <<boot-features-caching-provider-redis,Redis>>
* <<boot-features-caching-provider-caffeine,Caffeine>>
* <<boot-features-caching-provider-guava,Guava>>
* <<boot-features-caching-provider-guava,Guava>>
* <<boot-features-caching-provider-simple,Simple>>
* <<boot-features-caching-provider-simple,Simple>>
...
@@ -3384,6 +3385,31 @@ recommend to keep this setting enabled if you create your own `RedisCacheManager
...
@@ -3384,6 +3385,31 @@ recommend to keep this setting enabled if you create your own `RedisCacheManager
[[boot-features-caching-provider-caffeine]]
==== Caffeine
Caffeine is a Java 8 rewrite of Guava’s cache and will supersede the Guava support in
Spring Boot 2.0. If Caffeine is present, a `CaffeineCacheManager` is auto-configured.
Caches can be created on startup using the `spring.cache.cache-names` property and
customized by one of the following (in this order):
1. A cache spec defined by `spring.cache.caffeine.spec`
2. A `com.github.benmanes.caffeine.cache.CaffeineSpec` bean is defined
3. A `com.github.benmanes.caffeine.cache.Caffeine` bean is defined
For instance, the following configuration creates a `foo` and `bar` caches with a maximum
size of 500 and a _time to live_ of 10 minutes
[source,properties,indent=0]
----
spring.cache.cache-names=foo,bar
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s
----
Besides, if a `com.github.benmanes.caffeine.cache.CacheLoader` bean is defined, it is
automatically associated to the `CaffeineCacheManager`.
[[boot-features-caching-provider-guava]]
[[boot-features-caching-provider-guava]]
==== Guava
==== Guava
If Guava is present, a `GuavaCacheManager` is auto-configured. Caches can be created
If Guava is present, a `GuavaCacheManager` is auto-configured. Caches can be created
...
...
spring-boot-samples/spring-boot-sample-cache/README.adoc
View file @
6741f05a
...
@@ -84,6 +84,13 @@ a redis instance with the default settings is expected on your local box).
...
@@ -84,6 +84,13 @@ a redis instance with the default settings is expected on your local box).
=== Caffeine
Simply add the `com.github.ben-manes.caffeine:caffeine` dependency to enable support
for Caffeine. You can customize how caches are created in different ways, see
`application.properties` for an example and the documentation for more details.
=== Guava
=== Guava
Spring Boot does not provide any dependency management for _Guava_ so you'll have to add
Spring Boot does not provide any dependency management for _Guava_ so you'll have to add
the `com.google.guava:guava` dependency with a version. You can customize how caches are
the `com.google.guava:guava` dependency with a version. You can customize how caches are
...
...
spring-boot-samples/spring-boot-sample-cache/src/main/resources/application.properties
View file @
6741f05a
...
@@ -10,6 +10,12 @@
...
@@ -10,6 +10,12 @@
#spring.cache.jcache.config=hazelcast.xml
#spring.cache.jcache.config=hazelcast.xml
#
# Caffeine configuration
#
#spring.cache.caffeine.spec=maximumSize=200,expireAfterAccess=600s
#
#
# Guava configuration
# Guava configuration
#
#
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment