Spring Data GemFire: CustomExpiry Examples

自作多情 提交于 2019-12-13 04:43:05

问题


I am using Pivotal GemFire 9.1.1 and Spring Data GemFire 2.0.7.RELEASE.

I have a token that will be stored in a GemFire Region with a String Key and a Map<String,String> Value. The expiration of the token (i.e. entry in the GemFire Region) should be dynamic dependent on a few business scenarios.

I could find Pivotal GemFire documentation for CustomExpiry whereas I could not find any proper example/documentation on Spring Data GemFire (<gfe:custom-entry-ttl>).

Please share if there is a resource which instructs how to enable custom data expiration in Spring Data GemFire.


回答1:


There are actually 3 different ways a developer can configure a custom expiration policy for a Region using Pivotal GemFire's o.a.g.cache.CustomExpiry interface in SDG

Given an application-specific implementation of 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) {
    ...
  }
}

First, the XML approach.

<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>

As you can see in the example above, you can define "custom" Expiration policies using either a bean reference, as in the nested Time-To-Live (TTL) Expiration Policy declaration or by using a anonymous bean definition, as in the nested Idle Timeout (TTI) Expiration Policy of the "Example" PARTITION Region bean definition.

Refer to the SDG XML schema for precise definitions.

Second, you can achieve the same thing Java Config...

@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;
  }
}

Finally, you configure both TTL and TTI Expiration Policies using SDG Annotation-based Expiration configuration, as defined here. There is a test class along with the configuration in the SDG test suite demonstrating this capability.

Additional information about Annotation-based Expiration configuration in SDG can be found here.

Hope this helps!

-John



来源:https://stackoverflow.com/questions/50688581/spring-data-gemfire-customexpiry-examples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!