1

我有一个使用基于 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 也以同样的方式运行?

4

1 回答 1

1

简短的回答是否定的;不完全是。

我也不完全确定您的以下陈述是否完全准确......

我们使用 Guava(或 Hazelcast、redis 等)的经验是,我们不需要显式定义缓存。

对于 Hazelcast,根据最近的经验,我知道这不是真的(请参阅配置,特别是这一行)。 第 78 行是绝对必要的(以某种形式或形式,例如 XML);没有它,Spring 的缓存抽象将抛出异常。

虽然我没有测试 Redis 作为缓存提供程序,但 Redis 似乎可以处理动态Cache创建(也是this)。

Guava 很可能像ConcurrentMapCacheManager实现一样,不需要Caches显式定义预先存在的,因为如果没有显式“命名”ConcurrentMapCacheManager ,它将在运行时动态创建Cache( a ) 。但是,如果事先明确命名,则如果尚未定义(即“命名”),则会引发异常。ConcurrentHashMapCachesCache

我在这里有其他缓存提供程序的示例和测试,它们说明了Spring 缓存抽象在实践中的不同且相当独特的 UC ,由测试类或测试用例名称标识。

但是,在所有 Pivotal GemFire 或 Apache Geode 测试示例中,您必须显式创建将用作Spring缓存基础架构中的“ Cache”的区域。虽然,SDG 的实现将动态创建Spring 的 AOP所需的Spring对象(由底层 Region 支持) 。GemfireCacheManagerCacheCacheInterceptor

这会产生以下最少的必要配置,以启用以 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 Cache Abstraction 注释(例如)创建了动态区域创建原型,从这个测试@Cacheable开始可以看出。这是配置

如您所见,GemFire 区域没有明确的 bean 定义,它们将用作CachesSpring 的缓存基础设施。然而,应用程序的(测试的)Spring@Service组件确实使用了缓存

动态区域创建是通过使用Spring BeanPostProcessor此处)和 GemFire 函数(使用 SDG 的函数注释支持)在启动期间在运行时动态创建区域来完成的。函数执行在这里定义,实际的函数实现在这里定义

这个例子非常粗略(即不处理自定义区域配置(例如驱逐/过期、持久性、溢出等)超出DataPolicy)并且当前设置为处理对等缓存拓扑(即测试应用程序是对等成员/节点宝石火 DS)。

但是,很容易扩展这个原型以用于客户端/服务器拓扑,考虑到所有SpringJSR-107缓存注释并允许更多自定义区域配置。

随着时间的推移,这可能是我添加到可持续发展目标框架本身的东西。

希望这可以帮助。

干杯,约翰

于 2016-09-06T19:01:14.650 回答