我的日常

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

动态微博

查看: 3769|回复: 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"?>
. R( i, v& t: w: q) ^<ehcache>5 m& x* Q% V) b: n9 S. U- O4 q
 <diskStore path="java.io.tmpdir"/>
7 X% ~' T7 l: n6 _  <defaultCache! u6 L# c% ?, E. b
   maxElementsInMemory="10000" <!-- 缓存最大数目 -->- F) E9 a; i9 f# e# `+ ?! C! w& t
   eternal="false" <!-- 缓存是否持久 -->

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

   timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->! r$ l; U$ N7 y" W
   timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->
. b7 m8 ~/ f8 {" L   diskPersistent="false"
& q- A8 m8 {4 h( E6 Q( F& W9 T   diskExpiryThreadIntervalSeconds= "120"/>
, g3 [+ V8 y6 q</ehcache>

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

<!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 --># p. |( x& X6 D
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
7 W8 f8 Y( ~8 a3 O$ |6 P6 X <!-- 是否使用查询缓存 -->

 <property name="hibernate.cache.use_query_cache">true</property>
7 @, h3 ]4 r  a8 @& x+ L( [- ~4 s9 z  如果使用spring调用Hibernate的sessionFactory的话,这样设置:4 z8 _  B2 D+ T' D6 C! Y3 |& i
  <!--HibernateSession工厂管理 -->

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">% w7 E& T- r$ f0 {( w# K' y# @8 t
   <property name="dataSource">( `, ], o. K; u. Z% W0 G
    <ref bean="datasource" />3 u8 i# L9 R! o0 S; S4 K% m9 V
   </property>* J# c% s/ k, C, k
   <property name="hibernateProperties">
( e% I5 `1 R$ i9 a- r: U: C   <props>
% z( h2 Y2 ?1 l    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>& _2 x1 c9 C2 M9 l  v# j
     <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>; m# y$ m2 n* C' w
    <prop key="hibernate.show_sql">true</prop>9 B# R  s! f0 O2 W; z. y
    <prop key="hibernate.cache.use_query_cache">true</prop>+ V0 x3 \' o% r) U8 G0 l* I
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>' ~# N' E' g& C' T
   </props>+ }4 n; w9 A- b  E, C( ^$ G
 </property>  H0 u6 O0 J$ d, k/ I- q
 <property name="mappingDirectoryLocations">2 O5 F0 e/ h+ {9 q
  <list>0 D& g. _6 W7 V9 ]/ z* G+ q9 B
   <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>; G, _( x" J2 T% C
  </list>
& x3 i* @& A3 Y' _7 W( G- x( K </property>- Q: S' u1 _. Z6 e( Z! L8 F
</bean>

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

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

, t- l% V5 |4 P4 ], j3 r
  4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性

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

package cn.rmic.hibernatesample;

import java.util.List;

import org.hibernate.CacheMode;* s# P% ]9 W4 a: k# x+ C
import org.hibernate.Criteria;
" d6 w- e' i5 b  i: dimport org.hibernate.Query;$ o6 s+ m" t1 Y5 n1 H
import org.hibernate.Session;

import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;+ u' o  ^  {. M5 I- f
import cn.rmic.manager.po.Resources;

public class testCacheSelectList ...{

 public static void main(String[] args) ...{* P9 L* N- v' _9 i6 o- A
  Session s=HibernateSessionFactory.getSession();
7 o4 K9 |' M2 s) t* V  Criteria c=s.createCriteria(Resources.class);! N) d5 f8 h% W6 U3 R& M6 z/ u
  c.setCacheable(true);: C7 h" R" k) C' t0 s- Y
  List l=c.list();

  Resources resources=(Resources)l.get(0);
; |* b5 P; t3 G) g  System.out.println("-1-"+resources.getName());& S9 ?9 Q. n, O1 q
  HibernateSessionFactory.closeSession();


6 \# y  ~( U' b0 g  try ...{
) Q6 q8 v& ?% w/ y   Thread.sleep(5000);
; y' ]! X1 G) {  } catch (InterruptedException e) ...{2 H# v2 ~& g) E
   // TODO Auto-generated catch block
, I( k9 C0 J0 [7 w# W4 c) v   e.printStackTrace();$ p$ G* J+ X* ^3 j0 ^
  }& y; f% H8 @3 ^( D0 I9 t9 \
  s=HibernateSessionFactory.getSession();
4 p  f3 K1 r, k7 a$ Z. P( X" [  c=s.createCriteria(Resources.class);
7 G. ^- ^' c% h0 g  c.setCacheable(true);
9 ^$ q+ v. b. q- x9 M& J' Q  l=c.list();# j1 r4 ^2 x$ s) e- d* x6 o
  resources=(Resources)l.get(0);+ h2 i8 _' M. h1 J7 N+ t0 I
  System.out.println("-2-"+resources.getName());! U4 f0 j: A% ?4 h
  HibernateSessionFactory.closeSession();- w! A  [5 d! ~
 }0 q# j* |- m3 J, N$ @) v) p" g, M
}


, J' z+ K0 ]+ g/ g4 U0 w

1. 在Hibernate配置文件中设置:- L! |; K# Q1 {6 B2 A' O2 q7 m* L; G, R
   

<!-- Hibernate SessionFactory -->
( \5 |* T7 h7 |' Y3 P    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">; p! _8 F: c1 `6 ^
        <property name="dataSource" ref="dataSource"/>
9 h7 V# b# T9 k' H$ J- W5 i% ]        <property name="mappingResources">
3 K" B) c9 _- k        <list>
) P( y! L2 r, E, y            <value>com/ouou/model/Videos.hbm.xml</value>  
% H  [0 ]" L0 Q' |         </list>$ h, j1 z+ I0 [( z
         </property>
# l! ~- |, U! f1 [5 g& x& n% a        <property name="hibernateProperties">
, ?: R  A. O2 ~; W% g- g  `            <props>" e2 k# Q2 }9 P: H' q
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
6 V, [4 o8 [3 d                <prop key="hibernate.current_session_context_class">thread</prop>
* ]: r! W3 R/ G6 X7 U) \                <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>: ~+ e0 h% ?% Q; D
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>- B( A( `( x/ J0 i
                <!--add ehcache-->
& \; ^9 R5 P5 Q  {, `                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
/ \4 v7 P+ a3 B) E  q( }                <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->
. K9 l) x: d7 W3 x9 `5 A# O# L                <!--
8 G* k: E: D  p4 N1 \                <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
6 K& D$ w5 @6 c# S* n; c8 n                <prop key="hibernate.show_sql">true</prop>
. |/ V1 ]: z, Y3 Z1 Q8 l6 J, V                -->6 h/ G0 h/ W6 W  N1 w( _! j' l9 h
                <!--<prop key="hibernate.transaction.auto_close_session">true</prop>--># |- K1 d! C% ]  O) b
                <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>2 E* r3 M+ T3 p8 s3 s
                <!-- Create/update the database tables automatically when the JVM starts up* D0 F7 l7 G+ K7 n2 ?
                 <prop key="hibernate.hbm2ddl.auto">update</prop> -->: {' B3 I/ b9 i: A
                <!-- Turn batching off for better error messages under PostgreSQL -->8 ~7 f$ x4 M  b% i) N0 u4 [" a+ e
                <prop key="hibernate.jdbc.batch_size">25</prop>, b' _/ d# ^9 S- a& n3 s
                <!--
$ D3 q- T$ A" R' l+ m  t9 J9 E  v' |                <prop key="hibernate.connection.pool_size">10</prop>: n- l, ?1 q; ~% e4 C: S; P
                -->
) a) ]; j9 p; Z* V            </props>6 k9 H) K0 D* l4 t5 x; E* R
        </property>- P( c  w% S: {% u
    </bean>

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

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


4 `# E" l6 C6 R1 ]1 n9 I2 K9 Y/ T, J! N <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
, c6 o" j" A, T8 @" C, M8 M3 E; H     its value in the running VM.
3 j3 ?$ F% I6 f) d1 y. T- Z# u  y' }     The following properties are translated:
2 ^! L7 ^6 j2 }' P; J% k: |     user.home - User's home directory
8 G7 n0 f1 J2 h5 [( o2 c     user.dir - User's current working directory! r* M( N( x4 Y* z8 Y6 J0 ^9 F# t
     java.io.tmpdir - Default temp file path -->
6 X) Q  y" T/ X/ F     <!--<diskStore path="java.io.tmpdir"/>-->
0 h; D4 t- E0 w# y" b5 J2 d1 q     <diskStore path="/data/ehcache"/>

    <!--Default Cache configuration. These will applied to caches programmatically created through& C1 d1 i9 [' p
        the CacheManager.

        The following attributes are required:

        maxElementsInMemory            - Sets the maximum number of objects that will be created in memory6 W# R$ Q% M. H: T( T
        eternal                                     - Sets whether elements are eternal. If eternal,  timeouts are
" k. z! N; }2 f+ Z# W4 N# c* r                                                            ignored and the element is never expired.  ^4 y6 w% E0 o8 t
        overflowToDisk                      - Sets whether elements can overflow to disk when the in-memory cache
6 w! o; b# J- l: d+ ?: Z                                                        has reached the maxInMemory limit.

        The following attributes are optional:
) ?* @9 ?" s; {4 r) x, O0 y2 x        timeToIdleSeconds            - Sets the time to idle for an element before it expires.' g( l% P* C6 Z# e' T( C
                                                        i.e. The maximum amount of time between accesses before an1 O$ C( ^7 K2 H  V5 b
                                                        element expires Is only used if the element is not eternal.
1 v" S# d3 t9 N+ p+ B) ~                                                        Optional attribute. A value of 0 means that an Element can idle
4 O1 j& \! v9 E5 t6 Y                                                        for infinity.The default value is 0.3 |2 z$ i+ [; m& \5 @7 n. p! g
        timeToLiveSeconds              - Sets the time to live for an element before it expires.
' X. P! B; r- \& B0 l) F9 q5 |                                                         i.e. The maximum time between creation time and when an element5 V4 A) P7 S, w$ X; [  V6 o
                                                         expires.  Is only used if the element is not eternal.' K/ I5 Q9 w  i* o/ q% o0 a( y
                                                         Optional attribute. A value of 0 means that and Element can live$ c8 C9 r7 j- w+ T! y8 f) z9 [
                                                         for infinity.% @; f: h& {2 _7 h
                                                        The default value is 0.
  A- F# H) o) A9 ^1 O        diskPersistent                           - Whether the disk store persists between restarts of the Virtual
) x6 f. M2 U( H9 U1 h/ c: |                                                             Machine.
! V4 C2 h$ ^8 o  E& R* m$ m. s9 s: |                                                         The default value is false.$ |8 f1 l4 X2 {# ^4 U
        diskExpiryThreadIntervalSeconds   - The number of seconds between runs of the disk expiry thread.; X. ?& u% R0 Y' k1 R5 j' `
                                                         The default value  is 120 seconds.
( D6 N: y# ~: s1 i0 _) b        -->

    <defaultCache
4 ?- \0 z' _0 a3 t% d" w        maxElementsInMemory="10000"
4 k: }. ?0 ^# ]/ c        eternal="false"* G& x. s& G& V
        overflowToDisk="true"* F6 C! h0 d: X( B5 D* r  |
        timeToIdleSeconds="120"
1 \$ M( @+ F* B. X- F5 H4 M; R        timeToLiveSeconds="120"7 a3 r- t& t+ ~7 k3 t3 O1 X
        diskPersistent="false"
" r1 b  o3 r. P0 T, r4 g: P7 O        diskExpiryThreadIntervalSeconds="120"/>- G! l+ C7 H* R& x5 {
    <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"
. q: l8 b: [+ @+ p+ a  J     eternal="true" overflowToDisk="true"/>& ?3 y" U: ]1 Y& ]- _, X
    <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"" t2 _0 C# C" f3 b3 n( P: ]
    timeToLiveSeconds="120" overflowToDisk="true"/>' L) J/ r* S5 w  c6 y
    <cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=
7 G6 x4 F2 u: V0 d- e0 P3 }        "600"    timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>4 j' l" z! e! _  {% i1 C* i6 Z! ?
    <cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"& u3 }6 s# J+ L& |9 w
    eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"$ b+ h7 n: P$ t! C; x4 f- ~
    diskPersistent="false"/>$ \( z2 w% }5 D" O
    <cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"
0 ^, F% ?2 z* l9 B) p    timeToLiveSeconds="600"
/ N( C* W' v; S% c    overflowToDisk="false" diskPersistent="false"/>4 a  H6 Q2 P+ R( S8 i" c' q
    <cache name="com.ouou.model.Videos" maxElementsInMemory="10000"  eternal="false"
. F7 l+ C  R1 E/ Z! O; @    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
* B6 T4 Z" T+ J; ?- N    <cache name="com.ouou.model.Tags" maxElementsInMemory="10000"  eternal="false"
" a% ~8 {/ V  `3 v& \' \; W    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
2 J( h( }( p3 h( I. Y3 E</ehcache>

以com.ouou.model.Videos为例子+ }! p; x% C1 g' A6 [7 W
在Videos.hbm.xml中配置:2 _  b! g' s5 Y$ c
<class name="Videos" table="TEST" lazy="false">+ |% y. m( }( {5 C" ^) U( l
  <cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。+ B7 u1 O- L) M8 [
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,
, ]7 M; T) J0 X) a4 p如果不存在与类名匹配的cache名称,则用defaultCache。
0 e" j0 j; W& l$ Z8 p如果Videos包含set集合,则需要另行指定其cache/ w! y6 U4 A3 `0 Z) ~
例如Videos包含Tags集合,则需要' D2 g+ Y' C" [- a; q
添加如下配置到ehcache.xml中
6 d5 `& x% e3 v% G  e0 M+ O* e$ q<cache name="com.ouou.model.Tags"; M+ F) s2 h+ Q7 j5 F
        maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"( k# Y' j: J; c: t2 F
        timeToLiveSeconds="120" overflowToDisk="false" /># ?) B4 F; f; S9 ]
另,针对查询缓存的配置如下:
+ \7 I& p: C5 p, w+ w<cache name="org.hibernate.cache.UpdateTimestampsCache"4 Z6 |+ M$ v5 Q) Y! g. o! S
        maxElementsInMemory="5000"
( V% E  S  L, h- d+ S" ]' s0 s4 m1 @( ~        eternal="true"; M# H9 t) F7 {( F, A8 O7 Y
        overflowToDisk="true"/>% h6 |4 b6 T- Z3 o2 L1 p2 g3 {3 V
<cache name="org.hibernate.cache.StandardQueryCache"
) j( d6 u* i1 x7 u        maxElementsInMemory="10000"
0 z% k# r+ P; v5 x        eternal="false"
4 E8 m0 X: e2 r5 Z# I        timeToLiveSeconds="120"
# n: D: z' D& r' _  W& c        overflowToDisk="true"/>

3、 选择缓存策略依据:

<cache  usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>' I# _" O, O) S- l
ehcache不支持transactional,其他三种可以支持。' O( @2 [% R5 X" ], b
read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,
4 C! x" F( O. K4 p4 I但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。
- w; S( D* p) w4 h# u( Wread-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level- O* D7 D9 q! e' g9 N- G( s
(序列化事务隔离级别)
8 B8 U% f8 P5 ^0 J/ Enonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,. O- h; h7 W/ z3 s
那么比较适合使用非严格读/写缓存策略。

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

5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (1 `/ W! c, |; z6 s9 h; F
org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。
. F1 O- Q% t. V& V9 J请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。
  [5 e% |. ~( Q* }需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。

+ X4 S( ~  }/ _

Ehcache的配置说明


  \& ]3 l; W4 A3 S6 G- l<ehcache>7 t. f1 r' z2 n
<!--: z1 ?" u8 Q3 d; D1 a
磁盘存储配置:* b2 g( t$ @& v/ M% s+ X* L. Q2 |& f
用来指定缓存在磁盘上的存储位置。可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)
; P7 a# a) I' o" B9 w, T" L) X-->
' w, k0 O  N8 \6 n( o<diskStore path = "/var/apps/cache/" />- Y3 ~$ y1 y' z1 H: B! r% X
<!--! Q( Q/ B5 F+ u* V- v4 ~% F
指定CacheManagerEventListenerFactory,这个对象在缓存添加的时候会得到相应的通知
& U6 `1 b- C/ J- `1 nCacheManagerEventListenerFactory的属性! S+ v7 {7 ~$ i' X
*class - CacheManagerEventListenerFactory的一个实现类; \3 C! z/ z7 I) }
*properties - CacheManagerEventListenerFactory的属性值,以逗号(,)分割多个属性
  @* V& O& A6 @  c如果没有实现类被指定,则系统不创建CacheManager的监听器,没有默认值' V. U' ]5 {6 W/ t
-->
4 R1 v0 z  {/ `; s4 N+ [5 C7 y<cacheManagerEventListenerFactory class="" properties="" />1 O6 s% E: Q5 f% {: b! {6 N* ]
<!--
- h# p, ~! x, v! l( ~+ a$ J在进行分布式缓存的应用时候需要指定CacheManagerPeerProviderFactory,- p; F8 g0 C. u9 f) r& Z
用来生成CacheManagerPeerProvider的实例,以便和集群中的其他CacheManager通信。
) H7 ]/ H0 {! L& Q*class -CacheManagerPeerProviderFactory的一个实现类
# k+ `3 R" V+ m1 _: i*properties - CacheManagerPeerProviderFactory的属性值,以逗号(,)分割多个属性
5 d4 Z3 [* a* l# {2 _6 q6 bEhcache内建了2种基于RMI分布系统的通信策略:
, a) @/ }! ~% ?7 U/ V*automatic - 使用多播组。在一个节点加入或者推出集群的时候自动感应; n! q( E+ X, V6 P+ m
*manual - 硬编码方式

目前的awf中不考虑分布缓存6 O+ S( H) W. _4 y$ w! ^
-->
9 t0 R% j& S( `5 e! W<cacheManagerPeerListenerFactory class="" properties="" />0 T$ e% m2 K1 V2 ?1 ]- n" X1 u
<!--
: [( A; Z: T1 p; `9 k: O5 V缓存配置。
$ ^) ^% x' B3 t' E$ P- C以下属性是必须的:
$ n4 t  ]6 `  |5 qname - cache的标识符,在一个CacheManager中必须唯一, \1 H% U9 Y/ J3 t, W1 P
maxElementsInMemory - 在内存中缓存的element的最大数目
+ ]: o- b  i# J/ Y2 e( jmaxElementsOnDisk - 在磁盘上缓存的element的最大数目9 u- d6 I/ @# E6 a4 Y; \
eternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
. d0 ^# F, k! t; N6 ~overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上" r( p. \1 Y, |2 k/ H" b
以下属性是可选的:0 `2 }- G! Z# C1 z( y
timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.
1 Z6 E: h# l7 t, r        (如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove): W) c$ q5 \! Z: p* ~# u5 V
timeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期
, H# E7 o; }  i# t; w        (是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
) Z8 R0 O3 L( t  m2 O8 g: gdiskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。( N  X$ y4 x0 `$ R* @7 A
        (测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)/ k8 G' o- r$ W& c# u1 @
diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.
4 w& A' n# E+ P! f6 f5 t, p' l        (测试一下0的时候会如何)
: F' ^; x5 ?/ F0 C$ e! l+ k" e/ D1 CmemoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,1 a/ }; S3 C  w- ]4 \
        移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO

可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些
$ O  H; U7 B. H1 _*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件
& g7 x) t  t6 |( B( C" l*bootstrapCacheLoaderFactory - 启动时加载缓存的element
9 H" a# C* s: z' m# j) Y2 p每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。3 n, c/ J- H, h* L
Ehcache内建了基于RMI的实现 - RMICacheReplicatorFactory8 I4 T; N4 y3 D, W7 R: m  i
    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"" |" _+ \) q) G. K: w4 b! ]4 s8 r
        properties="replicateAsynchronouly=true,8 O$ C2 _9 m4 E6 p2 N1 J" _9 v
        replicatePuts=true,
! F0 O* i' N+ }/ [        replicateUpdates=true,
4 z5 e' m) U% M" Z; B% U. o        replicateUpdateViaCopy=true,
& _/ w: m: {1 i1 D" a4 G        replicateRemovals=true" />

-->/ g2 `1 H; C, H" e
<cache .... />
- \* `5 L6 a6 q4 g<!--
) G+ U; F. |- m) X默认的Cache配置。用来实现CacheManager.add(String cacheName)创建的缓存: y  M/ h" x0 `+ B+ C1 p4 h
-->1 O, {0 L+ N! t2 T% v! g
<defaultCache maxElementsInMemory="10000" eternal="false"
' I5 z* Y( }* b        timeToIdleSeconds="120" timeToLiveSeconds="120"5 a& `6 z' A. v0 d3 `8 w6 I. F
        overflowToDisk="true" maxElementsOnDisk="1000000"6 W: M( j7 P, J  h) l
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
& {$ T  s& K( }" k- E        memoryStoreEvictionPolicy="LRU"1 F- g2 p, U' X
        />

</ehcache>

% d0 }6 ?, f6 \3 F0 p4 [

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


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

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

   

关闭

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

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