关于spring:Spring认证中国教育管理中心Apache-Geode-的-Spring-数据教程五

5次阅读

共计 7960 个字符,预计需要花费 20 分钟才能阅读完成。

原题目:Spring 认证中国教育管理中心 -Apache Geode 的 Spring 数据教程五(Spring 中国教育管理中心)

5.5.9. 数据过期
Apache Geode 容许您管制条目在缓存中存在的工夫。到期是由通过的工夫驱动的,而不是驱赶,后者是由条目计数或堆或内存应用状况驱动的。一旦条目过期,就不能再从缓存中拜访它。

Apache Geode 反对以下过期类型:

生存工夫 (TTL):对象在上次创立或更新后能够保留在缓存中的工夫量(以秒为单位)。对于条目,创立和搁置操作的计数器设置为零。区域计数器在创立区域和条目标计数器重置时重置。
闲暇超时 (TTI):对象在上次访问后能够保留在缓存中的工夫量(以秒为单位)。每当重置其 TTL 计数器时,对象的闲暇超时计数器就会重置。此外,每当通过 get 操作或 netSearch. 每当为其条目之一重置闲暇超时时,区域的闲暇超时计数器就会重置。
这些中的每一个都能够利用于区域自身或区域中的条目。Spring Data for Apache Geode 提供 <region-ttl>、<region-tti>、<entry-ttl> 和 <entry-tti>Region 子元素来指定超时值和到期操作。

以下示例显示 PARTITION 设置了到期值的区域:

<gfe:partitioned-region id=”examplePartitionRegionWithExpiration”>
<gfe:region-ttl timeout=”30000″ action=”INVALIDATE”/>
<gfe:entry-tti timeout=”600″ action=”LOCAL_DESTROY”/>
</gfe:replicated-region>
无关过期策略的具体阐明,请参阅无关过期的 Apache Geode 文档。

基于正文的数据过期
应用 Spring Data for Apache Geode,您能够为各个 Region 条目值定义过期策略和设置(或者,换句话说,间接在应用程序域对象上)。例如,您能够在基于会话的应用程序域对象上定义过期策略,如下所示:

@Expiration(timeout = “1800”, action = “INVALIDATE”)
public class SessionBasedApplicationDomainObject {

}
您还能够别离应用 Idle Timeout (TTI) 和 Time-to-Live (TTL) 过期的 @IdleTimeoutExpiration 和 @TimeToLiveExpiration 正文来指定区域条目上的过期类型特定设置,如以下示例所示:

@TimeToLiveExpiration(timeout = “3600”, action = “LOCAL_DESTROY”)
@IdleTimeoutExpiration(timeout = “1800”, action = “LOCAL_INVALIDATE”)
@Expiration(timeout = “1800”, action = “INVALIDATE”)
public class AnotherSessionBasedApplicationDomainObject {

}
二者 @IdleTimeoutExpiration 并 @TimeToLiveExpiration 优先于个别的 @Expiration 正文当多于一个到期正文类型被指定时,如图所示的后面的例子英寸 既不 @IdleTimeoutExpiration 也不 @TimeToLiveExpiration 笼罩另一个。相同,当配置了不同的 Region 条目过期策略(例如 TTL 和 TTI)时,它们会互相补充。

@Expiration 基于所有的正文仅实用于区域条目值。Spring Data for Apache Geode 的过期正文反对不涵盖区域的过期。然而,Apache Geode 和 Spring Data for Apache Geode 的确容许您应用 SDG XML 命名空间设置区域到期工夫,如下所示:

<gfe:*-region id=”Example” persistent=”false”>
<gfe:region-ttl timeout=”600″ action=”DESTROY”/>
<gfe:region-tti timeout=”300″ action=”INVALIDATE”/>
</gfe:*-region>
Spring Data for Apache Geode 的 @Expiration 注解反对是通过 Apache Geode 的 CustomExpiry 接口实现的。无关 更多详细信息,请参阅 Apache Geode 无关配置数据过期的文档

Spring Data for Apache
GeodeAnnotationBasedExpiration 类(和 CustomExpiry 实现)负责解决 SDG@Expiration 正文,并依据申请为 Region 条目过期适当地利用过期策略配置。

要应用 Spring Data for Apache Geode 配置特定的 Apache Geode Regions 以将过期策略适当地利用于应用 @Expiration 基于正文的应用程序域对象,您必须:

通过应用适当的构造函数或不便的工厂办法之一,在 SpringApplicationContext 类型中定义一个 bean AnnotationBasedExpiration。在为特定的到期类型(例如闲暇超时 (TTI) 或生存工夫 (TTL))配置到期时,您应该应用 AnnotationBasedExpiration 类中的工厂办法之一,如下所示:<bean id=”ttlExpiration” class=”org.springframework.data.gemfire.expiration.AnnotationBasedExpiration” factory-method=”forTimeToLive”/> <gfe:partitioned-region id=”Example” persistent=”false”> <gfe:custom-entry-ttl ref=”ttlExpiration”/> </gfe:partitioned-region>
要配置闲暇超时 (TTI) 到期工夫,请应用 forIdleTimeout 工厂办法和 <gfe:custom-entry-tti ref=”ttiExpiration”/> 元素来设置 TTI。

(可选)应用 Spring Data for Apache Geode 的 @Expiration 正文之一,应用过期策略和自定义设置对存储在区域中的应用程序域对象进行正文:@Expiration, @IdleTimeoutExpiration, 或 @TimeToLiveExpiration
(可选)如果特定应用程序域对象基本没有应用 Spring Data for Apache Geode 的 @Expiration 注解进行注解,但 Apache Geode 区域配置为应用 SDG 的自定义 AnnotationBasedExpiration 类来确定存储在区域中的对象的过期策略和设置,您能够 AnnotationBasedExpiration 通过执行以下操作在 bean 上设置“默认”过期属性:
<bean id=”defaultExpirationAttributes” class=”org.apache.geode.cache.ExpirationAttributes”>

<constructor-arg value="600"/>
<constructor-arg value="#{T(org.apache.geode.cache.ExpirationAction).DESTROY}"/>

</bean>

<bean id=”ttiExpiration” class=”org.springframework.data.gemfire.expiration.AnnotationBasedExpiration”

  factory-method="forIdleTimeout">
<constructor-arg ref="defaultExpirationAttributes"/>

</bean>

<gfe:partitioned-region id=”Example” persistent=”false”>

<gfe:custom-entry-tti ref="ttiExpiration"/>

</gfe:partitioned-region>
您可能曾经留神到 Spring Data for Apache Geode 的 @Expiration 注解应用 aString 作为属性类型,而不是(兴许更适合的)强类型——例如,int 对于 ‘timeout’ 和 SDG’s ExpirationActionTypefor ‘action’。这是为什么?

好吧,输出 Spring Data for Apache Geode 的其余性能之一,利用 Spring 的外围基础设施来不便配置:属性占位符和 Spring 表达式语言 (SpEL) 表达式。

例如,开发人员能够通过在 @Expiration 正文属性中应用属性占位符来指定到期“超时”和“操作”,如以下示例所示:

@TimeToLiveExpiration(timeout = “${geode.region.entry.expiration.ttl.timeout}”

action = "${geode.region.entry.expiration.ttl.action}")

public class ExampleApplicationDomainObject {

}
而后,在您的 Spring XML 配置或 JavaConfig 中,您能够申明以下 bean:

<util:properties id=”expirationSettings”>
<prop key=”geode.region.entry.expiration.ttl.timeout”>600</prop>
<prop key=”geode.region.entry.expiration.ttl.action”>INVALIDATE</prop>

</util:properties>

<context:property-placeholder properties-ref=”expirationProperties”/>
当多个应用程序域对象可能共享类似的过期策略时以及当您心愿将配置内部化时,这都很不便。

然而,您可能须要由正在运行的零碎的状态确定的更多动静到期配置。这就是 SpEL 的弱小之处,实际上也是举荐的办法。您不仅能够在 Spring 容器中援用 bean 并拜访 bean 属性、调用办法等,而且过期 ‘timeout’ 和 ‘action’ 的值能够是强类型的。思考以下示例(基于后面的示例):

<util:properties id=”expirationSettings”>
<prop key=”geode.region.entry.expiration.ttl.timeout”>600</prop>
<prop key=”geode.region.entry.expiration.ttl.action”>#{T(org.springframework.data.gemfire.expiration.ExpirationActionType).DESTROY}</prop>
<prop key=”geode.region.entry.expiration.tti.action”>#{T(org.apache.geode.cache.ExpirationAction).INVALIDATE}</prop>

</util:properties>

<context:property-placeholder properties-ref=”expirationProperties”/>
而后,在您的应用程序域对象上,您能够定义超时和操作,如下所示:

@TimeToLiveExpiration(timeout = “@expirationSettings[‘geode.region.entry.expiration.ttl.timeout’]”

action = "@expirationSetting['geode.region.entry.expiration.ttl.action']")

public class ExampleApplicationDomainObject {

}
您能够设想,“expirationSettings”bean 可能是一个比 的简略实例更乏味和有用的对象 java.util.Properties。在后面的示例中,properties 元素 (expirationSettings) 应用 SpEL 将操作值建设在理论 ExpirationAction 枚举类型的根底上,如果枚举类型发生变化,则会迅速导致辨认失败。

例如,所有这些都已在 Spring Data for Apache Geode 测试套件中进行了演示和测试。无关更多详细信息,请参阅 起源。

5.5.10. 数据长久化
区域能够是长久的。Apache Geode 确保您放入配置为持久性的区域的所有数据都以可在您下次从新创立区域时复原的形式写入磁盘。这样做能够让数据在机器或过程失败后,甚至在 Apache Geode 数据节点有序敞开和随后重新启动后复原。

要应用 Spring Data for Apache Geode 启用持久性,请将任何元素上的 persistent 属性设置为,如以下示例所示:true<*-region>

<gfe:partitioned-region id=”examplePersitentPartitionRegion” persistent=”true”/>
也能够通过设置 data-policy 属性来配置持久性。为此,请将属性值设置为 Apache Geode 的 DataPolicy 设置之一,如以下示例所示:

<gfe:partitioned-region id=”anotherExamplePersistentPartitionRegion” data-policy=”PERSISTENT_PARTITION”/>
在 DataPolicy 肯定的区域类型匹配,并且还必须与批准 persistent 的属性,如果它也明确设置。如果该 persistent 属性设置为 false 但 DataPolicy 指定了持久性(例如 PERSISTENT_REPLICATE 或 PERSISTENT_PARTITION),则会引发初始化异样。

为了在长久化区域时取得最大效率,您应该通过 disk-store 元素配置存储。在 DiskStore 通过应用援用的 disk-store-ref 属性。此外,该区域能够同步或异步执行磁盘写入。以下示例显示了一个同步 DiskStore:

<gfe:partitioned-region id=”yetAnotherExamplePersistentPartitionRegion” persistent=”true”

disk-store-ref="myDiskStore" disk-synchronous="true"/>

这将在配置 DiskStore 中进一步探讨。

5.5.11. 订阅政策
Apache Geode 容许配置点对点 (P2P) 事件消息传递 来管制区域接管的入口事件。Spring Data for Apache Geode 提供了 <gfe:subscription/> 子元素来将订阅策略 REPLICATE 和 PARTITION 区域设置为 ALL 或 CACHE_CONTENT。以下示例显示了其订阅策略设置为 的区域 CACHE_CONTENT:

<gfe:partitioned-region id=”examplePartitionRegionWithCustomSubscription”>
<gfe:subscription type=”CACHE_CONTENT”/>
</gfe:partitioned-region>
5.5.12. 本地区域
Spring Data for Apache Geode 提供了 local-region 用于创立本地区域的专用元素。顾名思义,本地区域是独立的,这意味着它们不与任何其余分布式系统成员共享数据。除此之外,所有常见的区域配置选项都实用。

以下示例显示了一个最小申明(同样,该示例依赖 Spring Data for Apache Geode XML 命名空间命名约定来连贯缓存):

<gfe:local-region id=”exampleLocalRegion”/>
在后面的示例中,创立了一个本地 Region(如果同名的 Region 尚不存在)。Region 的名称与 bean ID (exampleLocalRegion) 雷同,并且 bean 假设存在名为 的 Apache Geode 缓存 gemfireCache。

5.5.13. 复制区域
一种常见的 Region 类型是 REPLICATERegion 或“正本”。简而言之,当一个区域被配置为 a 时 REPLICATE,承载该区域的每个成员都会在本地存储该区域条目标正本。对 REPLICATE 区域的任何更新都会散发到该区域的所有正本。创立正本时,它会经验一个初始化阶段,在此阶段它会发现其余正本并主动复制所有条目。当一个正本正在初始化时,您依然能够持续应用其余正本。

所有常见的配置选项都可用于 REPLICATE 区域。Spring Data for Apache Geode 提供了一个 replicated-region 元素。以下示例显示了一个最小申明:

<gfe:replicated-region id=”exampleReplica”/>
无关 更多详细信息,请参阅 Apache Geode 对于分布式和复制区域的文档。

5.5.14. 分区区域
Spring Data for Apache Geode XML 命名空间也反对 PARTITION 区域。

援用 Apache Geode 文档:

“分区区域是数据在托管该区域的对等服务器之间划分的区域,以便每个对等服务器存储数据的子集。应用分区区域时,应用程序会显示区域的逻辑视图,该视图看起来像蕴含该区域中所有数据的单个地图。对此映射的读取或写入通明地路由到承载作为操作指标的条目标对等方。Apache Geode 将哈希码域划分为桶。每个桶都调配给一个特定的对等点,但能够随时从新定位到另一个对等点,以进步整个集群的资源利用率。”

阿 PARTITION 区域通过应用所创立 partitioned-region 的元素。它的配置选项与 的相似 replicated-region,但减少了特定于分区的性能,例如冗余正本数、最大总内存、桶数、分区解析器等。

以下示例显示如何设置 PARTITION 具备两个冗余正本的区域:

<gfe:partitioned-region id=”examplePartitionRegion” copies=”2″ total-buckets=”17″>
<gfe:partition-resolver>

<bean class="example.PartitionResolver"/>

</gfe:partition-resolver>
</gfe:partitioned-region>
无关 更多详细信息,请参阅 Apache Geode 对于分区区域的文档。

分区区域属性
下表提供了特定于 PARTITION 区域的配置选项的疾速概览。这些选项是对后面形容的常见区域配置选项的补充。

Spring 认证中国教育管理中心 -Apache Geode 的 Spring 数据教程五

正文完
 0