没有ehcache.xml的Hibernate 2级缓存 [英] Hibernate 2nd level cache without ehcache.xml

查看:81
本文介绍了没有ehcache.xml的Hibernate 2级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Ehcache配置为休眠的第二级缓存,到目前为止,我发现的所有示例都指示您在类路径中创建ehcache.xml文件,例如:

I am trying to configure Ehcache as a hibernate 2nd-level cache, and so far all the examples I've found instruct you to create an ehcache.xml file in the classpath like:

<ehcache updateCheck="false">

    <diskStore path="java.io.tmpdir" />

    <defaultCache maxElementsInMemory="10000" eternal="false"
        statistics="true" timeToIdleSeconds="120" timeToLiveSeconds="120"
        overflowToDisk="true" diskPersistent="false" 
        diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

    <cache name="com.yourcompany.CachedEntity" eternal="true" maxElementsInMemory="1000" />     

</ehcache>

然后按以下方式配置Hibernate:

And then configure Hibernate as follows:

<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
<property name="net.sf.ehcache.configurationResourceName" value="/ehcache.xml" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />

在我的工作场所,我们鼓励您尽可能使用Java配置,并避免使用XML配置文件。我将不胜感激如何实现此目标。

In my workplace we are encouraged to use java config wherever possible and avoid XML config files. I'd appreciate any pointers on how this can be achieved.

推荐答案

stackoverflow问题在春季4中使用ehcache而不使用xml / 1812784 / learningjava> learningJava 显示了如何在Java中配置ehcache CacheManager ,但是您仍然需要一种方法来告诉hibernate它应该使用Java配置的 CacheManager

The stackoverflow question using ehcache in spring 4 without xml mentioned by learningJava shows how to configure an ehcache CacheManager in java but you still need a way to tell hibernate that it should use your java configured CacheManager.

一种可能的方法是通过 hibernate.cache.region.factory_class 属性。如果您查看 SingletonEhCacheRegionFactory ,您会发现在您自己的 CacheManager 中进行交换非常容易。

A possible way to this would be to configure a custom ehcache region factory via the hibernate.cache.region.factory_class property. If you look at the implementation of SingletonEhCacheRegionFactory you'll see that it will be quite easy to swap in your own CacheManager.

我怀疑缓存的java配置和xml配置之间的映射非常简单,但是如果ehcache在解析xml配置时在后台执行了一些魔术操作,则该工作正确设置Java配置可能会有些棘手。

I suspect that the mapping between the java configuration and the xml configuration of a cache is quite straightforward, but if ehcache performs some 'magic stuff' behind the scenes when it parses the xml configuration, the job of getting your java configuration right might be a bit trickier.

这篇关于没有ehcache.xml的Hibernate 2级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆