Coherence Step by Step 第三篇 缓存(二) 配置(翻译)
本章节详细的介绍如何用cache configuration deployment descripotr配置缓存。
coherence-config-base.xsd文件。这个文件位于coherence.jar文件的根目录。cache configuration deployment descriptor包含了个主要的元素,在本章中详细讲述:
<caching-scheme-mapping>元素和<caching-schemes>元素。这些元素被用来定义缓存方案和定义映射到缓存方案上的缓存名。<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>example</cache-name> <scheme-name>distributed</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <distributed-scheme> <scheme-name>distributed</scheme-name> </distributed-scheme> </caching-schemes> </cache-config>
<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>*</cache-name> <scheme-name>distributed</scheme-name> </cache-mapping> <cache-mapping> <cache-name>account-*</cache-name> <scheme-name>account-distributed</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <distributed-scheme> <scheme-name>distributed</scheme-name> </distributed-scheme> <distributed-scheme> <scheme-name>account-distributed</scheme-name> </distributed-scheme> </caching-schemes> </cache-config>
第一个映射,应用程序能用任何名字,当创建一个换名,名字映射到方案名为distributed的缓存方案定义。
第二个映射需要应用程序在指定缓存名时使用一个pattern。这个例子中,程序必须使用名字前缀为account-的。例如,应用程序指定account-overdue作为缓存名,这个缓存名使用方案名为account-distributed的缓存方案定义。
2.3 指定映射中的初始参数
Cache应谁支持使用初始化参数来覆盖基础缓存方案定义的属性。初始参数常用来促进缓存方案定义的重用。在这种情况下,多个缓存名映射到相同的缓存方案定义,但是每个映射覆更具需要覆盖了缓存属性。
初始参数定义了在<init-params>节点中使用一个<init-param>元素。<init-param>元素必须包含<param-name>元和和<param-value>元素。可以指定任意数量的参数。
下面的例子床架了两个cache mapping,映射到了同一个缓存方案定义。然而,第一个映射覆盖了在基础缓存方案定义的back-size-limit属性;同时,第二个映射使用back-size-limit作为配置的方式在基础缓存方案定义。
<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>*</cache-name> <scheme-name>distributed</scheme-name> <init-params> <init-param> <param-name>back-size-limit</param-name> <param-value>8MB</param-value> </init-param> </init-params> </cache-mapping> <cache-mapping> <cache-name>account-*</cache-name> <scheme-name>distributed</scheme-name> </cache-mapping> </caching-scheme-mapping> ... </cache-config>
3.定义缓存方案
缓存方案用来定义一个应用程序可用的缓存定义。缓存方案提供了一个说明性的机制,允许要使用它们的应用程序独立定义缓存。这个奖顶一一缓存的职责从应用程序中移除,并允许在不改变应用程序代码的情况加改变缓存。缓存方案通过允许许多应用使用同一个缓存定义,提高了缓存定义的重用。
缓存方案定义在<caching-schemes>元素里。每个缓存类型(distributed,replicated等)有一个对应的方案元素和属性用来定义缓存的类型。缓存方案也能被嵌套,允许更多自定义的和合成的缓存,例如near cache.
这章描述了如何为最常用的缓存类型定义缓存方案,并不表示coherence所提供的所有的缓存类型。
<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>example</cache-name> <scheme-name>distributed</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <distributed-scheme> <scheme-name>distributed</scheme-name> <backing-map-scheme> <local-scheme/> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme> </caching-schemes> </cache-config>
<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>example</cache-name> <scheme-name>replicated</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <replicated-scheme> <scheme-name>replicated</scheme-name> <backing-map-scheme> <local-scheme/> </backing-map-scheme> <autostart>true</autostart> </replicated-scheme> </caching-schemes> </cache-config>
<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>example</cache-name> <scheme-name>optimistic</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <optimistic-scheme> <scheme-name>optimistic</scheme-name> <backing-map-scheme> <local-scheme/> </backing-map-scheme> <autostart>true</autostart> </optimistic-scheme> </caching-schemes> </cache-config>
<caching-schemes>
, <distributed-scheme>
, <replicated-scheme>
, <optimistic-scheme>
, <near-scheme>
, <overflow-scheme>
, <read-write-backing-map-scheme>
, 和<backing-map-scheme>。
下面的例子定义了local cache 使用local 作为方案名,映射到名为example的缓存。
<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>example</cache-name> <scheme-name>local</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <local-scheme> <scheme-name>local</scheme-name> <eviction-policy>LRU</eviction-policy> <high-units>32000</high-units> <low-units>10</low-units> <unit-calculator>FIXED</unit-calculator> <expiry-delay>10ms</expiry-delay> </local-scheme> </caching-schemes> </cache-config>
<unit-calculator>
)的计算。<expiry-delay>
也许到了,但是没有被初始化直到有一个客户端进程访问了缓存。例如,如果 <expiry-delay>
值被设置为10秒,客户端在15秒后访问了缓存,那么过期发生在15秒后。<near-scheme>
元素用来定义near cache。near cache 是一个混合的cahce,因为它包含两个cache:<front-scheme>元素用来定义local(front-tier) calche,<back-scheme>元素哦你过来定义(back-tier) cache.通常,local cache用来作为front-tier,然而,front-tier也能够使用基于java objects(使用<class-scheme>)和non-jvm heap-based caches (使用<external-scheme>
o或者<paged-external-scheme>
)的方案。<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>example</cache-name> <scheme-name>near</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <near-scheme> <scheme-name>near</scheme-name> <front-scheme> <local-scheme/> </front-scheme> <back-scheme> <distributed-scheme> <scheme-name>near-distributed</scheme-name> <backing-map-scheme> <local-scheme/> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme> </back-scheme> </near-scheme> </caching-schemes> </cache-config>
- none 这个策略通知缓存不去监听失效时间。这是对原生性能和可扩展性最好的选择,当也许需求允许使用不是最新的数据。为front cache使用足够简短的逐出策略能够保存数据的最新。
- present 这个策略通知near cache监听back cache事件,只涉及到front cache中的当前的条目。当每个front cache的实例包含了明显的数据子集,相对于其他的front cache 实例,这个策略工作的很好。
- ALL 这个策略通知near cache 监听所有的back cache事件。这个策略适合大量读取的分层访问模式,在不同的front cahce中有明显的重叠。
- auto 这个策略通知near cache在present和all策略之间基于缓存统计数据自动切换。
<distributed-scheme> <scheme-name>DistributedInMemoryCache</scheme-name> <service-name>DistributedCache</service-name> <backing-map-scheme> <local-scheme> <eviction-policy>LRU</eviction-policy> <high-units>1000</high-units> <expiry-delay>1h</expiry-delay> </local-scheme> </backing-map-scheme> </distributed-scheme> <distributed-scheme> <scheme-name>DistributedInMemoryCache</scheme-name> <service-name>DistributedCache</service-name> <backing-map-scheme> <local-scheme> <scheme-ref>LocalSizeLimited</scheme-ref> </local-scheme> </backing-map-scheme> </distributed-scheme> <local-scheme> <scheme-name>LocalSizeLimited</scheme-name> <eviction-policy>LRU</eviction-policy> <high-units>1000</high-units> <expiry-delay>1h</expiry-delay> </local-scheme>
LocalSizeLimited
方案的能力。LocalSizeLimited
基础定义,并且覆盖了expiry-delay属性<distributed-scheme> <scheme-name> DistributedInMemoryCache </scheme-name> <service-name> DistributedCache </service-name> <backing-map-scheme> <local-scheme> <scheme-ref>LocalSizeLimited</scheme-ref> </local-scheme> </backing-map-scheme> </distributed-scheme> <replicated-scheme> <scheme-name> ReplicatedInMemoryCache </scheme-name> <service-name> ReplicatedCache </service-name> <backing-map-scheme> <local-scheme> <scheme-ref>LocalSizeLimited</scheme-ref> <expiry-delay>10m</expiry-delay> </local-scheme> </backing-map-scheme> </replicated-scheme> <local-scheme> <scheme-name> LocalSizeLimited </scheme-name> <eviction-policy>LRU</eviction-policy> <high-units> 1000 </high-units> <expiry-delay>1h</expiry-delay> </local-scheme>
<?xml version="1.0"?> <cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config" xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd"> <caching-scheme-mapping> <cache-mapping> <cache-name>example</cache-name> <scheme-name>DistributedInMemoryCache</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <distributed-scheme> <scheme-name>DistributedInMemoryCache</scheme-name> <service-name>DistributedCache</service-name> <thread-count>4</thread-count> <request-timeout>60s</request-timeout> <backing-map-scheme> <local-scheme> <scheme-ref>LocalSizeLimited</scheme-ref> </local-scheme> </backing-map-scheme> </distributed-scheme> <local-scheme> <scheme-name>LocalSizeLimited</scheme-name> <eviction-policy>LRU</eviction-policy> <high-units>1000</high-units> <expiry-delay>1h</expiry-delay> </local-scheme> </caching-schemes> </cache-config>
<param-type> | <param-value> | Description |
---|---|---|
|
|
作为构造函数的参数来传递缓存名, For example: <class-name>com.mycompany.cache.CustomCacheLoader </class-name> <init-params> <init-param> <param-type>java.lang.String</param-type> <param-value>{cache-name}</param-value> </init-param> </init-params> |
|
|
作为构造函数的参数来传递当前的classloader. For example: <class-name>com.mycompany.cache.CustomCacheLoader </class-name> <init-params> <init-param> <param-type>java.lang.ClassLoader</param-type> <param-value>{class-loader}</param-value> </init-param> </init-params> |
|
|
作为构造参数来传递当前的BackingMapManagerContext. For example:
<class-name>com.mycompany.cache.CustomCacheLoader </class-name> <init-params> <init-param> <param-type> com.tangosol.net.BackingMapManagerContext </param-type> <param-value>{manager-context}</param-value> </init-param> </init-params> |
|
|
通过带有指定<scheme-name>值的 <class-scheme> <scheme-name>dbconnection</scheme-name> <class-name>com.mycompany.dbConnection</class-name> <init-params> <init-param> <param-name>driver</param-name> <param-type>String</param-type> <param-value>org.gjt.mm.mysql.Driver </param-value> </init-param> <init-param> <param-name>url</param-name> <param-type>String</param-type> <param-value> jdbc:mysql://dbserver:3306/companydb </param-value> </init-param> <init-param> <param-name>user</param-name> <param-type>String</param-type> <param-value>default</param-value> </init-param> <init-param> <param-name>password</param-name> <param-type>String</param-type> <param-value>default</param-value> </init-param> </init-params> </class-scheme> ... <class-name>com.mycompany.cache.CustomCacheLoader </class-name> <init-params> <init-param> <param-type>{scheme-ref}</param-type> <param-value>dbconnection</param-value> </init-param> </init-params> |
|
cache name |
用来获取指定缓存名的NamedCache的引用。考虑下面的配置 example: <cache-config> <caching-scheme-mapping> <cache-mapping> <cache-name>boston-*</cache-name> <scheme-name>wrapper</scheme-name> <init-params> <init-param> <param-name>delegate-cache-name</param-name> <param-value>london-*</param-value> </init-param> </init-params> </cache-mapping> <cache-mapping> <cache-name>london-*</cache-name> <scheme-name>partitioned</scheme-name> </cache-mapping> </caching-scheme-mapping> <caching-schemes> <class-scheme> <scheme-name>wrapper</scheme-name> <class-name> com.tangosol.net.cache.WrapperNamedCache </class-name> <init-params> <init-param> <param-type>{cache-ref}</param-type> <param-value>{delegate-cache-name} </param-value> </init-param> <init-param> <param-type>string</param-type> <param-value>{cache-name}</param-value> </init-param> </init-params> </class-scheme> <distributed-scheme> <scheme-name>partitioned</scheme-name> <service-name>partitioned</service-name> <backing-map-scheme> <local-scheme> <unit-calculator>BINARY</unit-calculator> </local-scheme> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme> </caching-schemes> </cache-config> The |