Skip to content

Commit 707b636

Browse files
committed
Add auto-configuration support for Apache Geode as a caching provider.
1 parent 51092af commit 707b636

File tree

12 files changed

+430
-33
lines changed

12 files changed

+430
-33
lines changed

spring-boot-autoconfigure/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@
351351
</exclusion>
352352
</exclusions>
353353
</dependency>
354+
<dependency>
355+
<groupId>org.springframework.data</groupId>
356+
<artifactId>spring-data-geode</artifactId>
357+
<optional>true</optional>
358+
</dependency>
354359
<dependency>
355360
<groupId>org.springframework.data</groupId>
356361
<artifactId>spring-data-jpa</artifactId>

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheConfigurations.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*
2828
* @author Phillip Webb
2929
* @author Eddú Meléndez
30+
* @author John Blum
3031
*/
3132
final class CacheConfigurations {
3233

@@ -43,6 +44,7 @@ final class CacheConfigurations {
4344
mappings.put(CacheType.REDIS, RedisCacheConfiguration.class);
4445
mappings.put(CacheType.CAFFEINE, CaffeineCacheConfiguration.class);
4546
mappings.put(CacheType.GUAVA, GuavaCacheConfiguration.class);
47+
mappings.put(CacheType.GEODE, GeodeCacheConfiguration.class);
4648
mappings.put(CacheType.SIMPLE, SimpleCacheConfiguration.class);
4749
mappings.put(CacheType.NONE, NoOpCacheConfiguration.class);
4850
MAPPINGS = Collections.unmodifiableMap(mappings);

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheType.java

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @author Stephane Nicoll
2323
* @author Phillip Webb
2424
* @author Eddú Meléndez
25+
* @author John Blum
2526
* @since 1.3.0
2627
*/
2728
public enum CacheType {
@@ -71,6 +72,11 @@ public enum CacheType {
7172
*/
7273
GUAVA,
7374

75+
/**
76+
* Apache Geode backed caching.
77+
*/
78+
GEODE,
79+
7480
/**
7581
* Simple in-memory caching.
7682
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2011-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.cache;
18+
19+
import java.util.HashSet;
20+
import java.util.List;
21+
import java.util.Set;
22+
23+
import com.gemstone.gemfire.cache.Cache;
24+
import com.gemstone.gemfire.cache.GemFireCache;
25+
import com.gemstone.gemfire.cache.Region;
26+
import com.gemstone.gemfire.cache.client.ClientCache;
27+
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
29+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
31+
import org.springframework.cache.CacheManager;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Conditional;
34+
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.data.gemfire.CacheFactoryBean;
36+
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
37+
import org.springframework.data.gemfire.support.GemfireCacheManager;
38+
import org.springframework.util.Assert;
39+
import org.springframework.util.CollectionUtils;
40+
41+
/**
42+
* GemFire cache configuration.
43+
*
44+
* @author John Blum
45+
* @see com.gemstone.gemfire.cache.Cache
46+
* @see com.gemstone.gemfire.cache.GemFireCache
47+
* @see com.gemstone.gemfire.cache.client.ClientCache
48+
* @see org.springframework.context.annotation.Bean
49+
* @see org.springframework.context.annotation.Configuration
50+
* @see org.springframework.data.gemfire.support.GemfireCacheManager
51+
* @since 1.5.0
52+
*/
53+
@Configuration
54+
@ConditionalOnBean({ CacheFactoryBean.class, Cache.class,
55+
ClientCacheFactoryBean.class, ClientCache.class })
56+
@ConditionalOnClass(GemfireCacheManager.class)
57+
@ConditionalOnMissingBean(CacheManager.class)
58+
@Conditional(CacheCondition.class)
59+
class GeodeCacheConfiguration {
60+
61+
private final CacheProperties cacheProperties;
62+
63+
private final CacheManagerCustomizers cacheManagerCustomizers;
64+
65+
GeodeCacheConfiguration(CacheProperties cacheProperties,
66+
CacheManagerCustomizers cacheManagerCustomizers) {
67+
68+
this.cacheProperties = cacheProperties;
69+
this.cacheManagerCustomizers = cacheManagerCustomizers;
70+
}
71+
72+
@Bean
73+
public GemfireCacheManager cacheManager(GemFireCache gemfireCache) {
74+
GemfireCacheManager cacheManager = new GemfireCacheManager();
75+
76+
cacheManager.setCache((Cache) gemfireCache);
77+
78+
Set<Region<?, ?>> regions = getRegions(gemfireCache);
79+
80+
if (!CollectionUtils.isEmpty(regions)) {
81+
cacheManager.setRegions(regions);
82+
}
83+
84+
return this.cacheManagerCustomizers.customize(cacheManager);
85+
}
86+
87+
private Set<Region<?, ?>> getRegions(GemFireCache gemfireCache) {
88+
List<String> cacheNames = this.cacheProperties.getCacheNames();
89+
Set<Region<?, ?>> regions = new HashSet<Region<?, ?>>(cacheNames.size());
90+
91+
for (String cacheName : cacheNames) {
92+
Region<?, ?> region = gemfireCache.getRegion(cacheName);
93+
Assert.notNull(region, String.format("No Region for cache name [%s] was found", cacheName));
94+
regions.add(region);
95+
}
96+
97+
return regions;
98+
}
99+
}

0 commit comments

Comments
 (0)