I’m trying to introduce an ehCache 3.x based 2nd level cache into an existing application which uses Spring Framework 5.3 and Hibernate 5.6. (Mind, we are not talking about a Spring Boot application.) I’d like to rely solely on Java-based configuration (i.e. @Configuration
classes), which would allow me to read configuration values (like max cache size, time to live etc.) at run-time from @PropertySource
s — as opposed to a static ehcache.xml
file, as showcased in the vast majority of tutorials out there.
Ideally, such a programmatic hibernate caching configuration mechanism should allow to
- define default configuration values for caches
- a mechanism to optionally override those default values for individual caches (aka “regions”?)
- set configuration values referring collectively to all caches (like a max amount of memory allowed to be used by all caches in common)
The closest I came to these goals was a blog post by Henri Tremblay (http://blog.tremblay.pro/2017/02/jcache.html), which I stumbled over (the blog post, I mean, not Henri 😉 after doing literally hours of research. Howver, although the post deals with both Spring and Hibernate, it doesn’t actually bridge the two technologies, i. e. the Hibernate part doesn’t benefit from all the goodies provided by Spring (like @Value
s read from @PropertySource
s). Also, when overriding JCacheRegionFactory
methods (as suggested in the post), you quickly find yourself limited due to not having access to private fields hidden within the parent class (like missingCacheStrategy
or cacheManager
) — which proves that it is usually a good idea not to rely on the internals of other classes if you can avoid it.
So… any ideas how to achive the goals described above?