<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 9, comments - 4, trackbacks - 0, articles - 2
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    【轉】配置ehcache.xml文件

    Posted on 2009-04-17 17:47 我是菜鳥 閱讀(998) 評論(0)  編輯  收藏 所屬分類: java 筆記
    . 在Hibernate配置文件中設置:
    ????
    <!--?Hibernate?SessionFactory?-->
    ????
    <bean?id="sessionFactory"?class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    ????????
    <property?name="dataSource"?ref="dataSource"/>
    ????????
    <property?name="mappingResources">
    ????????
    <list>
    ????????????
    <value>com/ouou/model/Videos.hbm.xml</value>???
    ?????????
    </list>
    ?????????
    </property>
    ????????
    <property?name="hibernateProperties">
    ????????????
    <props>
    ????????????????
    <prop?key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    ????????????????
    <prop?key="hibernate.current_session_context_class">thread</prop>
    ????????????????
    <prop?key="hibernate.cglib.use_reflection_optimizer">false</prop>
    ????????????????
    <prop?key="hibernate.query.substitutions">true?'Y',?false?'N'</prop>
    ????????????????
    <!--add?ehcache-->
    ????????????????
    <prop?key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    ????????????????
    <prop?key="hibernate.cache.use_query_cache">false</prop><!--?是否使用查詢緩存?-->
    ????????????????
    <!--
    ????????????????
    <prop?key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
    ????????????????
    <prop?key="hibernate.show_sql">true</prop>
    ????????????????
    -->
    ????????????????
    <!--<prop?key="hibernate.transaction.auto_close_session">true</prop>-->
    ????????????????
    <prop?key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
    ????????????????
    <!--?Create/update?the?database?tables?automatically?when?the?JVM?starts?up
    ?????????????????
    <prop?key="hibernate.hbm2ddl.auto">update</prop>?-->
    ????????????????
    <!--?Turn?batching?off?for?better?error?messages?under?PostgreSQL?-->
    ????????????????
    <prop?key="hibernate.jdbc.batch_size">25</prop>
    ????????????????
    <!--
    ????????????????
    <prop?key="hibernate.connection.pool_size">10</prop>
    ????????????????
    -->
    ????????????
    </props>
    ????????
    </property>
    ????
    </bean>


    ??? 如果不設置“查詢緩存”,那么hibernate只會緩存使用load()方法獲得的單個持久化對象,如果想緩存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法獲得的數據結果集的話,就需要設置hibernate.cache.use_query_cache true 才行

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

    ?<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
    ?????its?value?in?the?running?VM.
    ?????The?following?properties?are?translated:
    ?????user.home?
    -?User's?home?directory
    ?????user.dir?-?User's?current?working?directory
    ?????java.io.tmpdir?-?Default?temp?file?path?-->
    ?????
    <!--<diskStore?path="java.io.tmpdir"/>-->
    ?????
    <diskStore?path="/data/ehcache"/>

    ????
    <!--Default?Cache?configuration.?These?will?applied?to?caches?programmatically?created?through
    ????????the?CacheManager.

    ????????The?following?attributes?are?required:

    ????????maxElementsInMemory????????????
    -?Sets?the?maximum?number?of?objects?that?will?be?created?in?memory
    ????????eternal?????????????????????????????????????
    -?Sets?whether?elements?are?eternal.?If?eternal,??timeouts?are?
    ?????????????????????????????????????????? ? ? ? ? ? ?????? ignored?and?the?element?is?never?expired.
    ????????overflowToDisk??????????????????????
    -?Sets?whether?elements?can?overflow?to?disk?when?the?in-memory?cache
    ????????????????????????????????????????????????????????has?reached?the?maxInMemory?limit.

    ????????The?following?attributes?are?optional:
    ????????timeToIdleSeconds???????????
    -?Sets?the?time?to?idle?for?an?element?before?it?expires.
    ??????????????????????????????????????????????????????? i.e.?The?maximum?amount?of?time?between?accesses?before?an
    ???????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? element?expires?Is?only?used?
    if?the?element?is?not?eternal.
    ????????????????????????????????????????????????????????Optional?attribute.?A?value?of?
    0?means?that?an?Element?can?idle
    ??????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???
    for?infinity.The?default?value?is?0.
    ????????timeToLiveSeconds?????????????
    -?Sets?the?time?to?live?for?an?element?before?it?expires.
    ???????????????????????????????????????????????????? ? ? i.e.?The?maximum?time?between?creation?time?and?when?an?element?
    ?????????????????????????????????????????????????????????expires.??Is?only?used?
    if?the?element?is?not?eternal.
    ?????????????????????????????????????????????????????????Optional?attribute.?A?value?of?
    0?means?that?and?Element?can?live
    ???????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???
    for?infinity.
    ????????????????????????????????????????????????????????The?
    default?value?is?0.
    ????????diskPersistent???????????????????????????
    -?Whether?the?disk?store?persists?between?restarts?of?the?Virtual
    ???????????????????????????????????????????????????????????? Machine.
    ?????????????????????????????????????????????????????????The?
    default?value?is?false.
    ????????diskExpiryThreadIntervalSeconds???
    -?The?number?of?seconds?between?runs?of?the?disk?expiry?thread.?
    ?????????????????????????????????????????????????????????The?
    default?value??is?120?seconds.
    ????????
    -->

    ????
    <defaultCache
    ????????maxElementsInMemory
    ="10000"
    ????????eternal
    ="false"
    ????????overflowToDisk
    ="true"
    ????????timeToIdleSeconds
    ="120"
    ????????timeToLiveSeconds
    ="120"
    ????????diskPersistent
    ="false"
    ????????diskExpiryThreadIntervalSeconds
    ="120"/>
    ????
    <cache?name="org.hibernate.cache.UpdateTimestampsCache"?maxElementsInMemory="5000"?
    ?????eternal
    ="true"?overflowToDisk="true"/>
    ????
    <cache?name="org.hibernate.cache.StandardQueryCache"?maxElementsInMemory="5"?eternal="false"
    ????timeToLiveSeconds
    ="120"?overflowToDisk="true"/>
    ????
    <cache?name="userCache"?maxElementsInMemory="100000"?eternal="false"?timeToIdleSeconds=
    ??????? "
    600" ?? timeToLiveSeconds="600"?overflowToDisk="false"?diskPersistent="false"/>
    ????
    <cache?name="com.ouou.webapp.util.OuouMethodIntecepter"?maxElementsInMemory="100000"?
    ????eternal
    ="false"?timeToIdleSeconds="600"?timeToLiveSeconds="600"?overflowToDisk="false"
    ????diskPersistent
    ="false"/>
    ????
    <cache?name="bbcode"?maxElementsInMemory="100000"?eternal="false"?timeToIdleSeconds="600"
    ????timeToLiveSeconds
    ="600"?
    ????overflowToDisk
    ="false"?diskPersistent="false"/>
    ????
    <cache?name="com.ouou.model.Videos"?maxElementsInMemory="10000"??eternal="false"?
    ????overflowToDisk
    ="false"?timeToIdleSeconds="120"?timeToLiveSeconds="120"?diskPersistent="false"/>
    ????
    <cache?name="com.ouou.model.Tags"?maxElementsInMemory="10000"??eternal="false"
    ????overflowToDisk
    ="false"?timeToIdleSeconds="120"?timeToLiveSeconds="120"?diskPersistent="false"/>
    </ehcache>


    以com.ouou.model.Videos為例子
    在Videos.hbm.xml中配置:
    <class name="Videos" table="TEST" lazy="false">
    ? <cache usage="read-write" region="ehcache.xml中的name的屬性值"/>注意:這一句需要緊跟在class標簽下面,其他位置無效。
    hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的屬性值",則使用name名為com.ouou.model.Videos的cache,
    如果不存在與類名匹配的cache名稱,則用defaultCache。
    如果Videos包含set集合,則需要另行指定其cache
    例如Videos包含Tags集合,則需要
    添加如下配置到ehcache.xml中
    <cache name="com.ouou.model.Tags"
    ??????? maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
    ??????? timeToLiveSeconds="120" overflowToDisk="false" />
    另,針對查詢緩存的配置如下:
    <cache name="org.hibernate.cache.UpdateTimestampsCache"
    ??????? maxElementsInMemory="5000"
    ??????? eternal="true"
    ??????? overflowToDisk="true"/>
    <cache name="org.hibernate.cache.StandardQueryCache"
    ??????? maxElementsInMemory="10000"
    ??????? eternal="false"
    ??????? timeToLiveSeconds="120"
    ??????? overflowToDisk="true"/>

    3、 選擇緩存策略依據:

    <cache? usage="transactional|read-write|nonstrict-read-write|read-only" (1)/>
    ehcache不支持transactional,其他三種可以支持。
    read-only:無需修改, 那么就可以對其進行只讀 緩存,注意,在此策略下,如果直接修改數據庫,即使能夠看到前臺顯示效果,
    但是將對象修改至cache中會報error,cache不會發生作用。另:刪除記錄會報錯,因為不能在read-only模式的對象從cache中刪除。
    read-write:需要更新數據,那么使用讀/寫緩存 比較合適,前提:數據庫不可以為serializable transaction isolation level
    (序列化事務隔離級別)
    nonstrict-read-write:只偶爾需要更新數據(也就是說,兩個事務同時更新同一記錄的情況很不常見),也不需要十分嚴格的事務隔離,
    那么比較適合使用非嚴格讀/寫緩存策略。

    4、 調試時候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作過程,主要用于調試過程,實際應用發布時候,請注釋掉,以免影響性能。

    5、 使用ehcache,打印sql語句是正常的,因為query cache設置為true將會創建兩個緩存區域:一個用于保存查詢結果集 (
    org.hibernate.cache.StandardQueryCache);另一個則用于保存最近查詢的一系列表的時間戳(org.hibernate.cache.UpdateTimestampsCache)。
    請注意:在查詢緩存中,它并不緩存結果集中所包含的實體的確切狀態;它只緩存這些實體的標識符屬性的值、以及各值類型的結果。
    需要將打印sql語句與最近的cache內容相比較,將不同之處修改到cache中,所以查詢緩存通常會和二級緩存一起使用。


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲区视频在线观看| 久久精品亚洲视频| 亚洲av无码av在线播放| a毛片基地免费全部视频| 亚洲女人影院想要爱| 亚洲精品视频免费看| 亚洲国产精品综合久久20| 成年美女黄网站色大免费视频| 最新亚洲精品国偷自产在线| 香蕉视频在线观看免费国产婷婷| 亚洲国产无线乱码在线观看| 免费观看四虎精品国产永久| 久久WWW免费人成—看片| 亚洲处破女AV日韩精品| 亚洲免费福利视频| 亚洲日本中文字幕天天更新| 亚洲国产一区视频| 免费福利在线视频| 激情综合亚洲色婷婷五月 | 国产日韩在线视频免费播放| 亚洲熟妇av一区二区三区| 久久精品免费观看国产| 国产成人精品日本亚洲11| 免费a级毛片18以上观看精品| 两个人看的www免费视频| 亚洲性一级理论片在线观看| 在线jyzzjyzz免费视频| 本道天堂成在人线av无码免费| 国产亚洲AV无码AV男人的天堂| 69式国产真人免费视频| 无码天堂va亚洲va在线va| 亚洲AV无码专区亚洲AV伊甸园| 无码国产精品久久一区免费| 一级黄色免费大片| 亚洲网址在线观看| 亚洲国产成人久久笫一页| 91久久成人免费| 日本精品久久久久久久久免费| 久久久久亚洲AV无码永不| 免费吃奶摸下激烈视频| 91在线手机精品免费观看|