Infinispan JPA 二级缓存默认值
Infinispan JPA 2nd level cache defaults
我正在尝试将 Infinispan 配置为休眠二级缓存。一切都很好,但我想调整默认配置,即所有缓存共享的值。
为注释为 @Cache
的实体自动创建缓存,我可以在 infinispan.xml
by <distributed-cache-configuratoin>
中一一自定义它们。但是,我想为所有这些缓存设置默认值(例如驱逐策略)。
另一件事是,我想将所有这些生成的缓存标记为"distributed"(默认情况下它们是"local")。
这是我的 infinispan.xml
的摘录:
<cache-container default-cache="default" statistics="true">
<transport stack="external-file" />
<!-- Configuring specifics for the User entity. How to do it globally? -->
<distributed-cache-configuration name="user" statistics="true" />
</cache-container>
我该怎么做这些事情?
实体的 default cache configuration 名为 entity
:
Cache configuration can differ for each type of data stored in the
cache. In order to override the cache configuration template, use
property hibernate.cache.infinispan.data-type.cfg
where data-type
can be one of:
entity
Entities indexed by @Id
or @EmbeddedId
attribute.
immutable-entity
Entities tagged with @Immutable
annotation or set
as mutable=false
in mapping file.
naturalid
Entities indexed by their @NaturalId
attribute.
collection
All collections.
timestamps
Mapping entity type → last modification timestamp. Used
for query caching.
query
Mapping query → query result.
pending-puts
Auxiliary caches for regions using invalidation mode
caches.
collection
、immutable-entity
和 naturalid
的默认配置也是为 entity
指定的配置,因此您不必单独配置它们(如果您当然不想要单独的配置),如 documentation and the source code.
中所示
备注
一般来说,使Hibernate L2缓存分布式可能不是一个好主意,因为实体实例存储在L2缓存中的disassembled hydrated state中,这意味着只有关联实体的id与父实体状态。
假设您有以下实体(A
、B
、C
都是可缓存的):
@Entity
public class A {
@ManyToOne
private B b;
@OneToMany
private Collection<C> cs;
}
即使 cs
集合也是可缓存的,要从缓存中完全 assemble 实体 A
实例,您将有以下到其他节点的网络往返集群:
- 获取实体
A
状态。
- 根据存储在
b
关联中的 ID 获取实体 B
状态。
- 获取
cs
个 ID 的集合。
- 对于
cs
集合中的每个 ID,逐一获取 C
实体状态。
显然,如果您正在组装 A
个实例的集合(例如来自查询的结果),则针对 A
.[=48 的每个实例执行上述所有操作=]
这一切都意味着直接从数据库读取数据(通过正确配置的延迟加载,例如使用 ),可能比分布式缓存中的所有网络往返更有效。
此外,这也是为什么 entity/collection 缓存应该 运行 在失效集群模式下的原因之一(数据仅缓存在 reads/writes 它的节点上,但在更改时的其他节点)。
我正在尝试将 Infinispan 配置为休眠二级缓存。一切都很好,但我想调整默认配置,即所有缓存共享的值。
为注释为 @Cache
的实体自动创建缓存,我可以在 infinispan.xml
by <distributed-cache-configuratoin>
中一一自定义它们。但是,我想为所有这些缓存设置默认值(例如驱逐策略)。
另一件事是,我想将所有这些生成的缓存标记为"distributed"(默认情况下它们是"local")。
这是我的 infinispan.xml
的摘录:
<cache-container default-cache="default" statistics="true">
<transport stack="external-file" />
<!-- Configuring specifics for the User entity. How to do it globally? -->
<distributed-cache-configuration name="user" statistics="true" />
</cache-container>
我该怎么做这些事情?
实体的 default cache configuration 名为 entity
:
Cache configuration can differ for each type of data stored in the cache. In order to override the cache configuration template, use property
hibernate.cache.infinispan.data-type.cfg
wheredata-type
can be one of:
entity
Entities indexed by@Id
or@EmbeddedId
attribute.
immutable-entity
Entities tagged with@Immutable
annotation or set asmutable=false
in mapping file.
naturalid
Entities indexed by their@NaturalId
attribute.
collection
All collections.
timestamps
Mapping entity type → last modification timestamp. Used for query caching.
query
Mapping query → query result.
pending-puts
Auxiliary caches for regions using invalidation mode caches.
collection
、immutable-entity
和 naturalid
的默认配置也是为 entity
指定的配置,因此您不必单独配置它们(如果您当然不想要单独的配置),如 documentation and the source code.
备注
一般来说,使Hibernate L2缓存分布式可能不是一个好主意,因为实体实例存储在L2缓存中的disassembled hydrated state中,这意味着只有关联实体的id与父实体状态。
假设您有以下实体(A
、B
、C
都是可缓存的):
@Entity
public class A {
@ManyToOne
private B b;
@OneToMany
private Collection<C> cs;
}
即使 cs
集合也是可缓存的,要从缓存中完全 assemble 实体 A
实例,您将有以下到其他节点的网络往返集群:
- 获取实体
A
状态。 - 根据存储在
b
关联中的 ID 获取实体B
状态。 - 获取
cs
个 ID 的集合。 - 对于
cs
集合中的每个 ID,逐一获取C
实体状态。
显然,如果您正在组装 A
个实例的集合(例如来自查询的结果),则针对 A
.[=48 的每个实例执行上述所有操作=]
这一切都意味着直接从数据库读取数据(通过正确配置的延迟加载,例如使用
此外,这也是为什么 entity/collection 缓存应该 运行 在失效集群模式下的原因之一(数据仅缓存在 reads/writes 它的节点上,但在更改时的其他节点)。