[hibernate] hibernate 和Ehcache集成配置
1.hibernate.cfg.xml
<!--hibernate 二级缓存 --> <property name="cache.use_second_level_cache">true</property> <property name="cache.use_query_cache">true</property> <property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory </property>
ehcache.xml:
<ehcache> <!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <!-- 对象超出缓存最大限制时, 存储在硬盘里的位置。 java.io.tmpdir: 指的是系统默认的缓存路径, 可以将其改为固定的路径。 --> <diskStore path="java.io.tmpdir"/> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <!-- 默认使用的配置。 maxElementsInMemory: 二级缓存中最多能存多少个对象。 eternal(永恒的): 若为true则固化缓存数据不变; 若为false则动态维护缓存; timeToIdleSeconds: 允许对象在缓存中空闲的时间,超过该 时间则从缓存中移除。 timeToLiveSeconds: 对象在缓存中存活的时间,超过该时间 则从缓存中移除。 overflowToDisk: 如果对象数超过了最大限制,是否将 多于的对象缓存到硬盘中,存储位置 通过标记diskStore指定。 --> <defaultCache maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="300" overflowToDisk="true" /> <!--Predefined caches. Add your cache configuration settings here. If you do not have a configuration for your cache a WARNING will be issued when the CacheManager starts The following attributes are required for defaultCache: name - Sets the name of the cache. This is used to identify the cache. It must be unique. maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <!-- 明确全类名的配置 --> <cache name="com.xwolf.hibernate.bean.Father" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <!-- Sample cache named sampleCache1 This cache contains a maximum in memory of 10000 elements, and will expire an element if it is idle for more than 5 minutes and lives for more than 10 minutes. If there are more than 10000 elements it will overflow to the disk cache, which in this configuration will go to wherever java.io.tmp is defined on your system. On a standard Linux system this will be /tmp" --> <cache name="sampleCache1" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <!-- Sample cache named sampleCache2 This cache contains 1000 elements. Elements will always be held in memory. They are not expired. --> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> <!-- Place configuration for your caches following --> </ehcache>
2.Father.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.xwolf.hibernate.bean.Father" table="FATHER" schema="DATAPROCESS"> <!--缓存的配置--> <cache usage="read-only"/> <id name="fatherId"> <column name="FATHER_ID" sql-type="number" precision="22" not-null="true"/> </id> <property name="fatherName"> <column name="FATHER_NAME" sql-type="varchar2" length="400"/> </property> <property name="createTime"> <column name="CREATE_TIME" sql-type="date"/> </property> <property name="status"> <column name="STATUS" sql-type="varchar2" length="40"/> </property> </class> </hibernate-mapping>
3.测试:
/** * Ehcache 配置查询 */ @Test public void testCache(){ String hql ="from Father"; Query query = session.createQuery(hql); //启用查询缓存 query.setCacheable(true); List<Father> fathers=query.list(); for (Father father:fathers){ System.out.println(father.getFatherId()+","+father.getFatherName()); } System.out.println("**********************************"); String hql1 ="from Father"; Query query1 = session.createQuery(hql1); query1.setCacheable(true); List<Father> fathers1=query1.list(); for (Father father:fathers1){ System.out.println(father.getFatherId()+","+father.getFatherName()); } }
参考官方文档:

浙公网安备 33010602011771号