我的日常

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

动态微博

查看: 3801|回复: 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"?>% ], M7 ~7 u# c5 u4 E
<ehcache>; j# a8 M1 B  j' M! Z. S) T
 <diskStore path="java.io.tmpdir"/>
; I& O0 E2 D+ d) F  <defaultCache
3 T# U/ S* J, y   maxElementsInMemory="10000" <!-- 缓存最大数目 -->
6 f7 @! j4 {8 A/ X) A* W   eternal="false" <!-- 缓存是否持久 -->

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

   timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->
+ Z/ L( @! F+ p8 R, a0 r9 U* ~   timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->" P' G, Z1 t" c0 M' ?  _
   diskPersistent="false"
+ d2 E7 z5 W3 r  r+ r4 L$ [8 f   diskExpiryThreadIntervalSeconds= "120"/>
1 h1 A7 s: x% k4 F</ehcache>

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

<!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->
: t3 H5 o- r. a0 N. l" o1 [: j<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>0 \: Y7 u( m, v% o, Y
 <!-- 是否使用查询缓存 -->

 <property name="hibernate.cache.use_query_cache">true</property>
- ^7 R9 ~* u2 ^/ D5 C6 x  如果使用spring调用Hibernate的sessionFactory的话,这样设置:  n6 j8 k% Q* C+ m" R
  <!--HibernateSession工厂管理 -->

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
7 h' x( b. f2 s4 L! w   <property name="dataSource">8 [1 a# O0 E, [8 c
    <ref bean="datasource" />7 U7 d! {( S' l/ D5 E: [# Y
   </property>
9 `8 h: j4 P( T) z. R0 B9 `, M3 G3 ~   <property name="hibernateProperties">
' e( s; z! f; i7 v   <props>
4 ~" B5 ?# ]* q( _4 R- y    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
# A+ r$ Y7 g. v     <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
  _- |4 E+ y! \$ _0 _1 z    <prop key="hibernate.show_sql">true</prop>
, }; y; X/ }  Z& a    <prop key="hibernate.cache.use_query_cache">true</prop>- }5 ~" M! f) f( A7 }
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
: x1 p# t7 i6 f; I0 O   </props>
0 |. U3 c8 d8 W9 ?6 J( R </property>
$ ]! `+ z$ p% c* C. A( d <property name="mappingDirectoryLocations">" S2 ~) h! x8 K+ N* F& G
  <list>( w9 t# _3 C7 d+ N! R6 e
   <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>
5 {& ^3 Y  ^  ^- L6 k  </list>
6 Q1 W4 t; j6 ^/ V$ Q </property>
* M' m, R# e2 m</bean>

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

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

  N7 ^; S  F' H, H) \6 v7 F
  4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性

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

package cn.rmic.hibernatesample;

import java.util.List;

import org.hibernate.CacheMode;4 z4 j. j* d5 j" f
import org.hibernate.Criteria;% B, ~  Q5 s; `9 K0 t% D% N
import org.hibernate.Query;
& S! U& c; u; o; F5 oimport org.hibernate.Session;

import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;4 R9 I7 R/ Y) G* S1 r
import cn.rmic.manager.po.Resources;

public class testCacheSelectList ...{

 public static void main(String[] args) ...{
1 O# |% r# \7 W, g* b( h4 a7 o  Session s=HibernateSessionFactory.getSession();
, o, u2 Y9 H' ~6 T  Criteria c=s.createCriteria(Resources.class);) k( K8 _4 P, H2 H! B* k/ C
  c.setCacheable(true);, n; r* M$ D( o. _
  List l=c.list();

  Resources resources=(Resources)l.get(0);# F/ q5 _3 b0 \0 r% @& L+ A0 [
  System.out.println("-1-"+resources.getName());: b/ s" m! m) @; S8 _
  HibernateSessionFactory.closeSession();

0 i9 p7 Q  m- D6 F, ^; S* a5 X$ [
  try ...{
5 p. g( @5 ~6 y/ Z   Thread.sleep(5000);
6 \0 P2 m( t8 a  } catch (InterruptedException e) ...{
, |; V# p: c9 d3 w* j   // TODO Auto-generated catch block
0 Z: b$ B& g1 Y4 j$ m   e.printStackTrace();! }$ I& v% X% v  u# [4 }
  }
" ^  F- x; e3 g! D! W  V  s=HibernateSessionFactory.getSession();$ }$ Q' V7 ~1 r" O; ~
  c=s.createCriteria(Resources.class);
  Y3 @* }9 P; E# ^( x  c.setCacheable(true);
$ K/ e: P( X4 i9 ]( @/ t5 U3 ?  l=c.list();
( B; ^0 L* ^( X# G/ F- i  resources=(Resources)l.get(0);
" b) v/ [/ X/ `2 U7 J  System.out.println("-2-"+resources.getName());
7 F$ x) S* M3 j& s  G/ n  HibernateSessionFactory.closeSession();
8 ~% E% U7 f& {0 P. {0 E5 q }
0 Q, Z1 r" @- o; S}


. M# r1 X& x2 h/ {* V2 B) `& a

1. 在Hibernate配置文件中设置:
9 p! |  ]  C; e0 Q* n   

<!-- Hibernate SessionFactory -->9 a) K* b8 b1 M7 P5 |" q6 [
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">5 z8 R: S3 o5 e3 \/ l
        <property name="dataSource" ref="dataSource"/>
  f) n3 \* z) ~' _) u+ r        <property name="mappingResources">6 j3 O  {+ g8 H) h5 C
        <list>7 W# [& b2 b  d$ I3 i3 b5 b) Z
            <value>com/ouou/model/Videos.hbm.xml</value>  + }5 R. \9 I- u( n  v7 M
         </list>, P: d2 [$ x& a
         </property>9 S, `+ b; h* y3 `, b8 o
        <property name="hibernateProperties">
& U% ~% N/ J" [$ W# V/ |8 \$ U            <props>3 M% B& _0 @( s& S) E  G& Z" g' H
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>6 e0 ]) ^% a  T. H3 p$ m! l
                <prop key="hibernate.current_session_context_class">thread</prop>
7 F- D  q, \! B5 O4 j                <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>( N. l& A; \: ^
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
: P7 P0 \& {/ k3 h$ I: @0 H0 h                <!--add ehcache-->0 Z5 ]+ b  {; W5 ^8 j6 S2 [# _
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
/ F. r6 I( ?5 I% W; j3 Q3 h% v3 @* b% d                <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->
$ e( c2 `' A4 R5 q4 r0 e0 ]/ W7 D; M' Y                <!--* p) r4 h- K5 k
                <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
  [( J, ~& B# ^! u; c3 B, X                <prop key="hibernate.show_sql">true</prop>4 }3 L9 D3 F' c! a8 h2 `2 t
                -->
  k; ?! H: {: }& ?3 D* S                <!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
3 g* m% L# U' i, M                <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
* G' p2 n( l) {4 E                <!-- Create/update the database tables automatically when the JVM starts up# G" F5 S& T$ z) _; t
                 <prop key="hibernate.hbm2ddl.auto">update</prop> -->
0 w  r' J/ i+ R5 q# y" h/ V  z, h                <!-- Turn batching off for better error messages under PostgreSQL -->
+ \, H: [1 P3 p6 G  P                <prop key="hibernate.jdbc.batch_size">25</prop>3 E; X3 T) a' r: V) s# _
                <!--3 e; W: Z1 B( T" T) L
                <prop key="hibernate.connection.pool_size">10</prop>3 g# F+ F  S7 E. J
                -->
; O" |# M  S1 f            </props>! ^) Y( X: x: Z. U4 \( p* {
        </property>! V! a# z3 w+ Y1 m
    </bean>

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

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


) `+ R& V/ T/ R8 h1 B <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, b0 ]. Y9 U1 f, i2 m  |
     its value in the running VM.7 X: ~( P  S# D! N  C$ g
     The following properties are translated:+ s" H4 `4 z. _4 L  e  B
     user.home - User's home directory
# ~  q: B* r# V- _- l: j$ W5 C% Y; p     user.dir - User's current working directory8 z) D: ~( o' d, Y: s9 |2 g( W
     java.io.tmpdir - Default temp file path -->$ x  g. `/ }$ l: e8 ]
     <!--<diskStore path="java.io.tmpdir"/>-->
& Y7 A8 f) P. a- Y     <diskStore path="/data/ehcache"/>

    <!--Default Cache configuration. These will applied to caches programmatically created through& z# \8 ?% b: q& w
        the CacheManager.

        The following attributes are required:

        maxElementsInMemory            - Sets the maximum number of objects that will be created in memory
1 ?1 n& F/ {( V9 q        eternal                                     - Sets whether elements are eternal. If eternal,  timeouts are
1 ]! [/ ?. i+ B5 E, ]' P$ I; `                                                            ignored and the element is never expired.  I8 m5 F6 I( h$ d) g
        overflowToDisk                      - Sets whether elements can overflow to disk when the in-memory cache
( b+ e% O+ G# z4 z. x4 F                                                        has reached the maxInMemory limit.

        The following attributes are optional:& w* n: q3 @- h
        timeToIdleSeconds            - Sets the time to idle for an element before it expires.
. O: f8 m) y' O, [2 O                                                        i.e. The maximum amount of time between accesses before an- E  M2 a4 k8 Z( n( _
                                                        element expires Is only used if the element is not eternal.
! Y2 `' h5 M+ g) F+ Z3 C6 e                                                        Optional attribute. A value of 0 means that an Element can idle1 T/ s' e, _. n+ `: H/ w& ?
                                                        for infinity.The default value is 0., O9 O$ C' K! d: @, S: L
        timeToLiveSeconds              - Sets the time to live for an element before it expires.5 f/ D7 O( e- z* K7 _; h- e
                                                         i.e. The maximum time between creation time and when an element) e; M! I0 k; c  S
                                                         expires.  Is only used if the element is not eternal.
' Z2 Q2 w+ L, U9 f                                                         Optional attribute. A value of 0 means that and Element can live
8 E. |$ {9 I( B3 R3 M( j7 m                                                         for infinity.
: w* W7 {1 M9 ~* j- a( [                                                        The default value is 0., o5 B0 K# u0 {: \0 E% f& I/ g$ I
        diskPersistent                           - Whether the disk store persists between restarts of the Virtual
' k1 i$ F, o& B7 [4 S: {  s6 @                                                             Machine.: q1 j1 T# E3 @' H* z# F
                                                         The default value is false.
" v3 ]3 ?. O2 w+ I) w2 a( G        diskExpiryThreadIntervalSeconds   - The number of seconds between runs of the disk expiry thread.; D2 {$ c% H6 u6 L# Y
                                                         The default value  is 120 seconds.
6 i  T; [9 L" o4 S; t3 I        -->

    <defaultCache- ]2 e0 ]4 P/ F8 E2 K
        maxElementsInMemory="10000"8 k2 ~7 a0 y0 ?* V
        eternal="false"
/ l( T0 A* _; j3 K        overflowToDisk="true", m/ ^5 y2 E2 H" N
        timeToIdleSeconds="120". P) o! ^2 e! t% V; c5 e
        timeToLiveSeconds="120"
2 U( k% {/ o9 N. Z        diskPersistent="false"- H% g" D9 W3 W# ^+ j
        diskExpiryThreadIntervalSeconds="120"/>
# C# a. I' W. u' X  Y8 u5 M    <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"
6 G) e9 i* j! J- a; H0 M" T     eternal="true" overflowToDisk="true"/>( B9 S, W! c2 e3 \6 k
    <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"
/ Q! ?8 e+ V2 i2 q% k: `; _    timeToLiveSeconds="120" overflowToDisk="true"/>
( V! d# X$ G& d* B9 h+ j    <cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=8 C! I- T, U9 r1 s2 A7 \" X: R, |
        "600"    timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>7 F0 p! ?2 A- i2 `
    <cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"
  f( |5 W" d' U2 y) B* F% R- K( I    eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"
7 X* a$ n" b+ G    diskPersistent="false"/>
% Y$ e) `% u: Y/ G    <cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600"# k4 {  s6 a4 n# m, n
    timeToLiveSeconds="600"7 R4 l# F3 V! F9 \' f7 ~9 u7 {
    overflowToDisk="false" diskPersistent="false"/>
" K  L& a  b8 E8 b: V    <cache name="com.ouou.model.Videos" maxElementsInMemory="10000"  eternal="false"( Y% a) z% y) w- W+ B* A
    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>  A3 t5 O" Z5 g6 K
    <cache name="com.ouou.model.Tags" maxElementsInMemory="10000"  eternal="false"
# N- F  \1 E* s5 ~2 Z! r    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
7 N9 w! S9 A. r# K5 I</ehcache>

以com.ouou.model.Videos为例子
( b( J3 m2 e% [! C在Videos.hbm.xml中配置:
+ J9 C1 T; O2 j' ^. g<class name="Videos" table="TEST" lazy="false">
6 _) h! i, Y& z: j* u& W  <cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。
. a# Z0 S% S$ ]. F3 b2 [  m8 ihbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,& B, A0 G: g7 U. |
如果不存在与类名匹配的cache名称,则用defaultCache。& }# s! _: y* X7 {
如果Videos包含set集合,则需要另行指定其cache8 O* Y! C, ^, ]* ?( y
例如Videos包含Tags集合,则需要
2 l, b( Z) x( y+ r, c4 T. ^5 U添加如下配置到ehcache.xml中+ w) ~& j4 M8 j' u& j  x+ A
<cache name="com.ouou.model.Tags"
1 h4 T! D( K& E5 @+ w        maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"1 i3 D4 N: U6 N+ M4 ?' C
        timeToLiveSeconds="120" overflowToDisk="false" />% c: t: V/ ]9 E
另,针对查询缓存的配置如下:4 h/ E' j/ X5 a4 ^+ n1 e$ U
<cache name="org.hibernate.cache.UpdateTimestampsCache"
" `( b6 I7 O$ F/ h        maxElementsInMemory="5000"
8 N8 r0 j* X! m6 a0 E        eternal="true"
, P7 v0 g$ ^3 I0 d+ }# [        overflowToDisk="true"/>
$ l& F& ^% T5 e2 C: [0 Y8 T<cache name="org.hibernate.cache.StandardQueryCache"
* c6 `! o9 i$ o5 A. U        maxElementsInMemory="10000"
3 f7 T& L4 h$ u        eternal="false"
% L! Z1 ^/ ]2 h+ ]2 f4 H        timeToLiveSeconds="120"
! I7 |  V0 L1 c  Y  v2 D& M        overflowToDisk="true"/>

3、 选择缓存策略依据:

<cache  usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>
% c% ^: w2 `9 v3 j) z+ j3 Yehcache不支持transactional,其他三种可以支持。
/ R. }: \* `9 Z- f- G* T' yread-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,
- e& V# i8 ]- H8 Q, X' J但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。- y8 g' v* K& C: z# B! h6 {" h
read-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level+ R% p" C; i- R, C% n% r
(序列化事务隔离级别)
2 V, x0 D8 q2 s; Jnonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,
6 i% o4 T3 k, Z: w2 P2 T/ R9 Q6 N7 h' f那么比较适合使用非严格读/写缓存策略。

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

5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (
3 Y' |: X/ r  g* |. `org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。. C% p- n8 @+ p& S- R
请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。
' M: r+ ]5 O3 |8 p+ ~# ~4 N需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。

/ z4 ?# [2 f' n, @

Ehcache的配置说明


& T; r& S) p9 g7 ]; R( P2 u<ehcache>
- t+ t, A6 U( z8 d$ F( L2 j<!--! |6 s; {# V0 a
磁盘存储配置:
" f0 x4 Q  s+ F- B用来指定缓存在磁盘上的存储位置。可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)0 K" s0 v) ]) m" s8 H
-->
. `7 l1 y9 c% E# _7 D" q( F<diskStore path = "/var/apps/cache/" />8 \, r5 p, D9 A* `
<!--. o) B3 J! w+ p: U" P2 z  `" t. ]0 S
指定CacheManagerEventListenerFactory,这个对象在缓存添加的时候会得到相应的通知4 m4 ^% Q7 [8 U; C+ s- e; s
CacheManagerEventListenerFactory的属性. B; B+ ?* t: V
*class - CacheManagerEventListenerFactory的一个实现类
) d, |8 x, j+ d9 C7 j. l+ u1 u# A*properties - CacheManagerEventListenerFactory的属性值,以逗号(,)分割多个属性
' b7 O+ u4 A+ B; ^" _! N4 i如果没有实现类被指定,则系统不创建CacheManager的监听器,没有默认值
/ k. v9 C. H7 j2 `* U* Q-->  ?) N& U# S2 f2 b7 ~9 _
<cacheManagerEventListenerFactory class="" properties="" />+ i+ |5 k5 |4 a* O/ x
<!--1 a% w! R: n5 ~+ c
在进行分布式缓存的应用时候需要指定CacheManagerPeerProviderFactory,
! s: P# [& H2 b  `用来生成CacheManagerPeerProvider的实例,以便和集群中的其他CacheManager通信。6 C8 s* b, x9 Y4 E6 v1 A% {
*class -CacheManagerPeerProviderFactory的一个实现类# m% T$ X9 f8 G3 X
*properties - CacheManagerPeerProviderFactory的属性值,以逗号(,)分割多个属性
) g3 J2 ^& I; d% I+ t$ G8 V1 C: ^* h; PEhcache内建了2种基于RMI分布系统的通信策略:
. r! \; }8 P) e$ E# L*automatic - 使用多播组。在一个节点加入或者推出集群的时候自动感应4 J: s# m: y1 P9 `
*manual - 硬编码方式

目前的awf中不考虑分布缓存
, A0 E6 K. }$ z. u2 b: q' q# q-->
7 p* t: K" m% D<cacheManagerPeerListenerFactory class="" properties="" />
( c" R$ L9 F6 @: Q% |) d5 T<!--
9 f) f! Z5 Q5 B$ \5 D7 e9 ?缓存配置。& e/ x' m3 x! v7 A) y8 `5 w3 D  k
以下属性是必须的:
( h8 G- M+ Q" r& b, qname - cache的标识符,在一个CacheManager中必须唯一
- o9 z; V/ Z7 w7 I. D/ a0 LmaxElementsInMemory - 在内存中缓存的element的最大数目
1 t" K$ Q$ P& m1 N. g: d5 ]maxElementsOnDisk - 在磁盘上缓存的element的最大数目/ l: q$ k* x' \4 a: P; t
eternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
* X. C% U3 M* M& SoverflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上8 z$ `! g' d0 C* ?( ?9 \# ]; R1 e2 L
以下属性是可选的:& ^! v* v# B& L9 E8 J$ h
timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.$ S  t8 h+ |. P; ?3 @- D4 K  ~( y
        (如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)" m. [. w- U9 V$ ]! w. E
timeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期7 s' f% i! v+ ]( w1 t( T
        (是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)/ K$ t0 \/ k/ g; M! ]. H5 A  _1 R
diskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。
$ e0 \9 V& z- Z+ ]/ F; E        (测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)
1 ]/ \. g( ]3 f/ K! ~% zdiskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.
4 i8 [3 Y/ U. t        (测试一下0的时候会如何)5 _4 q$ [. x8 x7 b  ^
memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,) D( y  }. U6 y
        移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO

可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些
. [$ a8 A5 b" O1 `*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件
2 ~& Y" b  g) g0 T*bootstrapCacheLoaderFactory - 启动时加载缓存的element
' C- p, H; ?3 w- @# m$ s' ^每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。
4 ~5 s8 O" M6 t. ?( R4 JEhcache内建了基于RMI的实现 - RMICacheReplicatorFactory
) C0 w3 [4 O5 |+ Q3 C8 ^    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"" h* ~- q8 |5 g* ?3 ~2 ]4 J
        properties="replicateAsynchronouly=true,! c- V+ A+ L6 a. b' R6 m
        replicatePuts=true,
- I% k+ x3 J) K8 U/ [        replicateUpdates=true,
8 {8 ~8 O3 d" {4 [        replicateUpdateViaCopy=true,5 n: i* [: M2 O% T( N6 F
        replicateRemovals=true" />

-->
4 h2 b, D$ I' N" A* ^<cache .... />3 F: @, G+ V, }* F, s; L
<!--: C0 r  K2 y# Q+ T* r# P1 j
默认的Cache配置。用来实现CacheManager.add(String cacheName)创建的缓存' f0 n% C, Z0 {3 c& \
-->
" _" H* F8 J0 L! T& q* O<defaultCache maxElementsInMemory="10000" eternal="false"
% l9 |: y9 e! |( [        timeToIdleSeconds="120" timeToLiveSeconds="120"
9 p2 R2 E$ ?8 s/ X  m% |6 y        overflowToDisk="true" maxElementsOnDisk="1000000"- ^4 x3 I& t* I& Y6 I
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"1 x( i, g/ b5 x( {7 o/ k
        memoryStoreEvictionPolicy="LRU"
, P! M) x+ L9 _. Z        />

</ehcache>

  V  O! ]$ [, @3 e" M, }2 L3 ?

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


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

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

   

关闭

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

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