有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringDataGemFire:自定义过期示例

我正在使用关键的GemFire 9.1.1和Spring数据GemFire 2.0.7。释放

我有一个令牌,它将使用String键和Map<String,String>值存储在GemFire区域中。令牌的过期(即GemFire区域中的条目)应该是动态的,取决于几个业务场景

我可以找到CustomExpiry的关键GemFire文档,但是我找不到任何关于Spring数据GemFire(<gfe:custom-entry-ttl>)的适当示例/文档

如果有资源指示如何在SpringDataGemFire中启用自定义数据过期,请共享


共 (1) 个答案

  1. # 1 楼答案

    开发人员使用SDG中的GemFire的^{}接口为一个区域配置自定义过期策略实际上有3种不同的方式

    给出了o.a.g.cacheCustomExpiry的特定于应用程序的实现

    package example.app.gemfire.cache;
    
    import org.apache.geode.cache.CustomExpiry;
    import org.apache.geode.cache.ExpirationAttributes;
    import ...;
    
    class MyCustomExpiry implements CustomExpiry<String, Object> {
    
      ExpirationAttributes getExpiry(Region.Entry<String, Object> entry) {
        ...
      }
    }
    

    首先,XML方法

    <bean id="customTimeToLiveExpiration" 
          class="example.app.gemfire.cache.MyCustomExpiry"/>
    
    <gfe:partitioned-region id="Example" persistent="false">
      <gfe:custom-entry-ttl ref="customTimeToLiveExpiration"/>
      <gfe:custom-entry-tti>
        <bean class="example.app.gemfire.cache.MyCustomExpiry"/>
      </gfe:custom-entry-tti>
    </gfe:partitioned-region>
    

    正如您在上面的示例中所看到的,您可以使用bean引用(如嵌套生存时间(TTL)过期策略声明)或使用匿名bean定义(如“示例”分区bean定义的嵌套空闲超时(TTI)过期策略)来定义“自定义”过期策略

    有关精确定义,请参阅SDGXML schema

    第二,您可以实现与Java配置相同的功能

    @Configuration
    class GemFireConfiguration {
    
      @Bean
      MyCustomExpiry customTimeToLiveExpiration() {
        return new MyCustomExpiry();
      }
    
      @Bean("Example")
      PartitionedRegionFactoryBean<String, Object> exampleRegion(
          GemFireCache gemfireCache) {
    
        PartitionedRegionFactoryBean<String, Object> exampleRegion =
          new PartitionedRegionFactoryBean<>();
    
        exampleRegion.setCache(gemfireCache);
        exampleRegion.setClose(false);
        exampleRegion.setPersistent(false);
        exampleRegion.setCustomEntryTimeToLive(customTimeToLiveExpiration());
        exampleRegion.setCustomEntryIdleTimeout(new MyCustomExpiry());
    
        return exampleRegion;
      }
    }
    

    最后,使用基于SDG注释的过期配置(如here所定义)配置TTL和TTI过期策略。SDG测试套件中有一个test classconfiguration来演示此功能

    有关SDG中基于注释的过期配置的更多信息,请参见here

    希望这有帮助

    -约翰