第 4 章 配置数据网格缓存
借助 Data Grid,您可以以声明性和编程方式定义用于缓存的属性和选项。
声明性配置使用遵循 Data Grid 模式的 XML 文件。另一方面,编程配置使用 Data Grid API。
在大多数情况下,您可以使用声明性配置作为缓存定义的起点。在运行时,您可以以编程方式配置缓存来调优设置或指定附加属性。但是,Data Grid 提供灵活性,以便您可以选择声明性、编程或两者的组合。
4.1. 声明性配置 复制链接链接已复制到粘贴板!
声明性配置符合模式,并在 XML 或 JSON 格式的文件中定义。
以下示例显示了 Data Grid 配置的基本结构:
<infinispan>
<!-- Defines properties for all caches within the container and optionally names a default cache. -->
<cache-container default-cache="local">
<!-- Configures transport properties for clustered cache modes. -->
<!-- Specifies the default JGroups UDP stack and names the cluster. -->
<transport stack="udp" cluster="mycluster"/>
<!-- Configures a local cache. -->
<local-cache name="local"/>
<!-- Configures an invalidation cache. -->
<invalidation-cache name="invalidation"/>
<!-- Configures a replicated cache. -->
<replicated-cache name="replicated"/>
<!-- Configures a distributed cache. -->
<distributed-cache name="distributed"/>
</cache-container>
</infinispan>
4.1.1. 缓存模板 复制链接链接已复制到粘贴板!
借助 Data Grid,您可以定义可用于创建缓存配置的模板。
例如,以下配置包含一个缓存模板:
<infinispan>
<!-- Specifies the cache named "local" as the default. -->
<cache-container default-cache="local">
<!-- Adds a cache template for local caches. -->
<local-cache-configuration name="local-template">
<expiration interval="10000" lifespan="10" max-idle="10"/>
</local-cache-configuration>
</cache-container>
</infinispan>
配置模板的继承
配置模板也可以从其他模板继承,以扩展和覆盖设置。
缓存模板继承是分级。要使子配置模板从父级继承,您必须在父模板后面包含它。
以下是模板继承示例:
<infinispan>
<cache-container>
<!-- Defines a cache template named "base-template". -->
<local-cache-configuration name="base-template">
<expiration interval="10000" lifespan="10" max-idle="10"/>
</local-cache-configuration>
<!-- Defines a cache template named "extended-template" that inherits settings from "base-template". -->
<local-cache-configuration name="extended-template"
configuration="base-template">
<expiration lifespan="20"/>
<memory max-size="2GB" />
</local-cache-configuration>
</cache-container>
</infinispan>
对于具有多个值的元素(如 属性 )而言,配置模板继承是额外的。从父配置生成子配置合并值。
例如,& lt;property value_x="foo" /> 在父配置中与 < property value_y="bar" /> 合并,以便在子配置中生成 < ;property value_x="foo" value_y="bar" />。
4.1.2. 缓存配置通配符 复制链接链接已复制到粘贴板!
您可以使用通配符将缓存定义与配置模板匹配。
<infinispan>
<cache-container>
<!-- Uses the `*` wildcard to match any cache names that start with "basecache". -->
<local-cache-configuration name="basecache*">
<expiration interval="10500" lifespan="11" max-idle="11"/>
</local-cache-configuration>
<!-- Adds local caches that use the "basecache*" configuration. -->
<local-cache name="basecache-1"/>
<local-cache name="basecache-2"/>
</cache-container>
</infinispan>
如果缓存名称与多个通配符匹配,则数据网格会抛出异常。
4.1.3. 多个配置文件 复制链接链接已复制到粘贴板!
Data Grid 支持 XML 包含(XInclude),允许您在多个文件中分割配置。
<infinispan xmlns:xi="http://www.w3.org/2001/XInclude">
<cache-container default-cache="cache-1">
<!-- Includes a local.xml file that contains a cache configuration. -->
<xi:include href="local.xml" />
</cache-container>
</infinispan>
如果要将模式用于包含的片段,请使用 infinispan-config-fragment-12.1.xsd 模式:
<local-cache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:12.1 https://infinispan.org/schemas/infinispan-config-fragment-12.1.xsd"
xmlns="urn:infinispan:config:12.1"
name="mycache"/>
Data Grid 配置只提供对 XInclude 规格的最小支持。例如,您无法使用 xpointer 属性,xi:fallback 元素、文本处理或内容协商。
参考