我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 盖世程序员 > SSH2搭建框架如何配置hibernate 缓存
总共48087条微博

动态微博

查看: 3802|回复: 0

SSH2搭建框架如何配置hibernate 缓存

[复制链接]

45

主题

0

听众

142

金钱

三袋弟子

该用户从未签到

跳转到指定楼层
楼主
发表于 2014-04-20 17:48:55 |只看该作者 |倒序浏览

1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:

<?xml version="1.0" encoding="UTF-8"?>2 U0 o' z# k; e& C; ]
<ehcache>
3 w5 d7 q8 b, M( Y' U <diskStore path="java.io.tmpdir"/>- x$ A3 t, Y. X$ v2 f* i  P
  <defaultCache% p5 Y; `9 z: P. Z+ H0 _. Q
   maxElementsInMemory="10000" <!-- 缓存最大数目 -->0 f  S3 {/ D; F8 \
   eternal="false" <!-- 缓存是否持久 -->

   overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时-->

   timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->* h8 A; t5 f5 }
   timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->6 s0 r7 v# j* B* }
   diskPersistent="false"
9 d2 W) f$ H+ q7 g% _  d   diskExpiryThreadIntervalSeconds= "120"/>% }6 q1 n# u$ N8 U# k3 j; A) [
</ehcache>

  2、在hibernate配置文件中设置:

<!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->0 d) g$ Z" E' S# F
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
+ H! S. h9 Q' D. I/ u: ]. e <!-- 是否使用查询缓存 -->

 <property name="hibernate.cache.use_query_cache">true</property>
# L/ j, E& R' Y- w" {3 C  如果使用spring调用Hibernate的sessionFactory的话,这样设置:
3 r4 E+ k5 h: k& p  <!--HibernateSession工厂管理 -->

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
* T8 B& v; f1 w2 x; \. S7 s   <property name="dataSource">* U5 w, e/ h6 v) J3 T2 i
    <ref bean="datasource" />. l+ |4 j, n2 E9 L
   </property># C! Y3 T6 Z% ^9 |
   <property name="hibernateProperties">
, l1 i3 Z: S6 a- U+ c1 n$ ~   <props>$ P/ f4 \7 _, S5 r/ O& G
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop># K& t& r0 k& s% N. C5 o9 p
     <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>. \/ a6 H  g  n! Y/ X% L" {
    <prop key="hibernate.show_sql">true</prop>
0 @0 F1 w; Q, i2 h  _0 q    <prop key="hibernate.cache.use_query_cache">true</prop>. d1 O' }+ \$ ~' [
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>7 {+ w$ T/ E  O3 |3 W% x1 s# g8 k
   </props>
# t( J* m; z  Q6 o0 Z </property>
; |" Z9 ]3 `* q" ]! S1 p1 x; u1 G <property name="mappingDirectoryLocations">/ C' m9 ?. k* V
  <list>" i- H7 M8 V  Q9 g- ~/ [/ P1 v" C
   <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>
, N; n( W! Q! ]  ?) z  </list>
/ F! W3 X  [; d% I5 ~. [5 \# q6 F/ _ </property>: Q2 @- ]+ K% `, K
</bean>

  说明一下:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行

  3、在Hbm文件中添加<cache usage="read-only"/>

# I' u* u+ |) @0 d7 f
  4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性

  5、实践出真知,给一段测试程序,如果成功的话第二次查询时不会读取数据库

package cn.rmic.hibernatesample;

import java.util.List;

import org.hibernate.CacheMode;
+ \- L+ ?) Y* |. `0 {7 f1 a- q) qimport org.hibernate.Criteria;2 I4 m7 m* J& I4 \% I
import org.hibernate.Query;1 @7 T- ?9 y9 z1 v  q
import org.hibernate.Session;

import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;
2 H6 s2 e$ B2 \' Bimport cn.rmic.manager.po.Resources;

public class testCacheSelectList ...{

 public static void main(String[] args) ...{
5 H7 O" e2 j% [$ I. P  Session s=HibernateSessionFactory.getSession();
) K. I" R& O* I$ h% }" h3 V/ L: R  Criteria c=s.createCriteria(Resources.class);/ Z% T% }0 \* {, s) ]
  c.setCacheable(true);
& s: A  N* m) R$ G  List l=c.list();

  Resources resources=(Resources)l.get(0);
) B. O/ ^1 V9 s# f; b  System.out.println("-1-"+resources.getName());
& V! b  M9 u/ B  {5 m  L  HibernateSessionFactory.closeSession();


2 d( }9 ^' s# i; ~  try ...{# A, W( X7 w( p7 C
   Thread.sleep(5000);
" n) q' Z  O% a. m8 V) J1 h9 D  z: O  } catch (InterruptedException e) ...{$ G/ [- n( H( |+ w, v! S
   // TODO Auto-generated catch block
- s( I- O/ Y& o) u: q- C0 v; q; v   e.printStackTrace();* E% y5 W. g+ s+ r/ [4 [" V
  }
2 h* {$ y: T' X' g" \+ W  s=HibernateSessionFactory.getSession();
& }- U3 Y1 o- u  c=s.createCriteria(Resources.class);4 I0 L0 g+ B3 x8 i' H/ R
  c.setCacheable(true);1 c; Q4 _* b1 ?2 {& \" V" R3 H6 u
  l=c.list();6 [" v  F2 h6 K7 |( h2 {
  resources=(Resources)l.get(0);
% {0 Q" h# h4 T3 A  System.out.println("-2-"+resources.getName());; P5 d3 z  p9 g! t& A1 p1 i
  HibernateSessionFactory.closeSession();  o( Y3 R4 Y5 j
 }
+ Z9 n8 d# R* D" Y}


  w2 u- a8 Q0 e; c  Q5 j/ G& u. _. N

1. 在Hibernate配置文件中设置:
! t+ b* n1 V- c0 J% |  B   

<!-- Hibernate SessionFactory -->& ]# [% S' B( C* @
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">9 _' V9 b/ v- D, [- A8 k: Z
        <property name="dataSource" ref="dataSource"/>
$ \: @: j5 `* J; O7 C0 h        <property name="mappingResources">/ L' G- V, z( a6 S
        <list>
% _- e; i' O, g# e            <value>com/ouou/model/Videos.hbm.xml</value>  
8 |4 i/ o& ~4 l0 \% S         </list>
/ @8 P9 o- _8 m/ i+ z/ @4 N2 C         </property>
; [9 [' J% ~6 w! a        <property name="hibernateProperties">
! k2 ]3 n, o  w2 M            <props>8 y9 {& `, g0 G: \9 c' ~/ O
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>( x7 R$ X9 `8 k3 ?
                <prop key="hibernate.current_session_context_class">thread</prop>0 `' _8 O: R+ m) h3 H
                <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>' G' H' e# X( b+ N' {* ?
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>( a) O, Y9 K0 T) ]+ H
                <!--add ehcache-->
% ]5 T3 M& H; g                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>2 t9 h/ E: z9 I% ~4 ]
                <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->, O3 ~* F0 g5 }/ I/ [7 w/ k
                <!--
  W! J! c- Z$ d: d+ g; |+ Q( N+ O                <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
( W* j# i- p7 L3 L) n& ?  H- b9 B+ H                <prop key="hibernate.show_sql">true</prop>
  T( j% f* S5 c0 g$ ?, o                -->
( `. ~7 v& o/ i                <!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
1 W$ ]% G9 O, Z( A                <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>" y( p2 Z4 [" z* R+ `. Z1 h! }* O
                <!-- Create/update the database tables automatically when the JVM starts up
* a  T1 H& v7 s" O: D; `* o                 <prop key="hibernate.hbm2ddl.auto">update</prop> -->
4 P3 Y6 \0 L: I- A                <!-- Turn batching off for better error messages under PostgreSQL -->
  O4 W: g9 h# C7 a* Q1 k' V                <prop key="hibernate.jdbc.batch_size">25</prop>3 u9 L2 h. V' B9 y
                <!--
7 r  Q/ }! w8 ], h& X. N                <prop key="hibernate.connection.pool_size">10</prop>" ]4 H! N; V% e4 f" D
                -->. c+ a" h( U- x: e6 Z
            </props>
# \; B: w+ J1 n+ n2 f" ^+ e0 i        </property>
& w, f2 i2 g& d$ \% S& R    </bean>

    如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置hibernate.cache.use_query_cache true 才行

2.首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:


5 V; x8 R* p* p <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. e( Y8 h/ Q* J3 ]
     its value in the running VM.; ]1 D( L% E- J3 c4 s: N
     The following properties are translated:
, b: T( {! \1 c     user.home - User's home directory
1 t8 p1 U" O9 F& {1 w     user.dir - User's current working directory" ?6 B( V! G' {. S* K. N1 K' P
     java.io.tmpdir - Default temp file path -->
$ h: G( |% z3 K! P) l     <!--<diskStore path="java.io.tmpdir"/>-->
  e. y3 v8 \9 w1 j2 s, L     <diskStore path="/data/ehcache"/>

    <!--Default Cache configuration. These will applied to caches programmatically created through
) ^, U& w; h; O+ c; M3 z9 j( z        the CacheManager.

        The following attributes are required:

        maxElementsInMemory            - Sets the maximum number of objects that will be created in memory
3 z- U4 k  f, }2 R9 A' d        eternal                                     - Sets whether elements are eternal. If eternal,  timeouts are- U4 E7 C+ T$ B
                                                            ignored and the element is never expired.) b3 u4 K& x% l, F" e9 v, B  U7 J* Q
        overflowToDisk                      - Sets whether elements can overflow to disk when the in-memory cache
( e0 R. e' O# p                                                        has reached the maxInMemory limit.

        The following attributes are optional:8 ~" b" j9 a# i, M' d" k
        timeToIdleSeconds            - Sets the time to idle for an element before it expires.( d$ d% m! @' @5 |+ S9 X" c
                                                        i.e. The maximum amount of time between accesses before an0 S2 M0 I! Y( N- s1 E; V& u, p
                                                        element expires Is only used if the element is not eternal.: n6 N3 s5 }4 Q9 @( j
                                                        Optional attribute. A value of 0 means that an Element can idle
2 t, E+ a' s; T: _) w                                                        for infinity.The default value is 0., D/ s; ]# E2 O$ M) [% ?& p
        timeToLiveSeconds              - Sets the time to live for an element before it expires.
4 ?) ^9 }3 D% X8 A. {                                                         i.e. The maximum time between creation time and when an element
/ ]9 V! ?. L9 x" }                                                         expires.  Is only used if the element is not eternal.
0 h/ v3 ]/ V/ E( J' e                                                         Optional attribute. A value of 0 means that and Element can live
) e1 l( `$ P( {* v                                                         for infinity.$ Q4 e$ A, M7 [$ m; Q
                                                        The default value is 0.
0 [  O+ P8 \1 v8 F8 }3 @# |; O1 Q        diskPersistent                           - Whether the disk store persists between restarts of the Virtual1 S0 w( s2 T- I( ]$ E+ B: [
                                                             Machine.
, Y! z4 j' n5 s8 V                                                         The default value is false.
8 Z- X  c; t+ W        diskExpiryThreadIntervalSeconds   - The number of seconds between runs of the disk expiry thread.
3 d! S0 t0 M/ b* ]) @                                                         The default value  is 120 seconds.7 F& E( n3 j* R: ~' g
        -->

    <defaultCache: G+ }8 p  p$ w$ {+ Z: g% c' B' J
        maxElementsInMemory="10000"9 y/ P: Y+ f* [9 u
        eternal="false"
  j6 a2 ?, a  Z1 v) f0 u        overflowToDisk="true"
3 n1 u' @8 b. y2 O        timeToIdleSeconds="120"
8 ?; r: l/ V. i; u" O8 [        timeToLiveSeconds="120"
6 L& e3 x2 y0 l7 P        diskPersistent="false"
% K$ V6 Q# I4 L        diskExpiryThreadIntervalSeconds="120"/>
" F7 M! ^9 y+ T- p' J4 u& c    <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"
/ v' K( x: C% i     eternal="true" overflowToDisk="true"/>
% u+ Y* o9 u& r2 h& F    <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"( {! X; n3 }& K: n* ~- ?$ x2 [" H
    timeToLiveSeconds="120" overflowToDisk="true"/>
% f) b$ v5 `3 G  F    <cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
0 |; m; ^4 h4 Q, q, [0 N, `        "600"    timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>! Y6 |9 H  E2 w( ]5 {" \
    <cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"' s& m/ V- p! N7 v
    eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
# D' L  n. P) N7 f1 }* P% H/ h    diskPersistent="false"/>+ P/ [' Y( a# a# r+ `5 c5 e
    <cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"
9 \5 g; C9 S3 C    timeToLiveSeconds="600": }( y. X. A. n" g$ f
    overflowToDisk="false" diskPersistent="false"/>( w6 s9 x2 }) J3 R6 N! ?6 O
    <cache name="com.ouou.model.Videos" maxElementsInMemory="10000"  eternal="false"
' l/ l! }' w2 d  f    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>0 N3 |2 m$ l9 t2 U
    <cache name="com.ouou.model.Tags" maxElementsInMemory="10000"  eternal="false"
5 Y9 V3 Z3 t7 @6 D    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>9 r* s# c9 F  |& j6 Y' v5 ?
</ehcache>

以com.ouou.model.Videos为例子0 ?7 `3 B, i# v( T' R2 P
在Videos.hbm.xml中配置:
9 t, X, P7 s8 S; g  F( O<class name="Videos" table="TEST" lazy="false">& z- ^+ p: c7 |) r  n) R8 H
  <cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。
) v. i' W& W! r. b3 a# |% K% ]hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,
) L7 p2 x  B  @2 B9 M9 Z2 V& n如果不存在与类名匹配的cache名称,则用defaultCache。0 ?; q. }3 B: n3 c
如果Videos包含set集合,则需要另行指定其cache
- Q9 m& l3 I. B  Y! ?# q例如Videos包含Tags集合,则需要7 y/ t2 n2 T# Z2 n& u% D5 {
添加如下配置到ehcache.xml中
! Q4 D* Z+ A0 z8 t! `( H7 M; ^8 _<cache name="com.ouou.model.Tags"- E4 @! Q6 I9 y; L
        maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"3 ~% k& t+ ]5 B1 r0 u" N; z6 V9 B7 C
        timeToLiveSeconds="120" overflowToDisk="false" />
" N7 g: r( G5 |$ d; d3 Z/ z另,针对查询缓存的配置如下:& O( A9 c0 x7 G4 U: Z
<cache name="org.hibernate.cache.UpdateTimestampsCache"
& f! z; e. G5 o( r) ]2 v        maxElementsInMemory="5000"
% ~" X. M: w  m0 K3 Z        eternal="true"# o, ^. u7 H  c4 ]/ M
        overflowToDisk="true"/>5 X; _) Y4 N- y9 J/ w' `/ K
<cache name="org.hibernate.cache.StandardQueryCache"
3 o$ j, p' M& W+ K        maxElementsInMemory="10000"
7 f& l, x! H/ h' T! x$ l5 N        eternal="false"; |4 ]+ b, [- M; `
        timeToLiveSeconds="120"
3 r6 L' n- o- a7 J/ V        overflowToDisk="true"/>

3、 选择缓存策略依据:

<cache  usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>
! E( y( Q1 m( s7 f: ^; dehcache不支持transactional,其他三种可以支持。# o- b% A  @. b4 Q) V. ?
read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,
7 Z1 c. r4 S! T2 l$ C+ Q# _但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。; J) `; d) \/ J
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level6 V, J% i( q2 w0 x. i! g( S/ e
(序列化事务隔离级别)
8 j1 y- \% P+ n/ _& nnonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,
+ @" C3 N9 p+ \5 Z) s# ]) [那么比较适合使用非严格读/写缓存策略。

4、 调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。

5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (: i5 _9 W: `3 S
org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。
$ a' p3 X1 W* {, R请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。
9 W6 u+ j7 S) v8 W& H需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。

) [+ I+ Y, @" `5 o6 I

Ehcache的配置说明

* @  E! [" K/ b. {. K4 t+ O
<ehcache>2 [9 X$ d$ J* y9 R
<!--
+ ^5 {$ S* g$ Y. [/ ]$ b: U磁盘存储配置:
0 I: v6 K$ d9 e) k' W/ H7 k用来指定缓存在磁盘上的存储位置。可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)
: ^- F0 m5 e# b-->7 c; t% ?0 H" d9 y/ c  v+ b
<diskStore path = "/var/apps/cache/" />
5 e6 u+ w7 S8 \<!--$ u4 L7 D0 l7 q, @; E
指定CacheManagerEventListenerFactory,这个对象在缓存添加的时候会得到相应的通知
5 S* o) u1 X: W5 b- @% D8 KCacheManagerEventListenerFactory的属性, l: L# V+ r# l3 l4 T
*class - CacheManagerEventListenerFactory的一个实现类
2 I  U, p0 E9 ^$ s* l3 `*properties - CacheManagerEventListenerFactory的属性值,以逗号(,)分割多个属性
1 a8 M' E* k/ }5 D. r如果没有实现类被指定,则系统不创建CacheManager的监听器,没有默认值# R2 d# J4 d' q$ M3 p
-->8 m5 M; R5 {2 d2 D
<cacheManagerEventListenerFactory class="" properties="" />
/ v% P- i2 I) P2 M7 }3 E+ T, o4 l7 c8 n9 D<!--, Q) F0 y: ]  Q: ^. _
在进行分布式缓存的应用时候需要指定CacheManagerPeerProviderFactory,
& U! U+ M! p+ V! G# Q* e' ~: l用来生成CacheManagerPeerProvider的实例,以便和集群中的其他CacheManager通信。
$ u# |4 J% Z3 |  W2 W# K*class -CacheManagerPeerProviderFactory的一个实现类/ y: S# ?/ t2 ~' o5 `# J) `+ X
*properties - CacheManagerPeerProviderFactory的属性值,以逗号(,)分割多个属性, r+ J: P: u) E$ h9 o
Ehcache内建了2种基于RMI分布系统的通信策略:& K7 C; R9 w! Y- N$ b8 T
*automatic - 使用多播组。在一个节点加入或者推出集群的时候自动感应
) R4 m6 e8 H* x3 I*manual - 硬编码方式

目前的awf中不考虑分布缓存
8 }4 Y6 V% m4 y1 y. W$ X6 I-->4 a, C7 t( Y( J! i- P5 ^
<cacheManagerPeerListenerFactory class="" properties="" />
' W, l; h5 O& O- ]8 {: I; l$ W<!--- T; }) ~2 ?; G; V
缓存配置。$ B* N0 x4 ^7 d
以下属性是必须的:' F+ I0 {/ r/ |( Y* X% z! n
name - cache的标识符,在一个CacheManager中必须唯一
# F1 V9 \% @' C* M- g- f* u+ G7 i6 @maxElementsInMemory - 在内存中缓存的element的最大数目
4 m/ e: _; v5 R* T& t! w5 xmaxElementsOnDisk - 在磁盘上缓存的element的最大数目
# b- {: v" W$ m1 p! I. f" Z2 eeternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略3 u! M. A3 n) a) g* }
overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上( x) o2 }% J. k8 m" s" @
以下属性是可选的:
9 f5 r+ d6 j: o3 `: a$ TtimeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.
  t4 K! q4 R, ?# @; f! c& }% g        (如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)
3 P. q) e, m  @  b7 l8 DtimeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期
  L* s& g3 |# I: |3 E9 d: O0 R        (是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
+ L4 p% W# S2 P# cdiskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。
2 @" C8 a: l$ `7 ^: J0 j- L        (测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)
# U0 B  }& w1 Z+ |diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.; Z4 |$ {; i1 A! t
        (测试一下0的时候会如何)
" N5 n; D. ~+ b- N7 ?memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,9 E3 Q% [; ~& e4 P+ g$ ^4 p
        移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO

可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些# A% r5 E5 V/ ^1 t$ t1 o
*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件
4 O5 j8 L" ~- W3 u/ K*bootstrapCacheLoaderFactory - 启动时加载缓存的element" y" v4 ]# f4 i
每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。
5 L, R) J+ ?# \+ ?5 @Ehcache内建了基于RMI的实现 - RMICacheReplicatorFactory# m6 m0 u4 o0 M% h% e
    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
  A# [- Y. F" e9 C& R        properties="replicateAsynchronouly=true,' P- Q; n) ^4 d5 |# g) K( J
        replicatePuts=true,- s8 f' k6 Q. b7 X9 D
        replicateUpdates=true,5 \3 i5 n+ c5 @7 ]+ W1 \" k/ i3 L# t
        replicateUpdateViaCopy=true,/ z* t' [- G' ?! L
        replicateRemovals=true" />

-->) L% S1 Q+ p7 ?: |& X
<cache .... />
4 g6 f6 j3 ]0 [+ E<!--6 r, q$ F1 [; Y/ G' G6 _9 {
默认的Cache配置。用来实现CacheManager.add(String cacheName)创建的缓存
: X! C$ o. v+ F1 m: H-->. x. K  Z0 }) L7 T
<defaultCache maxElementsInMemory="10000" eternal="false"+ N# u! ^  e5 B2 J+ Q+ z
        timeToIdleSeconds="120" timeToLiveSeconds="120"
' _0 b0 Q6 e' ^; Y0 [  j        overflowToDisk="true" maxElementsOnDisk="1000000"* c3 d- R5 C6 ^9 @; T6 y2 A
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
8 e- c' {3 ^: N- D        memoryStoreEvictionPolicy="LRU"
8 s4 w; a, I1 j8 U        />

</ehcache>


5 q) ^5 N0 ~' h, y

科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与科帮网享有帖子相关版权
3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网的同意
4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
7、科帮网管理员和版主有权不事先通知发贴者而删除本文


JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

上班中。。。
快速回复
您需要登录后才可以回帖 登录 | 立即注册

   

关闭

站长推荐上一条 /1 下一条

发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
快速回复 返回顶部 返回列表