24.3.2. 缓存容器
缓存容器是子系统使用的缓存存储库。每个缓存容器定义要使用的默认缓存。
JBoss EAP 7 定义以下默认 Infinispan 缓存容器:
-
用于单例缓存
的服务器
-
Web
会话集群 -
用于
有
状态会话 Bean 集群的 EJB -
用于实体
缓存的 Hibernate
示例:默认 Infinispan 配置
<subsystem xmlns="urn:jboss:domain:infinispan:7.0"> <cache-container name="server" aliases="singleton cluster" default-cache="default" module="org.wildfly.clustering.server"> <transport lock-timeout="60000"/> <replicated-cache name="default"> <transaction mode="BATCH"/> </replicated-cache> </cache-container> <cache-container name="web" default-cache="dist" module="org.wildfly.clustering.web.infinispan"> <transport lock-timeout="60000"/> <distributed-cache name="dist"> <locking isolation="REPEATABLE_READ"/> <transaction mode="BATCH"/> <file-store/> </distributed-cache> </cache-container> <cache-container name="ejb" aliases="sfsb" default-cache="dist" module="org.wildfly.clustering.ejb.infinispan"> <transport lock-timeout="60000"/> <distributed-cache name="dist"> <locking isolation="REPEATABLE_READ"/> <transaction mode="BATCH"/> <file-store/> </distributed-cache> </cache-container> <cache-container name="hibernate" default-cache="local-query" module="org.hibernate.infinispan"> <transport lock-timeout="60000"/> <local-cache name="local-query"> <object-memory size="1000"/> <expiration max-idle="100000"/> </local-cache> <invalidation-cache name="entity"> <transaction mode="NON_XA"/> <object-memory size="1000"/> <expiration max-idle="100000"/> </invalidation-cache> <replicated-cache name="timestamps" mode="ASYNC"/> </cache-container> </subsystem>
注意每个缓存容器中定义的默认缓存。例如,Web
缓存容器将 dist
分布式缓存定义为默认值。因此,集群 Web 会话时将使用 dist
缓存。
如需有关更改默认缓存并添加额外缓存的信息,请参阅配置缓存容器。
您可以添加额外的缓存和缓存容器,例如用于 HTTP 会话、有状态会话 Bean 或单例服务或部署。不支持直接由用户应用使用这些缓存。
24.3.2.1. 配置缓存容器
可以使用管理控制台或管理 CLI 配置缓存容器和缓存属性。
您应该避免更改缓存或缓存容器名称,因为配置中的其他组件可能会引用它们。
使用管理控制台配置缓存
从管理控制台中的 Configuration 选项卡导航到 Infinispan 子系统后,您可以配置缓存和缓存容器。在受管域中,确保选择要配置的适当配置文件。
添加缓存容器。
单击 Cache Container 标题旁边的 Add(+)按钮,选择 Add Cache Container,然后输入新缓存容器的设置。
更新缓存容器设置。
选择适当的缓存容器,再单击 View。根据需要配置缓存容器设置。
更新缓存容器传输设置。
选择适当的缓存容器,再单击 View。选择传输选项卡,并根据需要配置缓存容器传输设置。
配置缓存.
选择适当的缓存容器,再单击 View。例如,从相应的缓存选项卡中,您可以添加、更新和删除缓存。
使用管理 CLI 配置缓存
您可以使用管理 CLI 配置缓存和缓存容器。在受管域中,您必须通过 /profile=PROFILE_NAME
在前面指定要更新的配置集。
添加缓存容器。
/subsystem=infinispan/cache-container=CACHE_CONTAINER:add
添加复制的缓存.
/subsystem=infinispan/cache-container=CACHE_CONTAINER/replicated-cache=CACHE:add(mode=MODE)
设置缓存容器的默认缓存。
/subsystem=infinispan/cache-container=CACHE_CONTAINER:write-attribute(name=default-cache,value=CACHE)
为复制的缓存配置批处理。
/subsystem=infinispan/cache-container=CACHE_CONTAINER/replicated-cache=CACHE/component=transaction:write-attribute(name=mode,value=BATCH)
以下示例演示了如何将 并发
分布式缓存添加到 Web
缓存容器中。此缓存配置可以减轻默认缓存的锁定限制,允许多个并发请求同时访问同一 Web 会话。它允许无锁定读和获得专用锁定的频率更高,但时间较长。
使用以下管理 CLI 命令,将 并发
分布式缓存添加到 web
缓存容器中,并使其成为默认缓存:
batch /subsystem=infinispan/cache-container=web/distributed-cache=concurrent:add /subsystem=infinispan/cache-container=web:write-attribute(name=default-cache,value=concurrent) /subsystem=infinispan/cache-container=web/distributed-cache=concurrent/store=file:add run-batch
这会导致以下服务器配置:
<cache-container name="web" default-cache="concurrent" module="org.wildfly.clustering.web.infinispan"> ... <distributed-cache name="concurrent"> <file-store/> </distributed-cache> </cache-container>
更改默认 EJB 缓存容器
您可以在 ejb3
子系统中使用缓存容器,如下所述:
-
若要支持 EJB 会话 Bean 的传递,您可以使用
infinispan
子系统中定义的ejb
缓存容器来存储会话。 - 对于连接到服务器上集群部署的远程 EJB 客户端,您必须向这些客户端提供集群拓扑信息,以便在它们与节点交互失败时能够切换到集群中的其他节点。
如果要更改或重命名名为 ejb
的默认缓存容器(支持传递和调配拓扑信息),您必须将 cache-container
属性添加到 passivation-stores
元素中,并将 cluster
属性添加到 远程
元素中,如下例所示:如果您只是添加新的缓存供您自己的使用,则不需要进行这些更改。
<subsystem xmlns="urn:jboss:domain:ejb3:5.0"> <passivation-stores> <passivation-store name="infinispan" cache-container="ejb-cltest" max-size="10000"/> </passivation-stores> <remote cluster="ejb-cltest" connector-ref="http-remoting-connector" thread-pool-name="default"/> </subsystem> <subsystem xmlns="urn:jboss:domain:infinispan:7.0"> ... <cache-container name="ejb-cltest" aliases="sfsb" default-cache="dist" module="org.wildfly.clustering.ejb.infinispan"> </subsystem>
Hibernate 缓存容器中的驱除功能
hibernate
缓存容器的驱除功能会从内存中移除缓存条目。此功能有助于减少子系统的内存负载。
size
属性设置在缓存条目驱除开始前存储的最大缓存条目数。
示例:驱除功能
<cache-container name="hibernate" default-cache="local-query" module="org.hibernate.infinispan"> <transport lock-timeout="60000"/> <local-cache name="local-query"> <object-memory size="1000"/> <expiration max-idle="100000"/>
请注意,驱除只在内存中发生。缓存存储包含被驱除的缓存条目,以防止永久丢失信息。有关驱除功能的更多信息,请参阅 Infinispan 用户指南中的驱除和数据容器部分。