|
1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下: <?xml version="1.0" encoding="UTF-8"?>5 m' z9 U- M, D. F1 a1 ~$ r/ s
<ehcache>
$ C% B! O9 j. t2 M <diskStore path="java.io.tmpdir"/>3 }! U" i* b, I/ c' ^
<defaultCache( i9 |$ O5 l5 j; c
maxElementsInMemory="10000" <!-- 缓存最大数目 -->
8 T6 q. ~' v+ } eternal="false" <!-- 缓存是否持久 --> overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时--> timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->
) [) L! _# o) T$ ?' }3 e timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->* u2 w, G1 W3 {! ?" z% s9 N5 m1 A
diskPersistent="false"
- [* W" x8 Z9 p2 C7 ~ diskExpiryThreadIntervalSeconds= "120"/>' |! ` }7 s# `0 q
</ehcache> 2、在hibernate配置文件中设置: <!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->( m8 K' a$ I+ q+ C
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
2 P: F o3 [6 M( U8 G, X4 c- p <!-- 是否使用查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property>
B0 x8 g2 I$ P7 [$ |2 ~/ } 如果使用spring调用Hibernate的sessionFactory的话,这样设置:
5 O7 ^. ?" L- @ <!--HibernateSession工厂管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
: g4 R. @9 y3 V <property name="dataSource">
6 O8 ?% a' w7 Z; z; x0 Y/ ` <ref bean="datasource" />
& Y4 R* X* C* x/ m8 U </property>
9 L; ` {& f5 S c$ m <property name="hibernateProperties">
3 Y Y$ r3 ^& E! A <props>
' J) l0 d" a) b& L <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>, { a5 H. t4 T8 q8 t* S
<prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>" n6 A, I0 W1 K% M+ {
<prop key="hibernate.show_sql">true</prop>2 E- K+ \7 L% U/ O% r/ ^0 \
<prop key="hibernate.cache.use_query_cache">true</prop>
' P3 i* c' V7 h: p, m* Q( ?( t <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>& Y+ U% T/ a$ `! K, B7 B
</props>! y( q6 D S6 ?
</property>
5 f7 f7 Q! t5 L& a3 R/ _+ s <property name="mappingDirectoryLocations">
6 Y- p* H# l; ~# [+ T- F" L <list>
! e' o2 G3 j* W0 m! z <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>
7 X% A5 v3 F' k# }# D& v </list>
6 l( ?' A! C2 q( q& I+ z. y </property>4 ]0 j* r+ ^* E, |
</bean> 说明一下:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行 3、在Hbm文件中添加<cache usage="read-only"/>
- R% J2 x; x2 l6 c) U4 W# _& a 4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性
5、实践出真知,给一段测试程序,如果成功的话第二次查询时不会读取数据库 package cn.rmic.hibernatesample; import java.util.List; import org.hibernate.CacheMode;
- \( K- q- e' {5 J, ^import org.hibernate.Criteria;* a z" \$ a h9 Z
import org.hibernate.Query;5 h J$ O- d |1 x. N% x/ ~, [) R" C
import org.hibernate.Session; import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;2 s3 {2 S3 f5 t7 q, z; z; K9 w
import cn.rmic.manager.po.Resources; public class testCacheSelectList ...{ public static void main(String[] args) ...{
7 D+ c" W' F" T1 n. i Session s=HibernateSessionFactory.getSession();
1 e2 O, |5 E2 l3 ^. W$ V6 f( R Criteria c=s.createCriteria(Resources.class);
' D- K3 l O) q0 w4 x" a4 ?; P c.setCacheable(true);
0 G$ z; ^$ ^4 R6 N: N! T! I( z List l=c.list(); Resources resources=(Resources)l.get(0);" P0 ^: _4 Q8 y+ M" y: F; X" \
System.out.println("-1-"+resources.getName());" [% K' M) X: t7 t N
HibernateSessionFactory.closeSession();
1 J% P) i( e/ x( n- a$ t# o try ...{( g& W5 Q( L, A9 K8 j* d, t' E
Thread.sleep(5000);
* e" T8 D$ M5 s9 a8 D. N } catch (InterruptedException e) ...{$ Y! H3 D% f; g$ v
// TODO Auto-generated catch block4 L* r* u% i# n$ S( i: f" v2 b
e.printStackTrace();
% Q$ q& a4 }7 m3 l6 `% u( q }* j, ?- E7 v" D
s=HibernateSessionFactory.getSession(); m# d5 t5 T+ W
c=s.createCriteria(Resources.class);
- v9 u! s. d9 [5 x1 p9 J ?: I c.setCacheable(true);/ Q+ N$ D' |7 E. H
l=c.list();1 Q% E7 T& ?. h7 i* J. u, c2 q2 r+ S
resources=(Resources)l.get(0);
- ~! ?1 R! h; F0 `4 I/ J3 }* A: y System.out.println("-2-"+resources.getName());
% b( s9 T! N7 h- {5 n HibernateSessionFactory.closeSession();
# {) Q) a: {2 e& l+ o3 [1 I }
: M$ i1 D5 ]- G' E- ~}
$ ]3 e& G$ J1 B! ?; x/ w1. 在Hibernate配置文件中设置:" n2 Z7 t2 f# q2 ~$ d: H- B
<!-- Hibernate SessionFactory -->9 ?0 n0 D. H7 ?) M2 \1 q
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
0 j- ^2 S! _' t; G <property name="dataSource" ref="dataSource"/>
% @: x& q) W7 F$ {, a0 n <property name="mappingResources">3 L& ~* }9 ^# m$ Z8 m: C
<list>& {/ m. I! D* n( ^# {9 \: l* f
<value>com/ouou/model/Videos.hbm.xml</value>
1 [- a; c: z# A: X9 o2 k. `% V </list>, s( x$ ]) w9 _7 {* ?
</property>0 x+ U" ~; e- D2 j9 O7 ?
<property name="hibernateProperties">' Q7 a0 w5 d( m; D- I
<props>
7 H9 b7 C" O" U' L <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
) D, f8 ]4 W/ j% B' ]3 z <prop key="hibernate.current_session_context_class">thread</prop>
- R, ?2 X5 S# V$ d: q, \% v <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
$ ~; z. t3 _$ z. a& Q5 N <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
6 X4 ?) _) b# N* ]- T) q <!--add ehcache-->; t7 K7 Y% X5 {4 G5 n
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
5 Q# t* `# m2 m. P( S" K <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->
# o* B& K7 w7 L8 e9 m: s1 `& { <!--- J( U" C) K* J& S2 X
<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>' [/ p! ]- d2 Y/ [. D
<prop key="hibernate.show_sql">true</prop>7 u# u, m4 {- n+ g* D' O0 b
-->( G) T; K4 _ U C( f
<!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->! s; r& r$ y! M1 Q9 h" g
<prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
k& h7 l5 X" S% b <!-- Create/update the database tables automatically when the JVM starts up1 P3 @! N/ P* W0 I R4 T7 ~2 P- X
<prop key="hibernate.hbm2ddl.auto">update</prop> -->! z q$ y7 J8 v" j" x4 d
<!-- Turn batching off for better error messages under PostgreSQL -->
" m8 b: B0 u z5 \! n <prop key="hibernate.jdbc.batch_size">25</prop>* G- Y5 s* T' r
<!--
8 y- ]7 K5 V6 \& Z/ m <prop key="hibernate.connection.pool_size">10</prop>( o( h+ U& n6 h; ~, V
-->
0 O. E" j; F d9 Q+ [) Q </props>
5 L' S# Z; ?8 F) i+ [4 k </property>/ I0 b+ `) x. w% j$ m. ?
</bean> 如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行 2.首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下: : ^ Z/ M' c6 v( W) i( w
<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
4 t6 `% f& B7 B( E; E! G# z its value in the running VM.
" l! N2 m9 y9 |% D The following properties are translated:" j _3 g" {9 x* D# N1 Y
user.home - User's home directory
, T6 s+ p1 m9 L" I( {* x- Q+ \ user.dir - User's current working directory' M- g. i! [6 H V* Q
java.io.tmpdir - Default temp file path -->/ U/ H0 S% o0 @$ W$ O2 ?* u. }
<!--<diskStore path="java.io.tmpdir"/>-->$ x" u( l0 P) B( Y: z
<diskStore path="/data/ehcache"/> <!--Default Cache configuration. These will applied to caches programmatically created through7 f* ]2 `7 f3 B) @- i# d
the CacheManager. The following attributes are required: maxElementsInMemory - Sets the maximum number of objects that will be created in memory" a. e+ E/ O: P1 m* @/ U3 Q3 }
eternal - Sets whether elements are eternal. If eternal, timeouts are
& i6 R( f- y" }" E2 o0 W" Z- l ignored and the element is never expired.- v: K) i, p) T
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
$ l o7 U6 `1 ?8 Z has reached the maxInMemory limit. The following attributes are optional:
- x: ~7 X! F' }. t, b timeToIdleSeconds - Sets the time to idle for an element before it expires.2 c# J+ |) v( U' [. D( K* Q
i.e. The maximum amount of time between accesses before an
) i# c, j9 X% l% X3 ]/ e element expires Is only used if the element is not eternal.6 o( E/ P& [" M" N1 D9 A
Optional attribute. A value of 0 means that an Element can idle9 I, o* F- p, F1 g3 U+ v, a
for infinity.The default value is 0.
8 o5 H& p0 V6 t, z- L- | timeToLiveSeconds - Sets the time to live for an element before it expires.
) k+ v% G: Y7 o* j( }8 I6 S i.e. The maximum time between creation time and when an element0 _$ ]8 w8 a7 A' D, _* f
expires. Is only used if the element is not eternal.
1 H: g g+ k* j6 t" F) N Optional attribute. A value of 0 means that and Element can live
( M7 ^+ Z% z# l" m, b for infinity.; g! X! N0 f! x9 R
The default value is 0./ s# ~: T3 {" X" Z7 y; M, I F# w
diskPersistent - Whether the disk store persists between restarts of the Virtual
+ n/ q7 j" o( z0 ` Machine.
/ J, ~+ E/ o R& { The default value is false.- V" c" M3 w: X1 F0 \6 n# y0 I' z
diskExpiryThreadIntervalSeconds - The number of seconds between runs of the disk expiry thread.
2 e) i0 {3 v/ I) b4 K The default value is 120 seconds.' {: E5 @% B$ p+ C% u- g9 s, T% h
--> <defaultCache7 r8 ^# ^" [- C$ C% ^' q4 @9 |
maxElementsInMemory="10000"
% R5 ?4 u' `& [ eternal="false"
, ?& F- \! Y( X$ q. f" _ overflowToDisk="true"
$ e# j$ J. z$ K timeToIdleSeconds="120"1 o7 w: n2 [3 Z$ ~- R
timeToLiveSeconds="120"
2 K3 C1 v3 w( g; Z$ R$ l3 z diskPersistent="false"; T) Q3 |! C, e( ~) Y! p& r
diskExpiryThreadIntervalSeconds="120"/> I( W* h9 h8 t+ \& T
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"
9 ]6 t( B* }! w5 F& F8 {! U% M eternal="true" overflowToDisk="true"/>
3 V, V4 R/ V3 z+ Z& V <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"3 s9 O! T$ ? D+ J% @# z+ x
timeToLiveSeconds="120" overflowToDisk="true"/>
3 R: ~. K6 ?0 D& B) U <cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
0 J. G) c6 q/ _' k ], Q& | "600" timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>
3 ]" Q7 m4 }. L' f" H" N+ U <cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"0 O) s6 v' s ]: Q5 E0 B" w1 D
eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
3 O9 X1 T- ~* H5 q$ O diskPersistent="false"/>& }- O; G9 U6 p- b6 F
<cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"2 U' A G1 v, f! C& G6 Z/ y7 ^ O
timeToLiveSeconds="600"
% e3 U/ b7 Y' k- {( \& g, w/ I& L+ b# d overflowToDisk="false" diskPersistent="false"/>
, y# }3 ]0 D1 b <cache name="com.ouou.model.Videos" maxElementsInMemory="10000" eternal="false") E8 D3 `! a& e
overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
- R) Q& z. E: S% c! z9 @7 v. P; T( K <cache name="com.ouou.model.Tags" maxElementsInMemory="10000" eternal="false"
3 h. b/ Q6 V# q* j6 X6 P overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
- \: v( }5 ^. s1 I3 q( h' R+ s: X</ehcache> 以com.ouou.model.Videos为例子
8 k: S4 M. F- w在Videos.hbm.xml中配置:, N7 B' e" t9 t. u( {7 L/ `! Z& D+ ~
<class name="Videos" table="TEST" lazy="false">: [3 f% i: B5 i
<cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。 M7 y5 h( {! f4 d, e5 X, z2 E' Y
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,
0 l9 R! V8 S7 W t如果不存在与类名匹配的cache名称,则用defaultCache。
, u( O# S+ ^4 T+ g! E2 t7 _- Z5 @如果Videos包含set集合,则需要另行指定其cache+ V1 K$ p6 z8 f0 ^- {1 D
例如Videos包含Tags集合,则需要
4 i' z* |. p* W- K" {3 N4 p添加如下配置到ehcache.xml中# u: I$ a* a) ? X
<cache name="com.ouou.model.Tags"( M) r$ z5 d L( i6 v/ _" M
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
$ R) Y! d; Y# G+ n) N; K timeToLiveSeconds="120" overflowToDisk="false" />. A- ~6 c% _ v$ O# @, R: @
另,针对查询缓存的配置如下:& D8 M+ f" E( S4 w
<cache name="org.hibernate.cache.UpdateTimestampsCache"
1 V6 \3 B2 X: @ d! ] maxElementsInMemory="5000"
1 A/ L4 |' k3 \& s N* ` O eternal="true"0 ?" w1 s" R9 u1 s2 F6 T) x
overflowToDisk="true"/>
% O* t) `/ @0 E; u& ?( X' A<cache name="org.hibernate.cache.StandardQueryCache"
' G9 K& J/ c' } maxElementsInMemory="10000"
# e' J+ a. c. H2 D/ G( X4 U3 S. ~ eternal="false"
' t" \% }* j) f timeToLiveSeconds="120"' Q0 |# \, c0 Y8 e4 g9 ]
overflowToDisk="true"/> 3、 选择缓存策略依据: <cache usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>
' v/ g; E4 y% S# Y- U" E( Mehcache不支持transactional,其他三种可以支持。2 C7 O! F: Y' N% X# P# }3 C
read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,; w) B0 D7 F5 U, F0 e. N
但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。8 b. U( H& \$ F' \' ~4 ]7 C! j
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level
5 I7 z5 ^( q7 p* y2 E(序列化事务隔离级别) {8 Q6 E2 d+ c0 r! r
nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,- q* l U. _( Z& [
那么比较适合使用非严格读/写缓存策略。 4、 调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。 5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (
/ n' e; F4 \, D2 p) t) forg.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。
, E7 i. p" D9 e. z" \1 @+ t, q; b请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。" R3 H, G9 o* x: \. e
需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。 ' v+ @0 }& ^! ]5 k: I
Ehcache的配置说明 ! O" g% `! {) u; }% F7 D
<ehcache>
# c( G( L/ y; ?$ b: F<!--
' q4 x" J' ~# ]& b0 O: d5 \5 N磁盘存储配置:
6 Y& x# X- z! [& T2 Q4 C+ a用来指定缓存在磁盘上的存储位置。可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)8 F) p: A9 n4 C. w
-->
8 z, H* y, ~, u) V: A<diskStore path = "/var/apps/cache/" />, I1 p, n* ^8 b. \) y2 q. t5 r
<!--: b$ z! f% j2 T, y
指定CacheManagerEventListenerFactory,这个对象在缓存添加的时候会得到相应的通知
) c7 } S Y8 F# B- i9 |CacheManagerEventListenerFactory的属性8 t2 k6 Z( ? j: D" f! y9 n
*class - CacheManagerEventListenerFactory的一个实现类/ A/ P; E" x2 g/ q6 J
*properties - CacheManagerEventListenerFactory的属性值,以逗号(,)分割多个属性3 B$ I1 b3 j5 b) E
如果没有实现类被指定,则系统不创建CacheManager的监听器,没有默认值& e2 }2 C" J! Z
-->
4 w! U& u3 t7 j3 I/ x& ]* _! P) ?<cacheManagerEventListenerFactory class="" properties="" />
8 q. W0 `9 E! ~* h<!--
$ H; X: F! w: ?" B2 S在进行分布式缓存的应用时候需要指定CacheManagerPeerProviderFactory,
. h7 t0 W& _0 X; e& O- l" \, j用来生成CacheManagerPeerProvider的实例,以便和集群中的其他CacheManager通信。+ P( L+ \) M7 A! v
*class -CacheManagerPeerProviderFactory的一个实现类
0 {8 l+ Y$ ^2 ^+ T5 p*properties - CacheManagerPeerProviderFactory的属性值,以逗号(,)分割多个属性# p; W; |# }. _! ~5 }
Ehcache内建了2种基于RMI分布系统的通信策略:% z6 D) s; v: |0 _) Z- M# e
*automatic - 使用多播组。在一个节点加入或者推出集群的时候自动感应
, ~8 p7 M- z+ Z( ^6 v( y n*manual - 硬编码方式 目前的awf中不考虑分布缓存
" U7 p4 P% o9 Y-->) N4 r; l2 V: |- P" d
<cacheManagerPeerListenerFactory class="" properties="" />5 D( J P! y/ w% T) V' U* o
<!--
# m J" K8 w. @6 R% h缓存配置。
3 z7 b2 k, s4 c以下属性是必须的:+ Z# x' f+ j; {7 ^
name - cache的标识符,在一个CacheManager中必须唯一3 J- E, A- Q8 H3 D. _4 `5 ~) ~
maxElementsInMemory - 在内存中缓存的element的最大数目" I& y$ m& {# ^1 G
maxElementsOnDisk - 在磁盘上缓存的element的最大数目
. g1 U7 z) s( Z# ~% o9 Jeternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
! s- ? l2 k; g8 b4 d, ^- }/ NoverflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
, d9 W6 M: S* J+ h$ f以下属性是可选的:9 D8 j/ j( E& t
timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.
$ @* j" q# [% w& g (如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)
; L6 R/ C/ @$ x- M KtimeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期' i* r5 t; l: A. g+ {
(是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
1 `7 z$ [4 F! H6 ]% y0 xdiskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。
$ ? m8 J; h: Y2 M3 Y0 E (测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)* X( a& {2 z: `- ?9 }1 I
diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.) n6 q- ?6 K4 O
(测试一下0的时候会如何)5 ?4 ~1 l/ S) @; [7 t4 f Z
memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,
1 h8 a, h' o, e6 G 移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO 可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些
# U9 e4 S" [. X3 [2 m0 s5 b" t*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件0 P# R; M4 U: d# G+ ^, R9 j, T8 g
*bootstrapCacheLoaderFactory - 启动时加载缓存的element* t. k4 r* d! y) T
每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。9 n7 K$ I) e: V1 c8 a
Ehcache内建了基于RMI的实现 - RMICacheReplicatorFactory% h: I, f( _2 U4 F" i3 s
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
& A/ i ^8 \4 s" b properties="replicateAsynchronouly=true,
k: e! t0 v2 a! P: W replicatePuts=true,5 `# K1 k, z, A, q
replicateUpdates=true,
% o: h& t( t' Q7 d" h% J replicateUpdateViaCopy=true,
# l$ s. m( T3 x$ _& O replicateRemovals=true" /> -->
6 h: w8 G7 F1 S K5 k<cache .... />1 L. W# ]/ l* u2 g1 W
<!--
& T% M# h* B+ |4 x h7 {/ M默认的Cache配置。用来实现CacheManager.add(String cacheName)创建的缓存6 @8 j& g' a" u! U, k2 A
-->
' f1 _7 M8 Y8 q! j6 U( W1 g* m<defaultCache maxElementsInMemory="10000" eternal="false"
( Q4 Q0 N0 ~, i* H2 ` a timeToIdleSeconds="120" timeToLiveSeconds="120"
! }) a$ P) v! d V8 R9 [ overflowToDisk="true" maxElementsOnDisk="1000000"2 p; _+ Z/ m# A% J5 P- j
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
0 t) i& t* P3 Z1 k1 F memoryStoreEvictionPolicy="LRU"
- x9 `4 G3 b0 ^4 A' I0 r /> </ehcache> 8 R' M- P! ?3 A6 A
|