Infinispan JPA二级缓存默认值 [英] Infinispan JPA 2nd level cache defaults

查看:138
本文介绍了Infinispan JPA二级缓存默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Infinispan配置为一个hibernate二级缓存。一切都很好,但我想调整默认配置,即所有缓存共享的值。



注释的实体自动创建缓存@Cache ,我可以通过< distributed-cache-configuratoin>来在 infinispan.xml 中逐个定制它们。 。但是,我想为所有这些缓存设置默认值(例如驱逐策略)。另一件事是,我想将所有这些生成的缓存标记为分布式(默认情况下它们是本地)。



以下是我的 infinispan.xml 中的一个exceprt:

 < cache-container default-cache =defaultstatistics =true> 
< transport stack =external-file/>
<! - 为用户实体配置细节。如何做到全球? - >
< distributed-cache-configuration name =userstatistics =true/>
< / cache-container>

我如何做这些事? >解决方案

默认缓存配置为实体名为实体


对于存储在
高速缓存中的每种数据,高速缓存配置可能会有所不同。为了覆盖缓存配置模板,使用
属性 hibernate.cache.infinispan.data-type.cfg 其中 data-type
可以是以下之一:



实体由<$ c索引的实体$ c> @Id 或 @EmbeddedId 属性。

immutable-entity 使用 @Immutable 注释标记的实体或将
设置为 mutable = false $ b naturalid 通过它们的 @NaturalId 索引的实体 code>属性。



集合所有集合。



timestamps 映射实体类型→上次修改时间戳记。使用
进行查询缓存。



查询映射查询→查询结果。



pending-puts 使用无效模式的区域的辅助高速缓存
高速缓存



$ b $ p 集合
不可变实体和<$ c $的默认值c> naturalid
也是为 entity 指定的配置,因此您不必单独配置它们(如果您不想单独配置当然),正如可以在文档源代码

注意



通常,分发Hibernate L2缓存可能不是一个好主意,因为实体实例存储在 disassembled hydrated state in the L2 cache,意思是只有关联实体的id与父实体状态一起存储。



假设您有以下实体( A B C 都是可以缓存的):

  @Entity 
public class A {
@ManyToOne
private B b;

@OneToMany
私人收藏< C> CS;

$ / code>

即使 cs 集合也是可缓存的,为了从缓存中完全组装一个实体 A 实例,您将有以下网络往返集群的其他节点:


  1. 获取实体 A 状态。

  2. B 根据存储在 b 关联中的ID的状态。

  3. cs ids。

  4. 对于 cs 集合中的每个id,一个一个获取 C 实体状态。

显然,如果你正在汇集 A 个实例的集合(例如,来自查询结果),所有上述操作都针对 A < code $。

这意味着直接从数据库中读取数据(使用批量大小<例如a>)可能比分布式缓存中的所有网络往返效率高得多。另外,这也是为什么实体/集合缓存应该在失效集群模式下运行的原因之一(数据仅缓存在读/写它的节点上,但是更改时在其他节点上无效)。


I'm trying to configure Infinispan as a hibernate 2nd level cache. Everything is fine, but I want to tweak the default configuration, i.e. the values that all caches shared.

Caches are automatically created for entities annotated with @Cache, and I can customize them one by one in infinispan.xml by <distributed-cache-configuratoin>. However, I'd like to have default values (e.g. eviction strategy) for all of these caches.

Another thing is, I want to mark all these generated caches as "distributed" (they are "local" by default).

Here's an exceprt from my 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>

How do I do these things?

解决方案

The default cache configuration for entities is named 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.

The default for collection, immutable-entity and naturalid is also the configuration specified for entity, so you don't have to configure them separately (if you don't want separate configurations of course), as can be seen in the documentation and the source code.

Note

In general, making a Hibernate L2 cache distributed may not be a good idea, because entity instances are stored in disassembled hydrated state in the L2 cache, meaning that only ids of the associated entities are stored together with the parent entity state.

Suppose you have the following entities (A, B, C are all cachable):

@Entity
public class A {
  @ManyToOne
  private B b;

  @OneToMany
  private Collection<C> cs;
}

Even if the cs collection were cachable also, to fully assemble an entity A instance from the cache you would have the following network round trips to the other nodes of the cluster:

  1. Fetch the entity A state.
  2. Fetch the entity B state based on id stored in the b association.
  3. Fetch the collection of cs ids.
  4. For each id in the cs collection, fetch the C entity state, one by one.

Obviously, if you are assembling of collection of A instances (from a result of a query for example), all of the above is executed for each instance of A.

This all implies that reading data from the database directly (with properly configured lazy loading, using batch size for example), could be much more efficient than all of the network round trips in a distributed cache.

Also, that's one of the reasons why the entity/collection cache should run in the invalidation cluster mode (data are cached only on the node that reads/writes it, but invalidated on other nodes when changed).

这篇关于Infinispan JPA二级缓存默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆