Question about WildFly + Hibernate + Infinispan cache region

109 views
Skip to first unread message

Aljaž Koželj

unread,
Aug 17, 2023, 11:02:21 AM8/17/23
to WildFly
Hello,

recently I've been exploring performance gains that we can achieve by using 2nd level cache with Hibernate.

There are some entities that I want to keep in memory forever. They change very rarely and are not that numerous to begin with. From what I understand, one can achieve by setting lifespan and max_idle to -1 on the caching region.

As a test I added caching to a few entities (EntityA, EntityB, EntityC). Preloading entities into the cache resulted in 3 entity cache regions being filled. All are in the form of:

<deployed-artefact>#<persistence-unit><class-name-with-package>

Each entity has it's own caching region, is there a benefit to that? I assume these regions retrieve their configuration from the "entity" region that is also defined in standalone-full.xml.

In persistence.xml I managed to override lifespan and max_idle using the property as such:
<property name="hibernate.cache.infinispan.my-ar.war#MyPersistenceUnit.path.to.entity.EntityA.eviction.wake_up_interval" value="2000"/>

I have bolded out the name of the caching region. This works but it's awfully inconvenient. I would have to do this 2-3 times for each Entity!

Is there an easier solution? I know I can assign a specific caching region for each Entity using @Cache(region ="some region"). And then I can configure the properties for this specific region. Would that be reasonable? Is there a penalty for having 10 entities in the same caching region?

Thanks,
Aljaž

Paul Ferraro

unread,
Sep 2, 2023, 4:21:00 PM9/2/23
to WildFly
On Thursday, August 17, 2023 at 11:02:21 AM UTC-4 Aljaž Koželj wrote:
Hello,

recently I've been exploring performance gains that we can achieve by using 2nd level cache with Hibernate.

There are some entities that I want to keep in memory forever. They change very rarely and are not that numerous to begin with.

This is the primary use case for the second level cache.
It is not intended for entities that change frequently.
 
From what I understand, one can achieve by setting lifespan and max_idle to -1 on the caching region.

Correct.
 

As a test I added caching to a few entities (EntityA, EntityB, EntityC). Preloading entities into the cache resulted in 3 entity cache regions being filled. All are in the form of:

<deployed-artefact>#<persistence-unit><class-name-with-package>

Each entity has it's own caching region, is there a benefit to that? I assume these regions retrieve their configuration from the "entity" region that is also defined in standalone-full.xml.

Correct.
 
In persistence.xml I managed to override lifespan and max_idle using the property as such:
<property name="hibernate.cache.infinispan.my-ar.war#MyPersistenceUnit.path.to.entity.EntityA.eviction.wake_up_interval" value="2000"/>

I have bolded out the name of the caching region. This works but it's awfully inconvenient. I would have to do this 2-3 times for each Entity!

Is there an easier solution? I know I can assign a specific caching region for each Entity using @Cache(region ="some region"). And then I can configure the properties for this specific region. Would that be reasonable?

Yes.  We call this a custom cache region.  What you are proposing is precisely the intended usage.
Your persistence.xml will need to indicate to the second-level cache implementation which cache to use for your custom region.
e.g.

<property name="hibernate.cache.infinispan.your-region-name.cfg" value="your-cache-name"/>
 
Is there a penalty for having 10 entities in the same caching region?

No.
 

Thanks,
Aljaž
Reply all
Reply to author
Forward
0 new messages