Spring 4 Setting up EHcache 3 Hibernate 5 default cache

My project is on Spring 4.3.4, Hibernate 5.2.4 and Ehcache 3.3

I'm looking for a solution with a single JSR-107 (JCache) CacheManager for the whole application that provides:

  • Spring caching - with a number of explicitly named caches;
  • Hibernate L2 cache - with the ability to implicitly generate any number of regions (which are also caches)

The main problem here is Hibernate. In fact, it is about setting up a default template for its regions. After reviewing many topics, I have not found a completely suitable solution.

So, if it exists, I ask the experts to point it out. Programmatic only way of configuring highly appreciated (ehcache.xml deprecated).

Added: (in response to @Anthony Dahanne): I already saw the solution in the project you point to before I posted this thread. Moreover, this project became the starting point of my research a week ago.

But this solution involves explicitly declaring several named Cache scopes not only for Spring, but even for Hibernate, as we can find in CacheConfiguration # createCacheConfigurations .

This is inconvenient in a real project that contains 100,500 different Entities, NaturalKeys, etc. that Hibernate manages, because for all of them it has to create the corresponding regions on the fly. I highlight the main power of the Hibernate RegionFactory as the ability to do this magical work automatically. But what do we see in the sharpened project in the JCacheRegionFactory custom class ? An outright ban on creating new regions, which is implemented through an unconditional exception that will be thrown on any attempt to do so (this is even noted with "... but ensures that all caches already exist to prevent spontaneous creation of poorly configured caches ... ").

The ideal solution would be to be able to customize a single "default template" (at the CacheManager level), as it does in the case of encache.xml (... jcache: defaults default-template = "myDefaultTemplate ...), ... but without xml.

+3


source to share


2 answers


There is a demo example

This example is based on Spring boot programmatically configures the ehcache3 cache , displaying it as JSR-107 CacheManager



This cache is used in conjunction with Hibernate and also independently .

+1


source


We don't need the default configuration (aka new MutableConfiguration

). However, setting a meaningful default is acceptable. So, in your case, I would use a customized one JCacheRegionFactory

.



See this article about it.

0


source







All Articles