科帮网-Java论坛、Java社区、JavaWeb毕业设计

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

动态微博

查看: 3651|回复: 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"?>; v0 T0 [: F, U: o+ H# i
<ehcache>
9 I4 U! R/ _2 f/ G1 S: U7 c <diskStore path="java.io.tmpdir"/>. ]1 F5 q' Q/ n* `# A
  <defaultCache
* u1 d1 i& v1 J! d% \5 U   maxElementsInMemory="10000" <!-- 缓存最大数目 -->
0 L7 x9 ]/ K6 l2 `   eternal="false" <!-- 缓存是否持久 -->

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

   timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->+ Y- u* e' d9 V- z' V/ I* M: q0 k
   timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->
" p+ d6 {  w& S% z; G4 H4 C+ G$ b$ n5 f   diskPersistent="false"
  W: Z+ G" k9 b" O7 F% ?( C8 R   diskExpiryThreadIntervalSeconds= "120"/>7 e9 L! y" S0 K! Y8 H# P1 `
</ehcache>

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

<!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->- |/ j0 d2 j9 w% n: u6 P4 e
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
7 I9 g: U' {5 I' e. j2 ] <!-- 是否使用查询缓存 -->

 <property name="hibernate.cache.use_query_cache">true</property>
' P( M+ n' R, ~# ?3 j+ ^  如果使用spring调用Hibernate的sessionFactory的话,这样设置:
7 P( S* ]0 N4 m5 T* h; i1 p. f  <!--HibernateSession工厂管理 -->

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">: Z0 R' q1 a- j5 n
   <property name="dataSource">
/ {* e3 F/ X7 M: c1 K+ F    <ref bean="datasource" />
8 l9 S9 l* J2 p# _* t3 [0 n   </property>
( h. t4 [% m  S. W  c) {# F   <property name="hibernateProperties">
. W. P& d. }2 g$ s0 H   <props>: I/ R2 Q- L" A7 {! a5 t
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>2 P& Q% _$ B2 T  G2 K/ L7 o8 O. b
     <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>* F" ^6 V5 m+ @- O0 [: T, l3 p
    <prop key="hibernate.show_sql">true</prop>$ t: T1 H; O  P4 y5 }  s1 v
    <prop key="hibernate.cache.use_query_cache">true</prop>
; I: O  x+ Z" Y    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  o+ y7 E0 m7 L   </props>4 h+ V3 b! d: q% s, W. D
 </property>
) W+ w1 M9 m0 J7 ?' }: l <property name="mappingDirectoryLocations">2 b& V: Z/ K$ r5 f# e
  <list>
7 [( E* ^, [9 Y) W9 E   <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>
. `! S- K, }7 v# [. N  </list>
' m8 m2 k: A2 [  F( H7 A </property>
3 ^9 i6 s1 z( ~$ r. c</bean>

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

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

) K& P5 U5 ~7 G9 y9 [7 M
  4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性

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

package cn.rmic.hibernatesample;

import java.util.List;

import org.hibernate.CacheMode;
$ {, j% N" g0 N* E) U9 uimport org.hibernate.Criteria;
$ f7 F2 t& r/ E- Qimport org.hibernate.Query;
) r1 |( \9 q) d6 J8 h6 mimport org.hibernate.Session;

import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;
# Y( o2 p8 Q* Himport cn.rmic.manager.po.Resources;

public class testCacheSelectList ...{

 public static void main(String[] args) ...{, p  w1 ?# r# A# G
  Session s=HibernateSessionFactory.getSession();+ ]+ I; }' B/ q; t$ ^: y! s
  Criteria c=s.createCriteria(Resources.class);% l$ ]! P+ S6 J9 H5 O9 `3 }7 w1 R
  c.setCacheable(true);# D$ j$ j1 X# @5 S& H- n! D
  List l=c.list();

  Resources resources=(Resources)l.get(0);
& M  \% s4 ?$ Q4 B5 L  System.out.println("-1-"+resources.getName());' X$ q) H  F5 d: u
  HibernateSessionFactory.closeSession();


+ U- ^3 O  b+ k  try ...{
- V7 ]6 D9 G8 o1 o" _' i   Thread.sleep(5000);# A7 Q4 w% h6 m- m7 \3 Z
  } catch (InterruptedException e) ...{. ~- h/ Y! _" O
   // TODO Auto-generated catch block4 T& w5 Y* }$ D
   e.printStackTrace();" l0 T' x/ q: f0 q! C
  }
6 S. x8 p4 w4 u3 j* i, j$ `  s=HibernateSessionFactory.getSession();
& \& H/ Q  t$ ^; {  c=s.createCriteria(Resources.class);
# O4 M* I1 g  z( w5 {* i. ~+ U3 \+ v  c.setCacheable(true);
7 @( s; O- f0 \6 i7 N- Z* S+ u  l=c.list();" R) a# y- r  ^2 |. ?  A& I# ?
  resources=(Resources)l.get(0);
2 i6 T! N$ c0 i6 |; M7 d# v" p  System.out.println("-2-"+resources.getName());' Z7 H7 O9 ~0 e: L
  HibernateSessionFactory.closeSession();, t. F- [+ H& L6 O6 d+ `( Q4 I
 }: I" G$ \/ m! n
}

6 G6 h! B$ R) r/ ?+ M% W$ W

1. 在Hibernate配置文件中设置:* v7 F4 U$ S7 s( z
   

<!-- Hibernate SessionFactory -->0 b) X5 N# c5 g' \9 q
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">* m( I9 U7 G) m" r6 j8 u
        <property name="dataSource" ref="dataSource"/>4 h) T: r) ^, y" S; w6 p
        <property name="mappingResources">' y) w7 Q* {; G* t
        <list>
1 M2 n8 Y8 X  k6 @! {+ y( l            <value>com/ouou/model/Videos.hbm.xml</value>  
- w& t& M3 D3 |( d         </list>
) z4 Q: \4 C- g$ C6 B6 d( m         </property>
8 A2 V9 H! b/ F: E3 ?        <property name="hibernateProperties">
  ^% O5 Z8 U% i" u0 |" Y$ T: ~- C            <props>; b" J; Y% `/ C; P7 }( ?! b4 U, v
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
+ _9 q# \5 {6 K. j5 M7 q: a3 {                <prop key="hibernate.current_session_context_class">thread</prop>
- s6 V, m! Y- Q! M                <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>& q6 V1 L- h/ Z; R; Q
                <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
# G  ~4 u, Y# o                <!--add ehcache-->  b& w( ^4 F  p
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>8 P4 q; v7 T! |" L
                <prop key="hibernate.cache.use_query_cache">false</prop><!-- 是否使用查询缓存 -->: U& Q( I: H+ t- u. v
                <!--4 h/ C- j3 C# X% t0 v0 j+ c1 h' I
                <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
' ?7 V+ q0 U5 V: r- u                <prop key="hibernate.show_sql">true</prop>2 d, g! l, W4 q$ P& A
                -->+ f4 Z5 X7 p% v: f* M6 X
                <!--<prop key="hibernate.transaction.auto_close_session">true</prop>-->
, B7 K( T" E2 ?' T, F. V                <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
/ p; d1 @! e4 ?/ f, W0 Z                <!-- Create/update the database tables automatically when the JVM starts up
( b7 l/ r" f5 s. N+ j                 <prop key="hibernate.hbm2ddl.auto">update</prop> -->
8 ?" V' {) X9 J: I: G                <!-- Turn batching off for better error messages under PostgreSQL -->
: S3 H( A& u& I                <prop key="hibernate.jdbc.batch_size">25</prop>
. V7 G" K' b( z$ {$ L( b                <!--! M& A/ u6 J& h9 O. a4 }8 o
                <prop key="hibernate.connection.pool_size">10</prop>; H6 \) z; B* Z  f
                -->7 d; w6 h, T# J+ B
            </props>% [+ ]7 d: E$ V( S$ B
        </property>
1 l- L; |- z2 h    </bean>

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

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

, @5 W2 l) l  p5 E9 |
<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
+ P( |# o! \1 D5 P     its value in the running VM.
: n/ P' p9 I; x# U     The following properties are translated:
$ l8 e( N- L0 o0 J$ _     user.home - User's home directory( ~1 ~2 k" y5 a( v' \6 o0 O
     user.dir - User's current working directory
, q1 g( O( n/ B( E     java.io.tmpdir - Default temp file path -->) x2 @# c. Y1 b7 ~9 B# ?
     <!--<diskStore path="java.io.tmpdir"/>--># M( c4 B8 O- P+ q
     <diskStore path="/data/ehcache"/>

    <!--Default Cache configuration. These will applied to caches programmatically created through
& e. P! W$ r9 U1 V. [- R% {        the CacheManager.

        The following attributes are required:

        maxElementsInMemory            - Sets the maximum number of objects that will be created in memory
4 p9 K+ U1 D7 q* i- a, _3 A( z, H  S        eternal                                     - Sets whether elements are eternal. If eternal,  timeouts are
, Z4 J/ r( i7 ~% K5 _                                                            ignored and the element is never expired.$ }# a1 W+ ^- K$ S
        overflowToDisk                      - Sets whether elements can overflow to disk when the in-memory cache$ F% ~' k5 A  ]% I3 ?# s3 _
                                                        has reached the maxInMemory limit.

        The following attributes are optional:
' F7 d- s3 n& z8 s! B        timeToIdleSeconds            - Sets the time to idle for an element before it expires.' M: }# L/ O6 g& p  f% _6 y( U9 K
                                                        i.e. The maximum amount of time between accesses before an5 p* l7 o, r$ \
                                                        element expires Is only used if the element is not eternal.
8 b. e# i: S# O                                                        Optional attribute. A value of 0 means that an Element can idle
: A  I. c" G2 \/ X: O                                                        for infinity.The default value is 0.# m+ E8 D. z' \5 I1 \! T
        timeToLiveSeconds              - Sets the time to live for an element before it expires.3 Z" i/ n# a* n$ ^5 L
                                                         i.e. The maximum time between creation time and when an element$ y, ^" x# \7 t
                                                         expires.  Is only used if the element is not eternal.2 B7 r( L* s8 X& @' |8 u" k6 U
                                                         Optional attribute. A value of 0 means that and Element can live9 a" A: h  i( e3 `/ Z
                                                         for infinity.1 u, @$ }* j1 w: Z; G1 k  O
                                                        The default value is 0./ E) p. t; X) k% N7 `, ^
        diskPersistent                           - Whether the disk store persists between restarts of the Virtual8 |4 g6 F; X* O! W
                                                             Machine.
0 H9 |$ u5 g. J$ v1 _                                                         The default value is false.0 f8 W4 G! x4 a1 r, F. L4 Y
        diskExpiryThreadIntervalSeconds   - The number of seconds between runs of the disk expiry thread.3 B; a) s3 b. l
                                                         The default value  is 120 seconds." K/ l4 ]* ?( j' @
        -->

    <defaultCache
: b& B* |7 b  W) [# b        maxElementsInMemory="10000"6 a  g( F  l3 ]! i1 n
        eternal="false", L  R5 R6 Y, ^" ^  y
        overflowToDisk="true"
- T2 M0 L0 h( ~( @! y, I        timeToIdleSeconds="120"
* ~+ E9 K6 V3 q0 b6 ^        timeToLiveSeconds="120"
% g# _' i; S0 ?0 g- o        diskPersistent="false"
) u& v+ J; c5 o        diskExpiryThreadIntervalSeconds="120"/>- H: t8 O3 ^0 S) C
    <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000"3 r0 [! a$ a# N+ i, n
     eternal="true" overflowToDisk="true"/>0 s8 H  Y: C5 g$ X; j9 }* G! {7 t
    <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false"& ]# e: M! ~3 l6 Q- Z4 e0 v
    timeToLiveSeconds="120" overflowToDisk="true"/>
& ]* w' I1 n8 _% J" c5 f9 |    <cache name="userCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds=" D9 a$ N# g0 G8 S/ `/ l5 D8 k
        "600"    timeToLiveSeconds="600" overflowToDisk="false" diskPersistent="false"/>
$ Z8 s* {2 N; R9 f* {, y) R2 f    <cache name="com.ouou.webapp.util.OuouMethodIntecepter" maxElementsInMemory="100000"% h+ ^( i2 T' P& L( M% U
    eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="false"" F" T6 G3 r  p  c6 o3 e9 C
    diskPersistent="false"/>' Y! f1 H2 L  A# a* d
    <cache name="bbcode" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="600": u) [. R0 \, X" ?0 n* c" Z5 v) D
    timeToLiveSeconds="600"; S3 ]3 v/ F$ K9 d2 o$ k- r
    overflowToDisk="false" diskPersistent="false"/>
. p4 x3 v) R% T) C6 _3 L( p    <cache name="com.ouou.model.Videos" maxElementsInMemory="10000"  eternal="false"
/ @6 t3 Y1 X; j2 N/ V    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>+ F/ w! ^: |, c4 m, i
    <cache name="com.ouou.model.Tags" maxElementsInMemory="10000"  eternal="false"
+ _8 d1 N% |2 d( S# H    overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false"/>
* o( l+ N! f# d) _  }</ehcache>

以com.ouou.model.Videos为例子
( K8 {  A: p# u4 j$ C9 H7 \在Videos.hbm.xml中配置:
3 i) h: u& q% _2 @, V<class name="Videos" table="TEST" lazy="false"># |& K8 v# e& G, D+ g. }
  <cache usage="read-write" region="ehcache.xml中的name的属性值"/>注意:这一句需要紧跟在class标签下面,其他位置无效。+ o; e1 f( m9 r; }+ @* d
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.ouou.model.Videos的cache,, Z, F# _/ ~# k  ?5 R! W
如果不存在与类名匹配的cache名称,则用defaultCache。
; w; ^. J  N- Y如果Videos包含set集合,则需要另行指定其cache
# d" q- @# r! M/ [例如Videos包含Tags集合,则需要
( y+ @* s; b# B: v1 W' Q添加如下配置到ehcache.xml中9 o  E+ n, R9 O4 R- m, J* x$ J
<cache name="com.ouou.model.Tags"
  P: q( m0 m6 e. s/ ]! B  [        maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"1 L2 z# |+ I! A& B! T# r
        timeToLiveSeconds="120" overflowToDisk="false" />
  L: J1 h! y/ a( U  H) S) V另,针对查询缓存的配置如下:
9 O0 L" K" [$ a# m9 |<cache name="org.hibernate.cache.UpdateTimestampsCache"
4 Q( i/ @$ k4 }; V0 u        maxElementsInMemory="5000"0 l( i+ S$ |. H& [) N* L% P
        eternal="true"
! x2 u" B9 g  F5 q) }        overflowToDisk="true"/>3 d) g7 e+ V! o7 b! [$ B4 j
<cache name="org.hibernate.cache.StandardQueryCache"+ V% }! A  X; B( \: [. d
        maxElementsInMemory="10000"
5 A) g6 W; d, }9 d: t+ y/ K7 X! b- H        eternal="false"
. g7 P+ C0 v6 m% o& e9 E: B        timeToLiveSeconds="120"
6 \4 @2 M- J; \; ^9 l3 }        overflowToDisk="true"/>

3、 选择缓存策略依据:

<cache  usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>7 }# V3 i, R" K' n8 t6 q# t+ y) t
ehcache不支持transactional,其他三种可以支持。, u- {* Z- C( W- f0 J! u
read-only:无需修改, 那么就可以对其进行只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,& s! c: D$ N% }' `( C& ]$ ~9 E+ ?
但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。
$ b- Q0 \& c- F' m, Fread-write:需要更新数据,那么使用读/写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level
. D+ ^! L* s: z  S  z(序列化事务隔离级别)
9 Z% u3 L* `. [nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,- ?# y3 k% _/ z6 K
那么比较适合使用非严格读/写缓存策略。

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

5、 使用ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集 (: n/ V: j: C& I; [: ]
org.hibernate.cache.StandardQueryCache);另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。' h( K1 J% E( _5 @  |! i0 H
请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。
& [  T9 O0 s8 r" M5 Q需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。

# U# _, ~2 w1 b' B& Y/ }5 H

Ehcache的配置说明

3 y1 R2 k6 a: J, x  G
<ehcache>. m- a! h8 ]0 @& `! Y8 N
<!--+ A/ k! w6 {0 U; o7 Y
磁盘存储配置:
, y" ^& }/ p6 f4 R2 U用来指定缓存在磁盘上的存储位置。可以使用JavaVM环境变量(user.home, user.dir, java.io.tmpdir)
* F% z* ^( w! y4 S-->% w7 J4 Z9 V6 E# t
<diskStore path = "/var/apps/cache/" />
5 u) X2 ?& M, @/ t, h6 Q1 M<!--; z7 @# u9 p+ r
指定CacheManagerEventListenerFactory,这个对象在缓存添加的时候会得到相应的通知
# ~4 d7 w! M1 L/ n& l7 a8 N8 mCacheManagerEventListenerFactory的属性/ J. `) }/ {3 [) R; v5 r
*class - CacheManagerEventListenerFactory的一个实现类
0 }3 F. U! ~5 A$ F5 l4 o& c*properties - CacheManagerEventListenerFactory的属性值,以逗号(,)分割多个属性
' ~2 i" g& A( I+ d# u0 L如果没有实现类被指定,则系统不创建CacheManager的监听器,没有默认值: n& G+ P# `, d  D3 O) Z
-->
$ P- z" U& p5 j% A<cacheManagerEventListenerFactory class="" properties="" />
9 {8 d, W, l2 o- B' h6 E7 `<!--# Y% m3 `4 |! }+ C: {- g- G# C
在进行分布式缓存的应用时候需要指定CacheManagerPeerProviderFactory,3 J3 a* h. u! N5 z+ C- p& f
用来生成CacheManagerPeerProvider的实例,以便和集群中的其他CacheManager通信。2 Q) L: j0 k' W
*class -CacheManagerPeerProviderFactory的一个实现类
0 x- }, G; v0 O: n# c*properties - CacheManagerPeerProviderFactory的属性值,以逗号(,)分割多个属性4 i5 e* N0 {; t# E" m
Ehcache内建了2种基于RMI分布系统的通信策略:" E' V& d. a( @7 ^- @, J! [2 m
*automatic - 使用多播组。在一个节点加入或者推出集群的时候自动感应$ d9 S# Q' D: _" G/ f5 w: h5 Z
*manual - 硬编码方式

目前的awf中不考虑分布缓存% Y- B6 M6 J5 `/ o3 T5 a  i% Z
-->' q  x+ v& q) W3 p# O1 z
<cacheManagerPeerListenerFactory class="" properties="" />
; T8 p3 |9 [0 ~1 r9 g5 ^<!--
% y- Z/ N+ B: B, `缓存配置。
& [: V* i$ A" J! G! a8 j以下属性是必须的:
7 i. a, Z# K, K6 Z- Pname - cache的标识符,在一个CacheManager中必须唯一5 `; F1 k  d) {( F. f0 h7 x) U- J
maxElementsInMemory - 在内存中缓存的element的最大数目
2 ^8 ~8 q+ j  Y  ?% p( J1 umaxElementsOnDisk - 在磁盘上缓存的element的最大数目
" |% Y* y9 x: n) J0 ]0 Z" }( u3 qeternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
& Z/ n/ V) E8 s0 ~overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上( |. t  J8 l+ U& }9 A+ ~
以下属性是可选的:
) @* E6 ]2 W, stimeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.
- i. q+ s' f) `2 j. F+ }# K        (如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)1 t0 }" A  ~5 }* P
timeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期+ G; P  L5 c" L4 j8 ]4 g7 p) d
        (是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
  H! v! q- }4 B* v& g# m+ x+ _6 odiskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。
4 s3 M. W' w- w* B- t0 g5 Q        (测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)
' _5 t" X, H. D* P2 I' ]) g2 r* JdiskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.
" }. [! U- W$ Y& x7 ?/ N, T% _. K/ Q        (测试一下0的时候会如何)5 M# p3 l6 h$ a( q, }: s
memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,6 e0 d2 q% u) V: @7 H
        移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO

可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些) a/ `; B* D, X9 h2 E- m( k) L
*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件
8 R, ?" q+ d) X$ t: U' `*bootstrapCacheLoaderFactory - 启动时加载缓存的element
6 V& A0 q  s% N3 L1 n$ _9 J7 d* x; }每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。! @0 s6 M0 w7 a3 w8 i
Ehcache内建了基于RMI的实现 - RMICacheReplicatorFactory
0 i8 O, A! n  e/ l$ v    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"+ s, A0 e; o, {7 L; j6 x7 Z
        properties="replicateAsynchronouly=true,: U/ D. \$ k6 u9 z
        replicatePuts=true," m% U! Q8 s  F& ~: G
        replicateUpdates=true,
0 e) @, v) {9 S# F        replicateUpdateViaCopy=true,
% p8 b2 ^6 D3 l4 G- ~' e        replicateRemovals=true" />

-->
" d7 g% C% E3 r9 c' D  z7 {<cache .... />
2 o% M! V- |" i7 Q1 a5 V6 N<!--
( M: z3 l8 Z  C' Z; j默认的Cache配置。用来实现CacheManager.add(String cacheName)创建的缓存+ ]7 ~9 }9 t5 c
-->0 g; Q, E5 D; [$ A& _
<defaultCache maxElementsInMemory="10000" eternal="false"( C6 |" N, A3 w$ e) D0 _2 _& }0 \
        timeToIdleSeconds="120" timeToLiveSeconds="120"- N# E" R, ^7 Q- J  T( ]
        overflowToDisk="true" maxElementsOnDisk="1000000"
' L& a5 ]3 P6 D2 `5 {4 j  J        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
! F. M1 ]" w9 n* j/ s) A5 ?. n% p        memoryStoreEvictionPolicy="LRU"" G% }" B7 B1 R5 k
        />

</ehcache>

) n- Z3 Z; Q) P' H6 R

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


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

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

   

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