如何在不显式定义区域的情况下为 spring 缓存管理器配置 gemfire?
How to configure gemfire for spring cache manager without explicitly defining regions?
我有一个 spring 应用程序使用 spring 基于 Guava 缓存的缓存。由于高吞吐量需求和后写功能,我们现在正在考虑迁移到 Gemfire。
我成功地将 Gemfire 配置为缓存,并且能够从缓存中读取和写入。在所有配置示例中,配置需要定义 LocalRegionFactory 如下:
@Bean
public Region<Long,Person> myPersonRegion(LocalRegionFactoryBean<Long, Person> personRegion) throws Exception {
return personRegion.getObject();
}
@Bean
public LocalRegionFactoryBean<Long, Person> personRegion(GemFireCache cache,AsyncEventQueue gemfireQueue) {
LocalRegionFactoryBean<Long, Person> personRegion = new LocalRegionFactoryBean<>();
personRegion.setCache(cache);
personRegion.setClose(false);
personRegion.setName("person");
personRegion.setAsyncEventQueues(new AsyncEventQueue[]{gemfireQueue});
personRegion.setPersistent(false);
return personRegion;
}
bean定义好后,我们可以使用@Cacheable(value="person"),@CacheEvict(value="person")。如果我们直接使用缓存名称,gemfire 会抛出缓存未定义的错误。
我们使用 Guava(或 Hazelcast、redis 等)的经验是,我们不需要显式定义缓存。它会在第一次出现时由 spring 自动创建。
有没有办法配置 gemfire 也以相同的方式运行?
简短的回答是否;不完全是。
我也不完全确定您的以下陈述是否完全准确...
Our experience with Guava ( or Hazelcast, redis etc ) is that , we don't need to explicitly define the caches.
对于 Hazelcast,我知道这不是来自 recent experience (see configuration, and specifically this line 的 true。 第 78 行 是绝对必要的(以某种形式或形式,例如 XML);没有它,Spring 的 缓存抽象将抛出异常。
虽然我没有测试 Redis 作为缓存提供程序,但 Redis 似乎可以处理 dynamic Cache
creation (also this)。
Guava 很可能与 ConcurrentMapCacheManager
implementation, does not require pre-existing Caches
be explicitly defined since the ConcurrentMapCacheManager
will dynamically create the Cache
( a ConcurrentHashMap
) at runtime when requested if NOT explicitly "named" 一样。但是,如果 Caches
预先明确命名,则如果 Cache
尚未定义(即 "named"),则会抛出异常。
我有其他缓存提供程序 here 的示例和测试,这些示例和测试说明了 Spring 的 缓存抽象在实践中的不同且相当独特的 UC,已确定通过测试 class 或测试用例名称。
但是,在所有 Pivotal GemFire 或 Apache Geode 测试示例中,您必须明确创建将作为 Spring 中的“Cache
”的区域 缓存基础设施。虽然,SDG 的 GemfireCacheManager
实施将 dynamically create Spring Cache
对象(由基础区域支持),这是 Spring 所需的 AOP CacheInterceptor
.
这将导致以下最小的必要配置,以启用 GemFire/Geode 作为提供者的缓存...
@SpringBootApplication
@EnableCaching
class MyCachingApplication {
public static void main(String[] args) {
SpringApplication.run(MyCachingApplication.class, args);
}
@Bean
GemfireCacheManager cacheManager(GemFireCache gemfireCache) {
GemfireCacheManager cacheManager = new GemfireCacheManager();
cacheManager.setCache(gemfireCache);
return cacheManager;
}
// define all Region beans required by the application including Regions
// used specifically in Spring's Cache Abstraction
}
现在,话虽如此,我已经基于整个声明的应用程序 [服务] 组件中使用的 Spring 缓存抽象注释(例如 @Cacheable
)创建了动态区域原型,从开始可以看出有了这个 test. Here is the configuration.
如您所见,在 Spring 的缓存基础结构中将作为 Caches
的 GemFire 区域没有明确的 bean 定义。然而,应用程序的(测试的)Spring @Service
组件确实 make use of caching.
动态区域的创建是通过使用 Spring BeanPostProcessor
(here) and GemFire Functions (using SDG's Function annotation support) to dynamically create the Regions at runtime, during startup. The Function execution is defined here, and the actual Function implementation is defined here.
此示例非常粗糙(即不处理 DataPolicy
之外的自定义区域配置(例如 eviction/expiration、持久性、溢出等),当前设置为处理对等缓存拓扑(即测试应用程序是 GemFire DS 中的对等 member/node)。
然而,将这个原型扩展到 client/server 拓扑中是很容易的,考虑到所有 Spring 和 JSR-107 缓存注释并允许更多自定义区域配置。
及时,这可能是我添加到 SDG 框架本身的东西。
希望这对您有所帮助。
干杯,
约翰
我有一个 spring 应用程序使用 spring 基于 Guava 缓存的缓存。由于高吞吐量需求和后写功能,我们现在正在考虑迁移到 Gemfire。 我成功地将 Gemfire 配置为缓存,并且能够从缓存中读取和写入。在所有配置示例中,配置需要定义 LocalRegionFactory 如下:
@Bean
public Region<Long,Person> myPersonRegion(LocalRegionFactoryBean<Long, Person> personRegion) throws Exception {
return personRegion.getObject();
}
@Bean
public LocalRegionFactoryBean<Long, Person> personRegion(GemFireCache cache,AsyncEventQueue gemfireQueue) {
LocalRegionFactoryBean<Long, Person> personRegion = new LocalRegionFactoryBean<>();
personRegion.setCache(cache);
personRegion.setClose(false);
personRegion.setName("person");
personRegion.setAsyncEventQueues(new AsyncEventQueue[]{gemfireQueue});
personRegion.setPersistent(false);
return personRegion;
}
bean定义好后,我们可以使用@Cacheable(value="person"),@CacheEvict(value="person")。如果我们直接使用缓存名称,gemfire 会抛出缓存未定义的错误。
我们使用 Guava(或 Hazelcast、redis 等)的经验是,我们不需要显式定义缓存。它会在第一次出现时由 spring 自动创建。
有没有办法配置 gemfire 也以相同的方式运行?
简短的回答是否;不完全是。
我也不完全确定您的以下陈述是否完全准确...
Our experience with Guava ( or Hazelcast, redis etc ) is that , we don't need to explicitly define the caches.
对于 Hazelcast,我知道这不是来自 recent experience (see configuration, and specifically this line 的 true。 第 78 行 是绝对必要的(以某种形式或形式,例如 XML);没有它,Spring 的 缓存抽象将抛出异常。
虽然我没有测试 Redis 作为缓存提供程序,但 Redis 似乎可以处理 dynamic Cache
creation (also this)。
Guava 很可能与 ConcurrentMapCacheManager
implementation, does not require pre-existing Caches
be explicitly defined since the ConcurrentMapCacheManager
will dynamically create the Cache
( a ConcurrentHashMap
) at runtime when requested if NOT explicitly "named" 一样。但是,如果 Caches
预先明确命名,则如果 Cache
尚未定义(即 "named"),则会抛出异常。
我有其他缓存提供程序 here 的示例和测试,这些示例和测试说明了 Spring 的 缓存抽象在实践中的不同且相当独特的 UC,已确定通过测试 class 或测试用例名称。
但是,在所有 Pivotal GemFire 或 Apache Geode 测试示例中,您必须明确创建将作为 Spring 中的“Cache
”的区域 缓存基础设施。虽然,SDG 的 GemfireCacheManager
实施将 dynamically create Spring Cache
对象(由基础区域支持),这是 Spring 所需的 AOP CacheInterceptor
.
这将导致以下最小的必要配置,以启用 GemFire/Geode 作为提供者的缓存...
@SpringBootApplication
@EnableCaching
class MyCachingApplication {
public static void main(String[] args) {
SpringApplication.run(MyCachingApplication.class, args);
}
@Bean
GemfireCacheManager cacheManager(GemFireCache gemfireCache) {
GemfireCacheManager cacheManager = new GemfireCacheManager();
cacheManager.setCache(gemfireCache);
return cacheManager;
}
// define all Region beans required by the application including Regions
// used specifically in Spring's Cache Abstraction
}
现在,话虽如此,我已经基于整个声明的应用程序 [服务] 组件中使用的 Spring 缓存抽象注释(例如 @Cacheable
)创建了动态区域原型,从开始可以看出有了这个 test. Here is the configuration.
如您所见,在 Spring 的缓存基础结构中将作为 Caches
的 GemFire 区域没有明确的 bean 定义。然而,应用程序的(测试的)Spring @Service
组件确实 make use of caching.
动态区域的创建是通过使用 Spring BeanPostProcessor
(here) and GemFire Functions (using SDG's Function annotation support) to dynamically create the Regions at runtime, during startup. The Function execution is defined here, and the actual Function implementation is defined here.
此示例非常粗糙(即不处理 DataPolicy
之外的自定义区域配置(例如 eviction/expiration、持久性、溢出等),当前设置为处理对等缓存拓扑(即测试应用程序是 GemFire DS 中的对等 member/node)。
然而,将这个原型扩展到 client/server 拓扑中是很容易的,考虑到所有 Spring 和 JSR-107 缓存注释并允许更多自定义区域配置。
及时,这可能是我添加到 SDG 框架本身的东西。
希望这对您有所帮助。
干杯, 约翰