1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下: <?xml version="1.0" encoding="UTF-8"?>9 E; q! ]) ]+ Y/ s
<ehcache>- m+ E9 V# o' i( G# x8 R" Q: ?
<diskStore path="java.io.tmpdir"/>
8 Z G' H3 L P% ^ <defaultCache) M7 B3 u' K! ]' a9 b, G3 B& {
maxElementsInMemory="10000" <!-- 缓存最大数目 -->
2 x9 e5 ], ^- j eternal="false" <!-- 缓存是否持久 --> overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时--> timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->0 Q2 s$ n8 K7 y+ N, M: f
timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->
8 L5 o ~$ v, j: P& |; S9 T' v! p diskPersistent="false"* ^( _. x4 m+ G# H5 k/ C3 |
diskExpiryThreadIntervalSeconds= "120"/>
1 ]$ P' v: a, M+ S8 s</ehcache> 2、在hibernate配置文件中设置: <!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->
, S! T& s! I) d2 Y( v<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>" X7 S2 t b6 R+ z
<!-- 是否使用查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property>/ d$ _8 G; b6 R" |7 G; A
如果使用spring调用Hibernate的sessionFactory的话,这样设置:0 `. o3 F$ F2 t' F: H( |
<!--HibernateSession工厂管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
: Z% ?7 h& W. [( m9 Z5 L) J <property name="dataSource">
% a' u$ U6 F( y& T <ref bean="datasource" />
! l; o' k8 {) f4 m4 p" G* |; D ^0 \ </property>: d/ z6 P' H" A" H. @8 A* O$ ]
<property name="hibernateProperties">) n8 X3 r' h9 f) i( t" E0 r
<props>
/ e2 T# }: C/ ~& W; y! I <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>7 v, A! a+ B0 D
<prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>* m9 Z4 R' o! r
<prop key="hibernate.show_sql">true</prop>
& {. X3 t% b2 L7 k; p <prop key="hibernate.cache.use_query_cache">true</prop>
/ @) M" d/ d4 i2 o <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
( ]& R, H4 ]3 @ </props>5 O0 r) `" k- {! R4 N" _6 E; F7 m; x
</property>
, B; V' I# r7 D6 z) h <property name="mappingDirectoryLocations">
1 ?4 H* ~) t1 b$ a <list>$ \) D _; t1 ?$ U) t7 K$ c* ~# h
<value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>
6 R/ J1 v& }% [( p: Y# E </list>
0 A2 l: m6 Z+ P$ M </property>7 |$ M6 Q7 E; M5 Q9 D
</bean> 说明一下:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行 3、在Hbm文件中添加<cache usage="read-only"/> M( P8 @7 ?8 d! v
4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性 5、实践出真知,给一段测试程序,如果成功的话第二次查询时不会读取数据库 package cn.rmic.hibernatesample; import java.util.List; import org.hibernate.CacheMode;! `7 D [5 f1 W- [( S8 J
import org.hibernate.Criteria;: B4 t3 ^8 h% N; a/ B
import org.hibernate.Query;
8 u1 I5 b% z) z7 jimport org.hibernate.Session; import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;
9 \" x$ I6 c! Z0 T2 Vimport cn.rmic.manager.po.Resources; public class testCacheSelectList ...{ public static void main(String[] args) ...{+ m7 ^8 `+ h* i! E$ \' {
Session s=HibernateSessionFactory.getSession();
! ^ E8 Q! e) o Criteria c=s.createCriteria(Resources.class);
# K8 G6 H. ?; X c.setCacheable(true);+ O) _6 T1 ^: j' U. N& m6 b% D) f
List l=c.list(); Resources resources=(Resources)l.get(0);5 x6 g0 Z4 }- {
System.out.println("-1-"+resources.getName());
% L% _1 a0 D* ?) a8 X HibernateSessionFactory.closeSession(); ' f7 W& o H/ z: D. q% a2 g
try ...{
9 K2 k( a W7 ?" n( e2 r Thread.sleep(5000);
$ F/ C3 |' [$ ~; O2 N- V3 K } catch (InterruptedException e) ...{ |$ j4 o) X& P6 n4 J
// TODO Auto-generated catch block
( ] d8 c; c4 z, w" { e.printStackTrace();
5 C9 W% l' E& j" r! a* h. z6 X }
+ v: V9 F) i. U4 d s=HibernateSessionFactory.getSession();
2 W7 `3 J$ G p c=s.createCriteria(Resources.class);% L! j b0 R$ H, U# `! J* G
c.setCacheable(true);
5 Y, X4 C( K( B4 O5 m l=c.list();
- @/ g' e8 v9 c; }) j3 ~: d8 L resources=(Resources)l.get(0);
7 a- {. H! o" `4 V# V6 u0 _ System.out.println("-2-"+resources.getName());
, r# g/ j# c7 S. K9 j' e, z+ B9 T HibernateSessionFactory.closeSession();, R5 T* q- p: o) b* f
}" T- ~5 c# Q0 A$ @
} 0 t# s' }; Y5 J+ |% T
1. 在Hibernate配置文件中设置:3 W7 G9 f$ j M% O
<!-- Hibernate SessionFactory -->/ v# T2 l4 w! j5 b
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">3 S/ S2 }2 M; F7 s" z) z
<property name="dataSource" ref="dataSource"/>
' Z8 b% B8 K4 c. E ]0 F) G <property name="mappingResources">
4 s2 Y* n& a! A; l3 R3 c9 f! P3 A: ]9 D <list>
" Q8 L+ i! C: \# h, J7 T. |- T <value>com/ouou/model/Videos.hbm.xml</value>
9 U8 x E) x; t </list>9 e. u4 J, ~: e/ ^! F! O1 Y( Z7 Q
</property>; i) v) k& E3 ?+ w j. [& P
<property name="hibernateProperties">3 c% S; r/ w. G; E5 I. E) v: b1 i0 f
<props>) |- ~0 ]+ e! ^3 E4 v# {
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
/ V8 \/ G" U7 D4 L <prop key="hibernate.current_session_context_class">thread</prop>' }: j+ E& s9 e) e0 y- u
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
+ Y& H; B0 g& m <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
+ b4 s+ K: D8 w+ B I <!--add ehcache-->; T: q0 W: F: M' O* _
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
9 z* z. f2 j6 Z9 L; q" P <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->8 K/ s/ w' w5 @! e- E
<!--
; X& t& p, c& E& I# c& G <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
" m$ T1 f( g: G' J# ? <prop key="hibernate.show_sql">true</prop>
_0 B" I1 o O _7 F" D' ^; n -->
I# s z( c0 M8 }. v; M$ t <!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
1 j5 ~: f' w) N <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>, |0 f% m( K: s! m3 i
<!-- Create/update the database tables automatically when the JVM starts up
/ ^6 Y0 ^7 v+ C. ~( J# C <prop key="hibernate.hbm2ddl.auto">update</prop> -->
: h1 t1 q' W( l, ~+ k <!-- Turn batching off for better error messages under PostgreSQL -->7 P- a c& F. Q4 O
<prop key="hibernate.jdbc.batch_size">25</prop>
4 O1 K" w. a# p: J <!--8 U" P. }2 D8 C. h
<prop key="hibernate.connection.pool_size">10</prop>8 q" `+ X- z7 E& J
-->
( B) U) R0 ]6 b7 k" z5 L </props>
W$ _* U. v; e </property>
7 P1 d6 y. H0 Z9 j* ?8 Q- ? </bean> 如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行 2.首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下: . R \1 U2 P* T
<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' D+ Z% f& E; l! K7 f+ g
its value in the running VM.' I2 d' m0 m: c* H2 s( _* l" j
The following properties are translated:
8 m3 \+ J2 v V6 b- o user.home - User's home directory
1 u9 m0 |2 W2 A3 T8 I: f user.dir - User's current working directory" j9 n& x1 f. N+ o# b
java.io.tmpdir - Default temp file path -->: j) B5 M; V3 c. ~
<!--<diskStore path="java.io.tmpdir"/>-->
4 G; B- Q4 v# |, ^5 E <diskStore path="/data/ehcache"/> <!--Default Cache configuration. These will applied to caches programmatically created through. M% I+ Y. |2 W3 ?9 J
the CacheManager. The following attributes are required: maxElementsInMemory - Sets the maximum number of objects that will be created in memory
/ T* Q# R+ \$ E2 X3 @" N+ b7 r eternal - Sets whether elements are eternal. If eternal, timeouts are
2 ]; @3 {' H6 L( {+ E ignored and the element is never expired.
. V: i2 t4 ~2 H6 ? overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache" [# n1 h, L- Z o, L" ]
has reached the maxInMemory limit. The following attributes are optional:
0 ]+ w5 B% Q1 d+ E7 m" L( R5 O) W2 n timeToIdleSeconds - Sets the time to idle for an element before it expires.
+ ]/ a: ^, c K: w, c, g( Y i.e. The maximum amount of time between accesses before an5 g. P c- {; m5 f6 W# b- L; b
element expires Is only used if the element is not eternal./ q# U1 ]7 a. v* V; G# S. Q% X
Optional attribute. A value of 0 means that an Element can idle y6 J: G) N: l* X' u# q
for infinity.The default value is 0.
' S% d6 x' w3 K& G& D4 O/ s" z* a timeToLiveSeconds - Sets the time to live for an element before it expires.' I; [: A/ z2 L0 A9 x; O$ K
i.e. The maximum time between creation time and when an element) B6 ^! \9 x, O2 s6 J0 Q
expires. Is only used if the element is not eternal.
2 w0 C7 l- R3 ~! A" m1 R. M$ Y Optional attribute. A value of 0 means that and Element can live: O4 d% I9 y3 R7 Y+ s
for infinity.! C$ v, W- Z. H: Z9 ~
The default value is 0.
. r+ V8 ^, @. H3 l6 G diskPersistent - Whether the disk store persists between restarts of the Virtual0 N8 n, _3 f' W/ M. V5 A
Machine.
: M' j: i0 h* S The default value is false.
; ~1 t1 J, [' I0 _- G$ T5 z diskExpiryThreadIntervalSeconds - The number of seconds between runs of the disk expiry thread.
N# r$ O* @/ X6 P2 p The default value is 120 seconds.4 o) b) B! {7 a' H
--> <defaultCache
. g3 ^5 s0 w* f maxElementsInMemory="10000"2 L M% b5 r1 _- j3 I
eternal="false". Q' L. ?& _5 I; ^& F; o- w4 s
overflowToDisk="true"
8 |$ ?; y9 k h0 o: Y timeToIdleSeconds="120"4 D5 P+ ?5 n) O: H
timeToLiveSeconds="120"
: z3 c9 j& {; f* g7 M9 W diskPersistent="false" B' T' @3 L, ~9 C7 p( R" B- G: e
diskExpiryThreadIntervalSeconds="120"/>6 y" _8 Q% l; A# e# H" f; v/ ]
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"6 o5 x( l# A) Y* [. Y
eternal="true" overflowToDisk="true"/>" }, t& h l8 C( A5 N7 W2 ^
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false") P, d/ b( ^9 }8 ?' S; w
timeToLiveSeconds="120" overflowToDisk="true"/>
: }& L1 f% v8 f <cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
5 G/ Q! d; R8 L) r+ j "600" timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>+ y- Y. E3 o0 k, |# b
<cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"
A; h' n3 n8 c' v( p eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
1 Q2 O K5 P) w diskPersistent="false"/>2 q, M5 P& }( M E }2 s
<cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"
& M; J: U; n/ _9 _2 x& @% l9 k& ] timeToLiveSeconds="600"
8 n2 e( F& F+ y7 G' h. y overflowToDisk="false" diskPersistent="false"/>
% k2 i5 F, w! i* i- F2 O6 Q) u, D <cache name="com.ouou.model.Videos" maxElementsInMemory="10000" eternal="false"& J3 H6 a1 E5 {0 S( x: l' l2 d9 y* i
overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
C5 C2 ]) z, k( r1 k <cache name="com.ouou.model.Tags" maxElementsInMemory="10000" eternal="false"
- R1 b0 a+ h ^7 b6 b overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
% S1 j" S W" T1 v& _2 p/ X' U</ehcache> 以com.ouou.model.Videos为例子
+ l- Z) _/ L5 Q2 d" I) f在Videos.hbm.xml中配置:4 Y1 Z# P( U/ {8 [
<class name="Videos" table="TEST" lazy="false">7 a+ J0 r7 {& _2 S' I3 i
<cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。
& l* S" R- r2 e. q' T( V* a+ A( nhbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,9 f8 _6 P; `; E8 k1 S1 v
如果不存在与类名匹配的cache名称,则用defaultCache。
( J3 t3 I8 M8 x6 }如果Videos包含set集合,则需要另行指定其cache
8 I' P1 \$ f% n) }) V2 m例如Videos包含Tags集合,则需要
9 U' T9 @5 x: R添加如下配置到ehcache.xml中9 @0 I# f* v5 s
<cache name="com.ouou.model.Tags"
V t/ l, D$ v maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"/ a& }7 |& y' Q6 c [7 E
timeToLiveSeconds="120" overflowToDisk="false" />. x5 L4 b+ Z7 N9 _6 B8 Z4 j
另,针对查询缓存的配置如下:: w+ Y0 k4 g, I4 S: K
<cache name="org.hibernate.cache.UpdateTimestampsCache"* s, \5 |% L0 c$ f2 w0 }% [
maxElementsInMemory="5000") m K6 o& i& ~ e8 T
eternal="true"
: \) n( Z7 v1 H: `8 Y overflowToDisk="true"/>
0 R3 D& I( P }( W* |<cache name="org.hibernate.cache.StandardQueryCache"
# C- F9 T9 ^' ?# o$ r* u+ i maxElementsInMemory="10000"$ G9 h7 \- i& Y, @
eternal="false", i U* j6 |- E3 ]' \# E
timeToLiveSeconds="120"/ M3 T0 B- ?! I$ p5 O. a
overflowToDisk="true"/> 3、 选择缓存策略依据: <cache usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>( d( Z' e- s. Z# m: X$ I0 t
ehcache不支持transactional,其他三种可以支持。+ \5 G6 Z2 H( t7 T; e
read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,2 \1 d' Q# B/ \0 k
但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。: B$ k0 I8 {- t! A' _* I8 P
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level4 n7 F' ?8 O1 j. s0 S1 U- Q
(序列化事务隔离级别)
; R4 L2 d w6 X( s. W( Vnonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,
6 t) V2 r; @7 s3 o4 q- ?# i那么比较适合使用非严格读/写缓存策略。 4、 调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。 5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (6 ^1 u3 H1 q* [
org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。
+ p, V( R. ]: q8 Y* y) K请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。" g7 f* R; L; x& d3 @
需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。
/ R9 y7 Q* W: |" Y7 i7 QEhcache的配置说明
. f9 n3 @* E: c7 o6 {* M* d<ehcache>
5 g9 [& D; h" V3 K<!--
2 B1 e+ t4 U' _磁盘存储配置:
4 {. C) `: a9 i/ @用来指定缓存在磁盘上的存储位置。可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)2 @. V- v8 Q, n' W# h$ |4 a$ a" S2 U
-->2 A- j, A8 K' t' R7 x
<diskStore path = "/var/apps/cache/" />2 g+ j5 ^* x% j9 J" h& z" N
<!--0 N8 P+ Q/ U5 I9 f- N& j
指定CacheManagerEventListenerFactory,这个对象在缓存添加的时候会得到相应的通知8 a( F& F! `! o( R6 k, |
CacheManagerEventListenerFactory的属性; |8 C) Y' X9 s+ `1 M* y8 W
*class - CacheManagerEventListenerFactory的一个实现类; |6 n2 V3 e# g' @& W' e) X* s
*properties - CacheManagerEventListenerFactory的属性值,以逗号(,)分割多个属性
' w+ @7 c! ?3 z6 u如果没有实现类被指定,则系统不创建CacheManager的监听器,没有默认值% u9 d+ ]. F s* P
-->
K9 b5 K {! a8 t/ h7 }* X<cacheManagerEventListenerFactory class="" properties="" />
' }9 E' ~$ h6 q; o+ d+ ^<!--
1 n. Z1 I$ P7 y( Z% o! S* u在进行分布式缓存的应用时候需要指定CacheManagerPeerProviderFactory,
" s! j. Q) N. @* i& K用来生成CacheManagerPeerProvider的实例,以便和集群中的其他CacheManager通信。
6 t& T% p# Z+ V; K) D' x*class -CacheManagerPeerProviderFactory的一个实现类3 [, L- D7 a, x# p# N8 E
*properties - CacheManagerPeerProviderFactory的属性值,以逗号(,)分割多个属性
/ z& D: p8 j. {! O5 OEhcache内建了2种基于RMI分布系统的通信策略:
6 I0 C6 P# E8 B- R5 T*automatic - 使用多播组。在一个节点加入或者推出集群的时候自动感应4 @: k# Y3 p9 ^3 p7 J1 W
*manual - 硬编码方式
目前的awf中不考虑分布缓存9 ?; ?2 m5 Z _7 W7 s
-->: |/ a& j, R$ n8 G6 F- a
<cacheManagerPeerListenerFactory class="" properties="" />( k, x$ G! x; k, I" c3 K" k
<!--
9 f* r. g+ |6 w0 x1 U& O缓存配置。7 s' x7 G0 q F0 \+ J; w8 M; I
以下属性是必须的:! s8 p% J% e& G2 h( q
name - cache的标识符,在一个CacheManager中必须唯一
" m- |& a# |/ g7 ~6 Q, rmaxElementsInMemory - 在内存中缓存的element的最大数目
! m c5 h; r3 R; F6 Y( tmaxElementsOnDisk - 在磁盘上缓存的element的最大数目
/ H8 x. E9 a" D5 Y2 |/ |eternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
% n9 i" L+ e9 H+ |5 \& JoverflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
2 R3 y) P; m5 l) \以下属性是可选的:# z: e; `' D9 Z. {
timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.! O6 W( C$ } O/ ~, P E
(如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)
5 X$ F0 k8 o8 T1 VtimeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期& S6 k, P& x8 `
(是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
h; R) s* E: n% ediskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。
5 q* d8 n ~2 N& Y W (测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)3 J. {4 h3 n- Q0 B, T+ }9 q
diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.
/ x, M/ E, ?% B8 m# I k# l. _/ B (测试一下0的时候会如何): @1 _7 a3 X2 g9 A4 N# u
memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,
2 m) S) S0 s) s# }1 o, { 移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO 可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些
. d+ ^$ h( I: w*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件5 p3 \2 B u. m, C; K& p
*bootstrapCacheLoaderFactory - 启动时加载缓存的element3 F% H# V* z& Z5 h
每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。1 J# p6 J% }! K f( u& l4 V) }
Ehcache内建了基于RMI的实现 - RMICacheReplicatorFactory+ P: l J& M* y# ~& d# E* @) \2 I
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" j' X" Y( f6 ]
properties="replicateAsynchronouly=true,
. B8 W3 v% h. } replicatePuts=true,
0 G, Z" j1 \* D2 _, { replicateUpdates=true,- F7 \; h% C' C. @2 @( O& w6 S
replicateUpdateViaCopy=true,- B, v, N7 p1 O* d' S2 g( @" T( O
replicateRemovals=true" /> -->
4 p) V# t5 q2 l+ @<cache .... />1 x, K+ c5 {6 d9 w$ C( S$ p
<!--
7 [3 B) T1 Z. V+ c, R默认的Cache配置。用来实现CacheManager.add(String cacheName)创建的缓存, W, J6 U+ y$ k4 p. _: \0 H
-->/ i1 _* r% q& t. v- t( X: V" \" E
<defaultCache maxElementsInMemory="10000" eternal="false"
6 R, T- y6 e3 V. `4 P7 t f/ I B timeToIdleSeconds="120" timeToLiveSeconds="120"
. f& @$ Q0 ~0 b4 C: _ overflowToDisk="true" maxElementsOnDisk="1000000"
- M- [9 ^; j z3 D# n diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
/ F5 a, H8 N* f# s, G memoryStoreEvictionPolicy="LRU"
( g- K/ L0 s5 b9 E1 R! I /> </ehcache>
: ~0 h& f. |- c7 m) A! w: x |