1. 首先設(shè)置EhCache,建立配置文件ehcache.xml,默認(rèn)的位置在class-path,可以放到你的src目錄下:
xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds= "120"/>
ehcache>
2. 在Hibernate配置文件中設(shè)置:
<hibernate-configuration>
<session-factory>……<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><property name="cache.use_second_level_cache">true</property>……</session-factory></hibernate-configuration>
此外,可以把cache.use_second_level_cache設(shè)置為false關(guān)閉所有的hibernate二級(jí)緩存。但此屬性對(duì)指定<cache>的類缺省為true。
3. 為了使用二級(jí)緩存,需要在每一個(gè)Hibernate Entity上配置。
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Forest {
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="CUST_ID")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public SortedSet getTickets() {
return tickets;
}
@Cache(
CacheConcurrencyStrategy usage(); (1)
String region() default ""; (2)
String include() default "all"; (3)
)
(1) usage: 提供緩存對(duì)象的事務(wù)隔離機(jī)制,可選值有以下幾種
(NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL)
(2) region (optional): 指定緩存的區(qū)域,默認(rèn)是類的全限定名。利用緩存區(qū)域,可以更精確的指定每個(gè)區(qū)域的緩存超前策略。如果指定了緩存區(qū)域前綴(在hibernate.cfg.xml中設(shè)置cache.region_prefix屬性為一個(gè)字符串),則所有的緩存區(qū)域名前將加上這個(gè)前綴。
(3) include (optional): all to include all properties, non-lazy to only include non lazy properties (default all).
如果不是使用annotation的話,則是在Hbm文件中添加cache usage="read-only"
xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds= "120"/>
ehcache>
2. 在Hibernate配置文件中設(shè)置:
<hibernate-configuration>
<session-factory>……<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property><property name="cache.use_second_level_cache">true</property>……</session-factory></hibernate-configuration>
此外,可以把cache.use_second_level_cache設(shè)置為false關(guān)閉所有的hibernate二級(jí)緩存。但此屬性對(duì)指定<cache>的類缺省為true。
3. 為了使用二級(jí)緩存,需要在每一個(gè)Hibernate Entity上配置。
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Forest {

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="CUST_ID")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public SortedSet getTickets() {
return tickets;
}
@Cache(
CacheConcurrencyStrategy usage(); (1)
String region() default ""; (2)
String include() default "all"; (3)
)
(1) usage: 提供緩存對(duì)象的事務(wù)隔離機(jī)制,可選值有以下幾種
(NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL)
(2) region (optional): 指定緩存的區(qū)域,默認(rèn)是類的全限定名。利用緩存區(qū)域,可以更精確的指定每個(gè)區(qū)域的緩存超前策略。如果指定了緩存區(qū)域前綴(在hibernate.cfg.xml中設(shè)置cache.region_prefix屬性為一個(gè)字符串),則所有的緩存區(qū)域名前將加上這個(gè)前綴。
(3) include (optional): all to include all properties, non-lazy to only include non lazy properties (default all).
如果不是使用annotation的話,則是在Hbm文件中添加cache usage="read-only"