|
1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下: <?xml version="1.0" encoding="UTF-8"?>
8 o8 p/ k. T+ T% z8 y+ j- Q" t<ehcache>
8 d. r# E" \3 P7 n K& f <diskStore path="java.io.tmpdir"/>
0 l. P8 b- x2 U# e- ~ <defaultCache* x. c6 s) h9 i6 |) n- L$ Z- |9 l
maxElementsInMemory="10000" <!-- 缓存最大数目 -->
5 t6 f6 v, T7 v9 C+ ~9 a eternal="false" <!-- 缓存是否持久 --> overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时--> timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->
/ O% d: ^! @ ]' h) f timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->/ v* R5 d( r. p2 E9 e4 Q) x. d8 a8 n
diskPersistent="false"
& s4 @# M7 g6 V' w( ^4 |( l diskExpiryThreadIntervalSeconds= "120"/>: u7 A3 }; X! `- H @; W
</ehcache> 2、在hibernate配置文件中设置: <!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->* ?% a* s: f% g0 `" U
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>1 A$ A: m- Z5 l4 W6 E- k# h% e3 v! K
<!-- 是否使用查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property>5 R1 g8 x% _ f2 `7 h- u
如果使用spring调用Hibernate的sessionFactory的话,这样设置:
! C3 z. i2 Y: K; d; }- c- | <!--HibernateSession工厂管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">* `! y& L" f2 t5 J+ U3 m# z9 Z
<property name="dataSource">5 ~& C3 K" o; m; P+ w7 q+ d
<ref bean="datasource" />. _9 c4 \9 x: A/ }- U1 x
</property>
% i% I3 R1 A3 z8 K3 B <property name="hibernateProperties">. [8 r. C# d2 v
<props>
( t. F6 O5 T% g' `+ F, i <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
7 r% V G) C9 o0 d0 z j0 O8 f <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
1 v1 C8 T& ?& S! ?- G& J8 b <prop key="hibernate.show_sql">true</prop>
2 I, e+ i6 r1 K$ [+ f, J$ L9 x <prop key="hibernate.cache.use_query_cache">true</prop>
P$ J4 [) t. f: G) f: x <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>( W) z4 ~$ a6 r% w
</props>7 N, g; P4 f/ V* ]8 m5 H
</property>
8 b# a% q R- r O" b <property name="mappingDirectoryLocations">
% O" z h" C3 p1 _2 ?* S: y5 j <list>
" t5 ]7 B/ w1 C8 B <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>, `0 g) T" d' q2 f3 d
</list>, T0 I4 P$ Q% [- T2 A0 j
</property>3 O* v% m" Y0 |
</bean> 说明一下:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行 3、在Hbm文件中添加<cache usage="read-only"/>
" K* E6 E) J0 L7 d 4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性
5、实践出真知,给一段测试程序,如果成功的话第二次查询时不会读取数据库 package cn.rmic.hibernatesample; import java.util.List; import org.hibernate.CacheMode;
/ [ ?; p5 P8 c* h Y$ eimport org.hibernate.Criteria;
/ b6 u) Y, A8 ^6 \import org.hibernate.Query;% q4 v0 l( x8 N& q6 e
import org.hibernate.Session; import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;8 ]4 n8 p7 t+ x! f
import cn.rmic.manager.po.Resources; public class testCacheSelectList ...{ public static void main(String[] args) ...{- ]8 n1 C! V" [' g+ ^, _
Session s=HibernateSessionFactory.getSession();; r6 b9 w* }9 y6 R, I8 t
Criteria c=s.createCriteria(Resources.class);
7 X, { o( `+ h# P c; T( r c.setCacheable(true);
, ]2 E6 ~% t7 X7 q List l=c.list(); Resources resources=(Resources)l.get(0);
1 A1 h* P6 m/ {0 l9 V" E3 Y System.out.println("-1-"+resources.getName());7 R; ]/ I* n9 {2 y4 F) V
HibernateSessionFactory.closeSession(); ) t' j0 q9 R9 y% `
try ...{
/ a& c- t( @% J" j x8 Z& g- } Thread.sleep(5000);: M {# [8 w1 f
} catch (InterruptedException e) ...{7 H+ W% Z% w& @
// TODO Auto-generated catch block! W$ x [$ P/ `6 s, V7 A0 c9 _
e.printStackTrace();. I2 ~3 d- \9 ^: s; G
}
2 ?+ d4 N1 z7 U: ~' v6 [: _; i( U6 ` s=HibernateSessionFactory.getSession();
, s' x$ f: u! S) i+ h5 B c=s.createCriteria(Resources.class);
6 o8 m. o! y. y7 C' y/ O# R c.setCacheable(true);
2 b$ t/ T; I# I& m9 u l=c.list();* b6 B& d0 K0 r
resources=(Resources)l.get(0);; o& L7 }4 d) y+ T. x h2 d
System.out.println("-2-"+resources.getName());4 r9 A" g, N# `; v% F/ ?7 |+ ^
HibernateSessionFactory.closeSession();( C$ x: I3 M& `0 W o$ a3 C" _
}1 T0 W/ s- {- ~7 e- R9 t
} 3 {: }( B! ^& K. B1 K
1. 在Hibernate配置文件中设置:+ I2 U1 |# N8 l$ B" h. B2 T6 @
<!-- Hibernate SessionFactory -->
5 E2 E7 e( j* s! P2 V2 F( |5 d7 _# I <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
6 {. D' ]7 }+ J& I& |$ r4 R <property name="dataSource" ref="dataSource"/>
0 i5 L, [- _. f0 O" }+ O <property name="mappingResources">
+ X7 \/ C. W+ i0 o1 ^/ i, O <list>
3 g; o: f, _9 R5 n& S <value>com/ouou/model/Videos.hbm.xml</value>
& \8 Z4 q9 z. y+ B: {- B' H& J </list>" i! {( y$ z3 L% e; t! C# p
</property>9 ~* @/ x: ]1 {9 Q' j5 G4 ~
<property name="hibernateProperties">
' Q0 \3 P; z' U! ~' S5 f <props>3 x `* ?) X' y5 `6 }- `: J
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>; Y; t7 h7 J$ D
<prop key="hibernate.current_session_context_class">thread</prop>
; G& b8 [6 P. W9 |3 K4 n <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>; e n; @# C4 k/ ]" c
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>- d0 x$ a! o; j2 ^: y: h* R. M
<!--add ehcache-->
+ P( R/ ], P3 {% H$ F7 O# F) a% O! [8 V <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
) c/ D1 B7 ]* M0 P1 r: i$ }% i <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->
" c1 m( c) x, l" C) y9 t <!--
$ B. a" r6 \7 J0 z/ Y4 ]- X# K <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
& Z) e: v5 i$ Q; I8 Q <prop key="hibernate.show_sql">true</prop>1 {- p6 j9 Q# ?& T) @
-->
5 J0 H7 ~5 C) Q( s% e <!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
, U. h3 v. ?! I2 c l4 G! M <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>! q5 \& }/ \# a1 u, v( Q
<!-- Create/update the database tables automatically when the JVM starts up; b# U' j9 `9 q8 O2 V2 ]: {- ]7 l
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
" Y z3 [; n& V9 K/ V8 R; M, ` <!-- Turn batching off for better error messages under PostgreSQL -->0 }6 Y C2 J7 o. F7 x ]
<prop key="hibernate.jdbc.batch_size">25</prop>9 |% g* S2 ?1 B( E7 v
<!--
$ b1 Z/ X4 p& n5 v Z <prop key="hibernate.connection.pool_size">10</prop>1 k" _0 g& a; U
-->: K7 @' k5 y6 C3 {8 A' U: \9 o0 J
</props>
: O7 z( R- [% D6 ] </property>! c0 X& q" `, S* [9 ^ Q
</bean> 如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行 2.首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下: ( M$ o7 |# H8 a7 N6 N# e
<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$ `. v0 c5 M }1 R! K0 P
its value in the running VM.
5 s6 ^) J8 R4 l$ H. v% M, j The following properties are translated:1 M% q9 P! _ p; x# W: Q6 M) Z J
user.home - User's home directory
( G, p9 `' m1 a+ L user.dir - User's current working directory
' E$ W+ @( Y/ Q, h% u! A% I java.io.tmpdir - Default temp file path -->1 |) ~8 F; i, c7 I. I- w/ [
<!--<diskStore path="java.io.tmpdir"/>-->
# P' o0 ?) B( R4 X n <diskStore path="/data/ehcache"/> <!--Default Cache configuration. These will applied to caches programmatically created through1 J/ R6 A" ~: {6 r' a$ y9 V
the CacheManager. The following attributes are required: maxElementsInMemory - Sets the maximum number of objects that will be created in memory8 K; n' k& }' r5 }) d
eternal - Sets whether elements are eternal. If eternal, timeouts are
! V* N8 W3 k- z o! e$ I% v" x ignored and the element is never expired.5 _' a- U; b+ R: \" U6 _
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
# f e$ L/ O! I4 Y/ {* q) g! K has reached the maxInMemory limit. The following attributes are optional:
; R) O$ X2 |+ k0 X timeToIdleSeconds - Sets the time to idle for an element before it expires.0 N' R- o! P2 F; Y, @6 b( g5 w! V9 j
i.e. The maximum amount of time between accesses before an
- ~! L& U6 S1 m6 c7 |& } element expires Is only used if the element is not eternal./ W& M. a$ B+ l) x+ Z
Optional attribute. A value of 0 means that an Element can idle
# W* `; I! K' n; H8 a# F: @6 q for infinity.The default value is 0.
: s+ [6 ~' O2 D. j" I timeToLiveSeconds - Sets the time to live for an element before it expires.
% P8 S) q( J" Q$ ] i.e. The maximum time between creation time and when an element1 g0 Q6 s/ q: i/ H i5 K
expires. Is only used if the element is not eternal.
) S. g B S5 X" G# [" H Optional attribute. A value of 0 means that and Element can live
. ?; Q. d5 {* H' N for infinity.: R" O) p1 V! P9 n7 r2 z. s7 ?6 W
The default value is 0.
8 l( G6 h, a+ ~" c& a diskPersistent - Whether the disk store persists between restarts of the Virtual
8 }( }4 O8 h. P+ ~% T) p% P Machine.
9 _: F! n3 _ c0 S The default value is false.
/ K% Z% l1 [3 g4 d4 H1 C$ K# | diskExpiryThreadIntervalSeconds - The number of seconds between runs of the disk expiry thread.
6 G) {8 N0 z$ _) m The default value is 120 seconds.
$ F- S v2 D8 q( `- f) o --> <defaultCache% @1 ]; W! Z w2 Y, V
maxElementsInMemory="10000"
) G9 L% @9 d4 E+ L eternal="false"
% Q2 O! }! i0 I0 R- o overflowToDisk="true": E1 M: R+ }3 t) j" q. f" t
timeToIdleSeconds="120"0 b& K* a" K- a; M
timeToLiveSeconds="120", Z7 D1 h" ~; O' W1 k
diskPersistent="false"" A8 [ w8 h6 f6 l
diskExpiryThreadIntervalSeconds="120"/>4 u: r7 M3 b: D) b
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"8 l9 C# S* k1 T7 n0 H
eternal="true" overflowToDisk="true"/>, `( d5 V8 o. P$ _% s
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"
" n+ G* C9 l2 b4 E, z, C timeToLiveSeconds="120" overflowToDisk="true"/>, J; y7 v0 `2 H
<cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
& i4 x& ~( i6 U' Y "600" timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>
& d& x% I) Z+ s- Z4 A ~ <cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"4 D! n/ P ^0 M2 F0 X, T/ r$ a
eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
8 l) ?3 N, _' G* {+ _; R diskPersistent="false"/>- O; P$ n$ v. A" ~0 I+ f# [
<cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"5 d7 s3 g2 ~8 m, O m. I
timeToLiveSeconds="600"* o- q' i( h: P" k2 N3 S9 |
overflowToDisk="false" diskPersistent="false"/>
( Z& ^* U& }4 O; [% k <cache name="com.ouou.model.Videos" maxElementsInMemory="10000" eternal="false"
# O( O% P+ K4 s8 ` F8 d+ Q/ G- m ~ overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>( r! _- d# _& s
<cache name="com.ouou.model.Tags" maxElementsInMemory="10000" eternal="false"- d& c6 c1 D3 B* F! T* E
overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>' {4 P: a/ e6 t- z) J# R
</ehcache> 以com.ouou.model.Videos为例子
. k2 w' I% y! n( P" C i$ c" {$ G在Videos.hbm.xml中配置:
0 N, ]0 c4 D- Y" g4 X<class name="Videos" table="TEST" lazy="false">
, A7 p/ S) }& y. |4 _* R <cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。5 I* x3 N5 A; V8 V A4 q
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,
/ r5 h& w% H1 e/ T如果不存在与类名匹配的cache名称,则用defaultCache。, Q- S% }. R% x" E2 f
如果Videos包含set集合,则需要另行指定其cache$ K( [: ^; K& q5 Z) _
例如Videos包含Tags集合,则需要
- N! f) M& U1 s7 K% |3 |添加如下配置到ehcache.xml中
_5 S2 y% ^$ |" x& K<cache name="com.ouou.model.Tags"( z4 A0 C1 l3 D0 E
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
, U8 Z. L+ |" w3 _; T0 f timeToLiveSeconds="120" overflowToDisk="false" /># ]- t' G& L0 ]; E( U
另,针对查询缓存的配置如下:
+ {; m' f8 {& O4 {$ i R<cache name="org.hibernate.cache.UpdateTimestampsCache"
$ u) _5 S/ Z( P# v# m' Q5 F* n maxElementsInMemory="5000"
: q/ F3 K; C2 n; Q" d5 [ eternal="true"
+ y% y* z/ R. `( x$ f+ Z4 g overflowToDisk="true"/>
' q2 U! _" _) [7 D/ {<cache name="org.hibernate.cache.StandardQueryCache" h }4 {0 E# M% A' g/ E$ E
maxElementsInMemory="10000"; Z% c2 a0 e# n- F1 P
eternal="false"2 T. h0 z" @9 l0 [& i8 \4 B
timeToLiveSeconds="120"
4 p# E6 x) T. s3 K# ` overflowToDisk="true"/> 3、 选择缓存策略依据: <cache usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>! g0 U; h; A5 E, P. Q4 s* W2 L% h/ i8 v
ehcache不支持transactional,其他三种可以支持。2 y, X( ~. D& ^7 @* Q: G$ g
read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,- Z8 i: Y: i, O& K6 U
但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。5 T' ?5 c& U0 e! a
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level
$ L4 L/ I1 d- u) V(序列化事务隔离级别)5 |' k2 h8 E7 `6 [% i8 f3 ~' L1 I6 ~
nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,
. T1 r% ]; Q" \# W' a& F- \% X4 ]那么比较适合使用非严格读/写缓存策略。 4、 调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。 5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (9 a/ R M; h2 ~8 g4 r2 j; p
org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。- [: k8 g0 |- y3 l0 Z
请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。
, e, ]) h, V# s6 a需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。 , d3 m' _6 \3 ]+ d: a( i& f0 ]
Ehcache的配置说明
, K/ m% F g, \% @ d l& ~" q; I<ehcache>
/ `8 N6 e7 s) A( z8 J1 M<!--$ t: Y+ s9 y$ E
磁盘存储配置:. A# R2 ^: d- R5 G, T0 T
用来指定缓存在磁盘上的存储位置。可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)
/ g. T8 l# V) y-->
g* M) C5 z/ R9 F6 s' j0 ]<diskStore path = "/var/apps/cache/" />5 S' L0 G) F/ J% R* `
<!--
# v/ U" j! |( Y6 I- b0 q3 `4 O指定CacheManagerEventListenerFactory,这个对象在缓存添加的时候会得到相应的通知
0 w7 T! d4 k4 z; p. b% q# J% H- X8 {CacheManagerEventListenerFactory的属性
1 i% j2 L5 y" q2 V4 Z9 ~: b) Y*class - CacheManagerEventListenerFactory的一个实现类
8 Z9 g! ], _8 F% C( r*properties - CacheManagerEventListenerFactory的属性值,以逗号(,)分割多个属性
" \) {( [- d6 i如果没有实现类被指定,则系统不创建CacheManager的监听器,没有默认值: X I0 C0 p- h" D5 |
-->
8 \/ M- t/ T, p: }0 u<cacheManagerEventListenerFactory class="" properties="" />
2 i/ V7 I8 r [3 ?5 b/ u<!--7 S% \- K/ w% Y9 F
在进行分布式缓存的应用时候需要指定CacheManagerPeerProviderFactory,0 n0 U, ^4 K' r8 c' c+ Q9 `# c1 M" h
用来生成CacheManagerPeerProvider的实例,以便和集群中的其他CacheManager通信。: q9 s! H2 W/ |1 c" p% g# e+ K
*class -CacheManagerPeerProviderFactory的一个实现类
4 y* @; ]; |: I, z7 F4 p, u*properties - CacheManagerPeerProviderFactory的属性值,以逗号(,)分割多个属性
5 z Y$ u; O5 i& I6 g2 \Ehcache内建了2种基于RMI分布系统的通信策略:
- U3 u. {, H7 `: F* F*automatic - 使用多播组。在一个节点加入或者推出集群的时候自动感应 m/ @( b% S! C" R# u. T; @( X
*manual - 硬编码方式
目前的awf中不考虑分布缓存
. X5 H; b% N0 E) Z" K-->
1 v9 @4 k/ W* ?$ w7 @! l9 y9 W<cacheManagerPeerListenerFactory class="" properties="" />; Y& C1 W9 f4 }$ F; \( y
<!--. ~$ Z2 a& a- V3 {5 B
缓存配置。: x2 q4 M# E- h) D% E O
以下属性是必须的:5 ?- a( p- ?% D( }/ h7 m
name - cache的标识符,在一个CacheManager中必须唯一
- ]0 y: u+ s/ umaxElementsInMemory - 在内存中缓存的element的最大数目3 K8 j+ Q4 I! W2 R6 k& ]
maxElementsOnDisk - 在磁盘上缓存的element的最大数目. ]% U8 _6 o# H% X# l
eternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
4 u- L7 w2 z2 n: e7 Q' @overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
$ {# O9 ?6 t, h) s! P" d以下属性是可选的:
. w! P& e' S$ n) b% t: `timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.0 A0 u; H, @# Y3 l& n( }' ], I& N
(如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)0 ~5 L4 ]& ~# P8 Z
timeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期
2 e! o p2 ], n! s (是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
3 w" D/ E# ^/ ~% J8 QdiskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。& j4 i' W1 q2 p& k9 G$ I
(测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)
4 z$ i$ n n. Q: D/ ]' p& T7 }diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.
6 h1 k3 P, a0 H9 M4 C4 r (测试一下0的时候会如何)
) E) h( K% z B' {* E$ DmemoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,
0 ~3 y! B' b7 \' r- c# p8 P3 U 移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO 可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些6 p$ f3 A, \2 r$ e p& B9 D
*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件
0 f$ B; Q& b9 p' I*bootstrapCacheLoaderFactory - 启动时加载缓存的element
8 @" \0 |. r: E$ Q, R/ h每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。
, A2 G" v* T7 b. `. ~Ehcache内建了基于RMI的实现 - RMICacheReplicatorFactory
) W& l8 ^; P# `* A <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"0 x/ ^0 Z5 V" K; [+ Q
properties="replicateAsynchronouly=true,- S+ }2 h# a' Z- @4 z: c
replicatePuts=true,
. \% u* P7 k( d% J; M% B* F& F replicateUpdates=true,( A( h( d3 {, W M
replicateUpdateViaCopy=true,$ n. ~+ u/ i2 W0 o H$ s4 D7 q
replicateRemovals=true" /> -->5 R# ~. r: Z7 Q7 A. v- J: a' n
<cache .... />1 V8 [# X5 i) y q1 S# e4 e
<!--" Z% |! y: a8 x3 A* v" h
默认的Cache配置。用来实现CacheManager.add(String cacheName)创建的缓存4 [2 |& C9 C" ~& Y3 m
-->: f( x, X- e8 j; \
<defaultCache maxElementsInMemory="10000" eternal="false"/ Z" f) h: f; U/ R
timeToIdleSeconds="120" timeToLiveSeconds="120"
7 E3 j; c8 I8 o, B overflowToDisk="true" maxElementsOnDisk="1000000"
7 r: u+ E h2 Y1 F diskPersistent="false" diskExpiryThreadIntervalSeconds="120"% o7 \' E/ r$ T( A3 L, F* r
memoryStoreEvictionPolicy="LRU"
( L& k0 l' x# h1 w1 o /> </ehcache> ( o& u4 _! W I- k, {2 Z S0 |
|