coherence mysql_Coherence Step by Step 第三篇 缓存(二) 配置(翻译)

本章节详细的介绍如何用cache configuration deployment descripotr配置缓存。

1.概述

缓存是被配置在cache configuration deployment descriptor。默认的,Coherence 尝试加载在classpath中找到的第一个coherence-cache-config.xml deployment descriptor。Coherence包含一个样本coherence-cache-config.xml文件在coherence.jar中。要使用一个不同的coherence-cache-config.xml 文件,文件必须在classpath中,并且必须在在coherence.jar加载前加载;否则,会使用样本的缓存配置deployment descriptor。

cache configuration descriptor允许缓存在程序代码中单独定义。在运行时,承运使用在descriptor中定义的缓存的名字来引用一个缓存的实例。这允许引用程序编写独立的缓存定义。更具这个方法,可以不修改程序代码而修改缓存的定义。这个方法也最大化的重新缓存定义。

cache configuration descriptor的架构定义是coherence-cache-config.xsd文件,它导入了coherence-cache-config-base.xsd文件,它,返回来,隐式导入coherence-config-base.xsd文件。这个文件位于coherence.jar文件的根目录。cache configuration deployment descriptor包含了个主要的元素,在本章中详细讲述:元素和元素。这些元素被用来定义缓存方案和定义映射到缓存方案上的缓存名。

2.定义Cache Mapping

Cache mapping映射一个缓存名字到一个缓存方案定义。这个映射提供了应用程序和基础缓存定义的层次上的风力。分离后允许缓存实现根据需要来改变,而不用改变程序代码。Cache mapping也能用来设置初始化参数,应用于底层的缓存方案定义。

Cache mapping定义在节点中,用元素。可以创建任意数量的cache mapping。cache mapping必须包含缓存名和被缓存名所映射的方案名。

2.1 使用 One-to-One Cache Mappings

One-to-One cache mapping映射一个指定的缓存名到一个缓存方案定义。应用程序必须提供准确的在映射中指定的名字来使用缓存。 下面是创建单个cache mapping,映射缓存名字example到名为distributed的distributed cache方案定义。

coherence-cache-config.xsd">

example

distributed

distributed

2.2使用Cache Name Pattern Mappings

Cache name pattern mappings 允许应用程序在指定一个缓存名的时候使用pattern。Pattern使用星号作为掩码。Cache name patterns缓和了应用程序不得不知道准确的缓存名。下面的例子创建了两个cache mappings.第一个映射使用了星号来映射任何缓存名到一个名为distributed的distributed cache 方案定义。第二个映射映射了cache name pattern 为account-*的映射到方案名为account-distributed的缓存方案定义。

coherence-cache-config.xsd">

*

distributed

account-*

account-distributed

distributed

account-distributed

第一个映射,应用程序能用任何名字,当创建一个换名,名字映射到方案名为distributed的缓存方案定义。

第二个映射需要应用程序在指定缓存名时使用一个pattern。这个例子中,程序必须使用名字前缀为account-的。例如,应用程序指定account-overdue作为缓存名,这个缓存名使用方案名为account-distributed的缓存方案定义。

2.3 指定映射中的初始参数

Cache应谁支持使用初始化参数来覆盖基础缓存方案定义的属性。初始参数常用来促进缓存方案定义的重用。在这种情况下,多个缓存名映射到相同的缓存方案定义,但是每个映射覆更具需要覆盖了缓存属性。

初始参数定义了在节点中使用一个元素。元素必须包含元和和元素。可以指定任意数量的参数。

下面的例子床架了两个cache mapping,映射到了同一个缓存方案定义。然而,第一个映射覆盖了在基础缓存方案定义的back-size-limit属性;同时,第二个映射使用back-size-limit作为配置的方式在基础缓存方案定义。

coherence-cache-config.xsd">

*

distributed

back-size-limit

8MB

account-*

distributed

...

3.定义缓存方案

缓存方案用来定义一个应用程序可用的缓存定义。缓存方案提供了一个说明性的机制,允许要使用它们的应用程序独立定义缓存。这个奖顶一一缓存的职责从应用程序中移除,并允许在不改变应用程序代码的情况加改变缓存。缓存方案通过允许许多应用使用同一个缓存定义,提高了缓存定义的重用。

缓存方案定义在元素里。每个缓存类型(distributed,replicated等)有一个对应的方案元素和属性用来定义缓存的类型。缓存方案也能被嵌套,允许更多自定义的和合成的缓存,例如near cache.

这章描述了如何为最常用的缓存类型定义缓存方案,并不表示coherence所提供的所有的缓存类型。

3.1 定义distributed cache 方案

元素用来定义distributed cache。distributed cache使用了distributed cache的服务实例。在cache configuration中可以定义任意数量的distributed cache。

下面的例子定义了基本的distributed cache ,用distributed作为方案名,映射到名为example的缓存。元素设置为true,用来在启动cache server节点时候启动服务。

coherence-cache-config.xsd">

example

distributed

distributed

true

这个例子中,distributed缓存定义了local cache 用来作为backing map。

3.2 定义Replicated Cache方案

元素用来定义replicated caches。replicated cache 使用了replicated cache服务实例。在cache configruation file中可以定义任意数量的replicated cache。

下面的例子定义了一个基本的replicated cache,使用replicated作为方案名,映射到名为example的缓存。元素设置为true,用来在启动cache server节点时候启动服务。

coherence-cache-config.xsd">

example

replicated

replicated

true

这个例子中,replicated cache 定义了local cache 用来作为backing map。

3.3 定义Optimistic Cache 方案

元素用来定义optimistic caches。optimistic cache使用optimistic cache服务实例。在cache configruation file中可以定义任意数量的optimistic cache。

下面的例子定义了基本的optimistic cache,使用optimistic作为方案名,映射到名为example的缓存。元素设置为true,用来在启动cache server时启动服务。

coherence-cache-config.xsd">

example

optimistic

optimistic

true

这个例子中,optimistic cache 定义了local cache 用来作为backing map。

3.4 定义Local Cache 方案

元素用来定义local caches。local cache通常是被嵌套在其它缓存方案中的,例如near cache的前面的一层。因此,这个元素能够出现作为下面元素的的子元素:,,,,,,, 和。

下面的例子定义了local cache 使用local 作为方案名,映射到名为example的缓存。

NOTE:local cache在cache server上通常不用来作为单独的缓存;另外,如果配置文件只定义local cache,那么cache server不会启动。

coherence-cache-config.xsd">

example

local

local

LRU

32000

10

FIXED

10ms

3.4.1 控制local cache 的增长

提供了几个可选的子元素来控制缓存的增长。例如,和子元素限制了缓存的规模。当缓存达到了允许大小的最大值是,它会自己裁剪,回到指定的小尺寸,更具指定的eviction-policy来选择那些条目被逐出。条目和尺寸的限制被测量,依据方案的unit-calculator()的计算。

local cache 使用缓存配置元素来配置条目在他们过期之前仍然在缓存中的时间量。当访问缓存时,客户端进程初始化这些动作。因此,也许到了,但是没有被初始化直到有一个客户端进程访问了缓存。例如,如果值被设置为10秒,客户端在15秒后访问了缓存,那么过期发生在15秒后。

NOTE:

客户端现成执行逐出,不是一个后台现成。另外,过期延时参数(cExpiryMillis)被定义为一个整数,是以毫秒的形式表示。因此,时间的最大值永远不会超过Integer.MAX_VALUE(2147483647)毫秒或大约24填。

3.5 定义Near Cache 方案

元素用来定义near cache。near cache 是一个混合的cahce,因为它包含两个cache:元素用来定义local(front-tier) calche,元素哦你过来定义(back-tier) cache.通常,local cache用来作为front-tier,然而,front-tier也能够使用基于java objects(使用)和non-jvm heap-based caches(使用o或者)的方案。

下面的例子定义了near cache,使用near作为方案名,映射到名为example的缓存。front-tier是local cache,back-tier是distributed cache

NOTE: Near caches为客户端使用而通常不是给cache server使用;此外,如果定义在cache configuration file中的缓存定义是near cache,那么cache 服务器是不会启动的。

coherence-cache-config.xsd">

example

near

near

near-distributed

true

3.5.1 Near Cache无效策略

对于near cache是一个可选的元素。无效策略是用来指定front-tier和back-tier是如何保持同步的。near cache可以配置来监听back cache中的确定时间,自动更新 或者使front-tier中的条目失效。根据back cache实现的接口,near cache提供了四个不同的策略来使在back cache中被其它进程改变过的front cache条目失效。

下面的表描述了失效策略

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策略之间基于缓存统计数据自动切换。

4. 使用方案继承

方案继承允许缓存方案通过继承其他方案来创建,并且根据需要来有选择的覆盖继承方案的属性。这种弹性使得缓存方案容易被维护,提高缓存方案的重用。元素被使用在缓存方案定义和指定一个从哪里继承的缓存名。

下面的两个distributed cache 方案是相同的,第一个明确的配置了local scheme 用来作为backing map。

第二个定义使用元素来继承一个local scheme ,命名为LocalSizeLimited:

DistributedInMemoryCache

DistributedCache

LRU

1000

1h

DistributedInMemoryCache

DistributedCache

LocalSizeLimited

LocalSizeLimited

LRU

1000

1h

上面的例子中,第一个distributed 方案定义更加紧凑,然后,第二个定义提供了更加容易的在多个方案中重用LocalSizeLimited方案的能力。

下面的例子演示了多个方案重用同一个LocalSizeLimited基础定义,并且覆盖了expiry-delay属性

DistributedInMemoryCache

DistributedCache

LocalSizeLimited

ReplicatedInMemoryCache

ReplicatedCache

LocalSizeLimited

10m

LocalSizeLimited

LRU

1000

1h

5. 使用缓存方案属性

缓存方案属性为部分应用程序根据需求来修改缓存行为。每个方案类型包含了它自己的一组对于缓存是有效的属性。缓存属性使用它们各自的元素定义在缓存方案定义中。

许多缓存属性使用默认值,除非在缓存方案定义中有给出明确的不同的值。clustered caches (distributed, replicated and optimistic) 使用通过它们各自的缓存服务定义所指定的默认值。缓存服务定义在operational deployment descriptor。虽然它可能通过使用operational override file改变属性值,但是缓存属性最常通过缓存方案定义来设置。

下面的例子创建了一个基本的distributed cacha 方案,设置了进程数量属性和请求超时属性。此外,local scheme用来作为backing map,设置属性来限制local cache的大小限制。

coherence-cache-config.xsd">

example

DistributedInMemoryCache

DistributedInMemoryCache

DistributedCache

4

60s

LocalSizeLimited

LocalSizeLimited

LRU

1000

1h

6.使用参数宏命令

cache configuration deployment descriptor支持在配置类构造函数的参数时,参数宏命令来最小化自定义编码和启用规范的常用属性。宏命令应该被包含在花括号内,如下所示,没有任何引用或空格

Description

java.lang.String

{cache-name}

作为构造函数的参数来传递缓存名, For example:

com.mycompany.cache.CustomCacheLoader

java.lang.String

{cache-name}

java.lang.ClassLoader

{class-loader}

作为构造函数的参数来传递当前的classloader. For example:

com.mycompany.cache.CustomCacheLoader

java.lang.ClassLoader

{class-loader}

com.tangosol.net.BackingMapManagerContext

{manager-context}

作为构造参数来传递当前的BackingMapManagerContext. For example:

com.mycompany.cache.CustomCacheLoader

com.tangosol.net.BackingMapManagerContext

{manager-context}

{scheme-ref}

local-scheme

通过带有指定值的 ,  or 实例化一个对象定义,用它作为构造参数. For example:

dbconnection

com.mycompany.dbConnection

driver

String

org.gjt.mm.mysql.Driver

url

String

jdbc:mysql://dbserver:3306/companydb

user

String

default

password

String

default

...

com.mycompany.cache.CustomCacheLoader

{scheme-ref}

dbconnection

{cache-ref}

cache name

用来获取指定缓存名的NamedCache的引用。考虑下面的配置 example:

boston-*

wrapper

delegate-cache-name

london-*

london-*

partitioned

wrapper

com.tangosol.net.cache.WrapperNamedCache

{cache-ref}

{delegate-cache-name}

string

{cache-name}

partitioned

partitioned

BINARY

true

The CacheFactory.getCache("london-test")调用的结果将在标准的partitioned cache 引用。相反的,theCacheFactory.getCache("boston-test") 调用将会分解delegate-cache-name参数的值到 london-test 并且将会构造一个WrapperNamedCache的实例,委托给NamedCache 通过CacheFactory.getCache("london-test") 调用返回。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值