<property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property>
<property name="cache.use_second_level_cache"> true </property> <property name="cache.provider_class"> org.hibernate.cache.EhCacheProvider </property>
<ehcache> <diskStore path="java.io.tmpdir"/> <!--默认cache的配置--> <defaultCache maxElementsInMemory="10000"<!--在缓存里最多多少个对象--> eternal="false"<!--是否从来不把内存的对象清除--> timeToIdleSeconds="12"<!--12秒没人来查这个缓存就将这个对象清除--> timeToLiveSeconds="120"<!--某个对象呆了120秒缓存就将这个对象清除--> overflowToDisk="true"<!--溢出的时候是否放到硬盘--> /> <cache name="sampleCache1"<!--缓存名字,可以想用哪一个缓存就用哪一个--> maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> </ehcache>
@Cache( (1)CacheConcurrencyStrategy usage(); (2)String region() default ""; (3)String include() default "all"; )例如:
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)NONSTRICT_READ_WRITE说的是非严格读写
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) public class Category { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
@Test public void testHuanCun2(){ Session session = sf.openSession(); session.beginTransaction(); Category c=(Category)session.load(Category.class,1); System.out.println(c.getName()); session.getTransaction().commit(); session.close(); Session session2 = sf.openSession(); session2.beginTransaction(); Category c2=(Category)session2.load(Category.class,1); System.out.println(c2.getName()); session2.getTransaction().commit(); session2.close(); }
发现第二次去缓存中去取了
转载请注明出处:http://blog.csdn.net/acmman/article/details/44132207