Spring認(rèn)證中國教育管理中心-Apache Geode 的 Spring 數(shù)據(jù)教程五

原標(biāo)題:spring認(rèn)證中國教育管理中心-apache geode 的 spring 數(shù)據(jù)教程五(spring中國教育管理中心)

5.5.9.數(shù)據(jù)過期

apache Geode 允許您控制條目在緩存中的存留時(shí)間。過期是由經(jīng)過的時(shí)間驅(qū)動(dòng)的,而不是驅(qū)逐,后者是由條目數(shù)量或或內(nèi)存使用情況驅(qū)動(dòng)的。一旦條目過期,它將無法再從緩存中訪問。

Apache Geode 支持以下過期類型:

生存時(shí)間 (TTL):對(duì)象在上次創(chuàng)建或更新后可以在緩存中保留的時(shí)間量(以秒為單位)。對(duì)于條目,創(chuàng)建和放置操作的計(jì)數(shù)器被設(shè)置為零。區(qū)域計(jì)數(shù)器在創(chuàng)建區(qū)域和條目的計(jì)數(shù)器重置時(shí)重置。空閑超時(shí) (TTI):對(duì)象在上次訪問后可以在緩存中保留的時(shí)間量(以秒為單位)。每當(dāng)重置其 TTL 計(jì)數(shù)器時(shí),對(duì)象的空閑超時(shí)計(jì)數(shù)器也會(huì)重置。此外,每當(dāng)通過 get 操作或 netSearch 訪問時(shí),對(duì)象的空閑超時(shí)計(jì)數(shù)器也會(huì)重置。每當(dāng)為其條目之一重置空閑超時(shí)時(shí),區(qū)域的空閑超時(shí)計(jì)數(shù)器也會(huì)重置。這些過期類型可以應(yīng)用于區(qū)域本身或區(qū)域中的條目。Spring Data for Apache Geode 提供了 Region 子元素來指定超時(shí)值和到期操作。

以下示例展示了具有過期值設(shè)置的 PARTITION 區(qū)域:

<partitioned-region id="examplePartitionRegionWithExpiration">     <region-ttl action="INVALIDATE" timeout="30000"></region-ttl>     <entry-tti action="LOCAL_DESTROY" timeout="600"></entry-tti> </partitioned-region>

有關(guān)過期策略的詳細(xì)說明,請(qǐng)參閱 Apache Geode 的過期文檔。

基于注釋的數(shù)據(jù)過期

使用 Spring Data for Apache Geode,您可以為各個(gè) Region 條目值定義過期策略和設(shè)置(或者,換句話說,直接在應(yīng)用程序域?qū)ο笊希@纾梢栽诨跁?huì)話的應(yīng)用程序域?qū)ο笊隙x過期策略,如下所示:

@Expiration(timeout = "1800", action = "INVALIDATE") public class SessionBasedApplicationDomainObject {     ... }

您還可以使用 @IdleTimeoutExpiration 和 @TimeToLiveExpiration 注釋分別指定區(qū)域條目上的空閑超時(shí) (TTI) 和生存時(shí)間 (TTL) 過期的特定設(shè)置,如以下示例所示:

@TimeToLiveExpiration(timeout = "3600", action = "LOCAL_DESTROY") @IdleTimeoutExpiration(timeout = "1800", action = "LOCAL_INVALIDATE") @Expiration(timeout = "1800", action = "INVALIDATE") public class AnotherSessionBasedApplicationDomainObject {     ... }

當(dāng)多個(gè)到期注釋類型被指定時(shí),@IdleTimeoutExpiration 和 @TimeToLiveExpiration 優(yōu)先于一般的 @Expiration 注釋。它們不會(huì)相互覆蓋,而是補(bǔ)充不同的 Region 條目過期策略(例如 TTL 和 TTI)。

@Expiration 注釋僅適用于區(qū)域條目值。Spring Data for Apache Geode 的過期注釋支持不涵蓋區(qū)域的過期。但是,Apache Geode 和 Spring Data for Apache Geode 確實(shí)允許您使用 SDG xml 命名空間設(shè)置區(qū)域到期時(shí)間,如下所示:

<gfe:*-region id="Example" persistent="false">     <region-ttl action="DESTROY" timeout="600"></region-ttl>     <region-tti action="INVALIDATE" timeout="300"></region-tti> </gfe:*-region>

Spring Data for Apache Geode 的 @Expiration 注解支持是通過 Apache Geode 的 CustomExpiry 接口實(shí)現(xiàn)的。有關(guān)更多詳細(xì)信息,請(qǐng)參閱 Apache Geode 關(guān)于配置數(shù)據(jù)過期的文檔。

Spring Data for Apache Geode 的 AnnotationBasedExpiration 類(和 CustomExpiry 實(shí)現(xiàn))負(fù)責(zé)處理 SDG @Expiration 注釋,并根據(jù)請(qǐng)求為 Region 條目過期適當(dāng)?shù)貞?yīng)用過期策略配置。

要使用 Spring Data for Apache Geode 配置特定的 Apache Geode Regions 以將過期策略適當(dāng)?shù)貞?yīng)用于使用 @Expiration 基于注釋的應(yīng)用程序域?qū)ο螅仨殻?/p>

在 Spring ApplicationContext 中定義一個(gè) AnnotationBasedExpiration 類型的 bean,通過使用適當(dāng)?shù)?a href="http://www.babyishan.com/tag/%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0">構(gòu)造函數(shù)或方便的工廠方法之一。在為特定的到期類型(例如空閑超時(shí) (TTI) 或生存時(shí)間 (TTL))配置到期時(shí),您應(yīng)該使用 AnnotationBasedExpiration 類中的工廠方法之一,如下所示:

<bean class="org.springframework.data.gemfire.expiration.AnnotationBasedExpiration" factory-method="forTimeToLive" id="ttlExpiration"></bean> <partitioned-region id="Example" persistent="false">     <custom-entry-ttl ref="ttlExpiration"></custom-entry-ttl> </partitioned-region>

要配置空閑超時(shí) (TTI) 到期時(shí)間,請(qǐng)使用 forIdleTimeout 工廠方法和 元素來設(shè)置 TTI。

(可選)使用 Spring Data for Apache Geode 的 @Expiration 注釋之一,使用過期策略和自定義設(shè)置對(duì)存儲(chǔ)在區(qū)域中的應(yīng)用程序域?qū)ο筮M(jìn)行注釋:@Expiration, @IdleTimeoutExpiration, 或 @TimeToLiveExpiration。

(可選)如果特定應(yīng)用程序域?qū)ο蟾緵]有使用 Spring Data for Apache Geode 的 @Expiration 注解進(jìn)行注解,但 Apache Geode 區(qū)域配置為使用 SDG 的自定義 AnnotationBasedExpiration 類來確定存儲(chǔ)在區(qū)域中的對(duì)象的過期策略和設(shè)置,您可以通過在 bean 上設(shè)置“默認(rèn)”過期屬性來實(shí)現(xiàn),如下所示:

<bean class="org.apache.geode.cache.ExpirationAttributes" id="defaultExpirationAttributes">     <constructor-arg value="600"></constructor-arg>     <constructor-arg value="#{T(org.apache.geode.cache.ExpirationAction).DESTROY}"></constructor-arg> </bean> <bean class="org.springframework.data.gemfire.expiration.AnnotationBasedExpiration" factory-method="forIdleTimeout" id="ttiExpiration">     <constructor-arg ref="defaultExpirationAttributes"></constructor-arg> </bean> <partitioned-region id="Example" persistent="false">     <custom-entry-tti ref="ttiExpiration"></custom-entry-tti> </partitioned-region>

您可能已經(jīng)注意到 Spring Data for Apache Geode 的 @Expiration 注解使用 String 作為屬性類型,而不是(可能更合適的)強(qiáng)類型——例如,int 對(duì)于 ‘timeout’ 和 SDG’s ExpirationActionType 對(duì)于 ‘action’。這是為什么

好吧,輸入 Spring Data for Apache Geode 的其他功能之一,利用 Spring 的核心基礎(chǔ)設(shè)施來方便配置:屬性占位符和 Spring 表達(dá)式語言 (SpEL) 表達(dá)式。

例如,開發(fā)人員可以通過在 @Expiration 注釋屬性中使用屬性占位符來指定到期“超時(shí)”和“操作”,如以下示例所示:

@TimeToLiveExpiration(timeout = "${geode.region.entry.expiration.ttl.timeout}"     action = "${geode.region.entry.expiration.ttl.action}") public class ExampleApplicationDomainObject {     ... }

然后,在您的 Spring XML 配置或 JavaConfig 中,您可以聲明以下 bean:

<properties id="expirationSettings">     <prop key="geode.region.entry.expiration.ttl.timeout">600</prop>     <prop key="geode.region.entry.expiration.ttl.action">INVALIDATE</prop>     ... </properties> <property-placeholder properties-ref="expirationProperties"></property-placeholder>

當(dāng)多個(gè)應(yīng)用程序域?qū)ο罂赡芄蚕硐嗨频倪^期策略時(shí)以及當(dāng)您希望將配置外部化時(shí),這都很方便。

但是,您可能需要由正在運(yùn)行的系統(tǒng)的狀態(tài)確定的更多動(dòng)態(tài)到期配置。這就是 SpEL 的強(qiáng)大之處,實(shí)際上也是推薦的方法。您不僅可以在 Spring 容器中引用 bean 并訪問 bean 屬性、調(diào)用方法等,而且過期 ‘timeout’ 和 ‘action’ 的值可以是強(qiáng)類型的。考慮以下示例(基于前面的示例):

<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>     ... </properties> <property-placeholder properties-ref="expirationProperties"></property-placeholder>

然后,在您的應(yīng)用程序域?qū)ο笊希梢远x超時(shí)和操作,如下所示:

@TimeToLiveExpiration(timeout = "@expirationSettings['geode.region.entry.expiration.ttl.timeout']"     action = "@expirationSetting['geode.region.entry.expiration.ttl.action']") public class ExampleApplicationDomainObject {     ... }

您可以想象,“expirationSettings” bean 可能是一個(gè)比 java.util.Properties 的簡(jiǎn)單實(shí)例更有趣和有用的對(duì)象。在前面的示例中,properties 元素 (expirationSettings) 使用 SpEL 將操作值建立在實(shí)際 ExpirationAction 枚舉類型的基礎(chǔ)上,如果枚舉類型發(fā)生變化,則會(huì)迅速導(dǎo)致識(shí)別失敗。

例如,所有這些都已在 Spring Data for Apache Geode 測(cè)試套件中進(jìn)行了演示和測(cè)試。有關(guān)更多詳細(xì)信息,請(qǐng)參閱 來源。

5.5.10.數(shù)據(jù)持久化

區(qū)域可以是持久的。Apache Geode 確保您放入配置為持久性的區(qū)域的所有數(shù)據(jù)都以可在您下次重新創(chuàng)建區(qū)域時(shí)恢復(fù)的方式寫入磁盤。這樣做可以讓數(shù)據(jù)在機(jī)器或進(jìn)程失敗后,甚至在 Apache Geode 數(shù)據(jù)節(jié)點(diǎn)有序關(guān)閉和隨后重新啟動(dòng)后恢復(fù)。

要使用 Spring Data for Apache Geode 啟用持久性,請(qǐng)將任何元素上的 persistent 屬性設(shè)置為 true,如以下示例所示:

<partitioned-region id="examplePersitentPartitionRegion" persistent="true"></partitioned-region>

也可以通過設(shè)置 data-policy 屬性來配置持久性。為此,請(qǐng)將屬性值設(shè)置為 Apache Geode 的 DataPolicy 設(shè)置之一,如以下示例所示:

<partitioned-region data-policy="PERSISTENT_PARTITION" id="anotherExamplePersistentPartitionRegion"></partitioned-region>

在 DataPolicy 一定的區(qū)域類型匹配,并且還必須與同意 persistent 的屬性,如果它也明確設(shè)置。如果該 persistent 屬性設(shè)置為 false 但 DataPolicy 指定了持久性(例如 PERSISTENT_REPLICATE 或 PERSISTENT_PARTITION),則會(huì)引發(fā)初始化異常。

為了在持久化區(qū)域時(shí)獲得最大效率,您應(yīng)該通過 disk-store 元素配置存儲(chǔ)。在 DiskStore 通過使用引用的 disk-store-ref 屬性。此外,該區(qū)域可以同步或異步執(zhí)行磁盤寫入。以下示例顯示了一個(gè)同步 DiskStore:

<partitioned-region disk-store-ref="myDiskStore" disk-synchronous="true" id="yetAnotherExamplePersistentPartitionRegion" persistent="true"></partitioned-region>

這將在配置 DiskStore 中進(jìn)一步討論。

5.5.11.訂閱政策

Apache Geode 允許配置點(diǎn)對(duì)點(diǎn) (p2p) 事件消息傳遞 來控制區(qū)域接收的入口事件。Spring Data for Apache Geode 提供了 子元素來將訂閱策略 REPLICATE 和 PARTITION 區(qū)域設(shè)置為 ALL 或 CACHE_CONTENT。以下示例顯示了其訂閱策略設(shè)置為 CACHE_CONTENT 的區(qū)域:

<partitioned-region id="examplePartitionRegionWithCustomSubscription">     <subscription type="CACHE_CONTENT"></subscription> </partitioned-region>

5.5.12.本地區(qū)域

Spring Data for Apache Geode 提供了 local-region 用于創(chuàng)建本地區(qū)域的專用元素。顧名思義,本地區(qū)域是獨(dú)立的,這意味著它們不與任何其他分布式系統(tǒng)成員共享數(shù)據(jù)。除此之外,所有常見的區(qū)域配置選項(xiàng)都適用。

以下示例顯示了一個(gè)最小聲明(同樣,該示例依賴 Spring Data for Apache Geode XML 命名空間命名約定來連接緩存):

<local-region id="exampleLocalRegion"></local-region>

在前面的示例中,創(chuàng)建了一個(gè)本地 Region(如果同名的 Region 尚不存在)。Region 的名稱與 bean ID (exampleLocalRegion) 相同,并且 bean 假定存在名為 gemfireCache 的 Apache Geode 緩存。

5.5.13.復(fù)制區(qū)域

一種常見的 Region 類型是 REPLICATE Region 或“副本”。簡(jiǎn)而言之,當(dāng)一個(gè)區(qū)域被配置為 REPLICATE 時(shí),承載該區(qū)域的每個(gè)成員都會(huì)在本地存儲(chǔ)該區(qū)域條目的副本。對(duì) REPLICATE 區(qū)域的任何更新都會(huì)分發(fā)到該區(qū)域的所有副本。創(chuàng)建副本時(shí),它會(huì)經(jīng)歷一個(gè)初始化階段,在此階段它會(huì)發(fā)現(xiàn)其他副本并自動(dòng)復(fù)制所有條目。當(dāng)一個(gè)副本正在初始化時(shí),您仍然可以繼續(xù)使用其他副本。

所有常見的配置選項(xiàng)都可用于 REPLICATE 區(qū)域。Spring Data for Apache Geode 提供了一個(gè) replicated-region 元素。以下示例顯示了一個(gè)最小聲明:

<replicated-region id="exampleReplica"></replicated-region>

有關(guān)更多詳細(xì)信息,請(qǐng)參閱 Apache Geode 關(guān)于分布式和復(fù)制區(qū)域的文檔。

5.5.14.分區(qū)區(qū)域

Spring Data for Apache Geode XML 命名空間也支持 PARTITION 區(qū)域。

引用 Apache Geode 文檔:

“分區(qū)區(qū)域是數(shù)據(jù)在托管該區(qū)域的對(duì)等服務(wù)器之間劃分的區(qū)域,以便每個(gè)對(duì)等服務(wù)器存儲(chǔ)數(shù)據(jù)的子集。使用分區(qū)區(qū)域時(shí),應(yīng)用程序會(huì)顯示區(qū)域的邏輯視圖,該視圖看起來像包含該區(qū)域中所有數(shù)據(jù)的單個(gè)地圖。對(duì)此映射的讀取或?qū)懭胪该鞯芈酚傻匠休d作為操作目標(biāo)的條目的對(duì)等方。Apache Geode 將哈希碼域劃分為桶。每個(gè)桶都分配給一個(gè)特定的對(duì)等點(diǎn),但可以隨時(shí)重新定位到另一個(gè)對(duì)等點(diǎn),以提高整個(gè)集群的資源利用率。”

一個(gè) PARTITION 區(qū)域通過使用 partitioned-region 的元素創(chuàng)建。它的配置選項(xiàng)與 replicated-region 的類似,但增加了特定于分區(qū)的功能,例如冗余副本數(shù)、最大總內(nèi)存、桶數(shù)、分區(qū)解析器等。

以下示例顯示如何設(shè)置具有兩個(gè)冗余副本的 PARTITION 區(qū)域:

<partitioned-region copies="2" id="examplePartitionRegion" total-buckets="17">     <partition-resolver>         <bean class="example.PartitionResolver"></bean>     </partition-resolver> </partitioned-region>

有關(guān)更多詳細(xì)信息,請(qǐng)參閱 Apache Geode 關(guān)于分區(qū)區(qū)域的文檔。

分區(qū)區(qū)域?qū)傩?/p>

下表提供了特定于 PARTITION 區(qū)域的配置選項(xiàng)的快速概覽。這些選項(xiàng)是對(duì)前面描述的常見區(qū)域配置選項(xiàng)的補(bǔ)充。

Spring認(rèn)證中國教育管理中心-Apache Geode 的 Spring 數(shù)據(jù)教程五

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊8 分享