如何在Spring MVC和Hibernate中使用ehcache


问题内容

我是spring-
mvc的新手,并且希望将ehcache集成为hibernate中的第二级缓存。我遵循了本教程ehcache, 现在hibernate.xml中的条目如下:

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

ehcache.xml中的条目如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

<diskStore path="java.io.tmpdir"/>
<!--defaultCache
        eternal="false"
        maxElementsInMemory="1000"
        maxElementsOnDisk="10000"
        overflowToDisk="true"
        diskPersistent="true"
        timeToLiveSeconds="300"
        statistics="true"
        copyOnWrite="true"
/-->

<cache name="com.payupaisa.cms.model.Event" 
 maxElementsInMemory="100000" 
 eternal="true" 
 overflowToDisk="false"
 memoryStoreEvictionPolicy="LFU"
 statistics="true"
timeToLiveSeconds="3600"
/>

</ehcache>

我们正在遵循mvc模型,并且在模型中我定义了注释

@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY,
region="department")

现在的问题是如何开始在服务层中使用此缓存。我没有在项目中创建hibernateUtil.java。我们正在使用基于Web的Spring-Hibernate
MVC应用程序。现在如何开始,我没有。


问题答案:

这个例子展示了一个整合Spring + Hibernate + EHCache的例子。