What is the Hibernate Layer 2 cache solution?

We are using Hibernate with JPA in the project we are currently developing. Currently working on Wildfly and future features - this is a commercial version depending on the needs.

Some alternatives for the second level cache are listed in the Hibernate documentation here . There are also other solutions such as Hazelcast . Among the alternatives, it seems that JBoss officially supports the Infinispan solution .

As in many projects, we have some tables that will rarely change, if at all, for example, a list of cities, statuses that a project can have, etc. Therefore, our decision to use the second level cache for such cases.

  • Could you share your experience in such a scenario? Is this a good use case? Is there a better way to save such data? How do you process data that rarely or never changes?
  • Which provider have you used in the past and / or are currently using as a second level cache and why? What are the advantages and disadvantages of the solution you have chosen?
+5
source share
3 answers
  • Layer 2 cache is a cache of relationship data, not full-featured object caching. Thus, these are only the basic properties of the entity and foreign keys that are saved. Regarding entity associations, * -To-One can be stored in different areas of caching, while "-To-Many" relationships also require the inclusion of a query cache.

    I think the second level cache is much more useful when your entities actually change from time to time. The second level cache entry is invalid when the entity is identified as dirty.

  • But if you rarely change your data, it's probably best to use the cache of the real object. The object cache will store the entire tree of relations of objects, and this is a much better alternative if you mainly work with entities, unlike projections related to free style.

    So, if your workflows revolve around objects with hierarchies, then a cache object is more appropriate.

  • If your application does not actually work with entities, but with various data connecting the projections, then you should add DB indexes and make sure that there is enough memory for the database server to work with data from memory, without getting to disk.

Decision:

I recommend that you use HazelCast , which is a high-performance data network in memory. HazelCast allows you to either integrate it as a second-level cache, or you can save whole graphs of objects if you want. Just follow their documentation on how you can integrate it into your current application.

+2
source

I used ehcache to store and cache geographic data. It is very convenient to use. Take a look at their website http://ehcache.org/ .

+1
source

The second level sleeping cache is best suited for data that rarely or never changes. However, since hibernate provides a generic caching provider implemented by several caching solutions, it limits the usability of the features provided by caching solutions.

As a solution, I recommend using TayzGrid as a sleep mode of the second level, since along with the main functions of caching and expiration, etc. it also provides database dependencies, i.e. data in the cache will be automatically locked by the cache if the corresponding entries in the database change. You can find more information here: http://www.alachisoft.com/tayzgrid/hibernate-second-level-cache.html

+1
source

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


All Articles