Default Layer 2 Infinispan JPA Cache Settings

I am trying to configure Infinispan as a second level sleeping cache. Everything is fine, but I want to configure the default configuration, that is, the values ​​that all cache together.

Caches are automatically created for objects annotated using @Cache , and I can configure them one at a time in infinispan.xml on <distributed-cache-configuratoin> . However, I would like to have default values ​​(e.g. eviction strategy) for all of these caches.

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

Here's the 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 can I do it?

+5
source share
1 answer

the default cache setting for objects is called entity :

The cache configuration may vary for each type of data stored in the cache. To override the cache configuration template, use the hibernate.cache.infinispan.data-type.cfg , where data-type can be one of:

entity Objects indexed by the @Id or @EmbeddedId .

immutable-entity Objects marked with the @Immutable annotation or set to mutable=false in the mapping file.

naturalid Objects indexed by their @NaturalId attribute.

collection All collections.

timestamps Type of mapping object β†’ timestamp of the last modification. Used to cache requests.

query Match a query -> query result.

pending-puts Helper caches for regions that use cache invalid mode.

By default, collection , immutable-entity and naturalid also configured for entity , so you do not need to configure them separately (unless you want, of course, separate configurations), as you can see in the documentation and the source code .

Note

In general, creating a distributed Hibernate L2 cache may not be a good idea, since object instances are stored in a disassembled hydrated state in L2, which means that only the identifiers of related objects are stored together with the state of the parent object.

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

 @Entity public class A { @ManyToOne private B b; @OneToMany private Collection<C> cs; } 

Even if the cs collection was also cachable to completely collect the entity A instance from the cache, you would have the following network crawls to other cluster nodes:

  • Get Object A
  • Get object B based on the identifier stored in association B
  • Get a collection of cs identifiers.
  • For each identifier in the cs collection, select the state of the C object one by one.

Obviously, if you collect a collection of instances of A (for example, from a query result), all this is done for each instance of A

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

In addition, one of the reasons why the entity / collection cache should be executed in cluster invalidation mode (data is cached only on a node that reads / writes it, but is invalid on other nodes when changing).

+4
source

Source: https://habr.com/ru/post/1243142/


All Articles