欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
EhCache使用詳細介紹(轉)

Ehcache中不僅可以用配置文件來(lái)配置緩存,而在代碼中也可以實(shí)現同樣的功能。
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache(“testCache”, 50000, false, false, 8, 2);
Cache test = singletonManager.getCache(“testCache”);
刪除只需要調用singletonManager.removeCache(“testCache”);
Shotdown CacheManager
在使用完Ehcache后,必須要shutdown緩存。Ehcache中有自己的關(guān)閉機制,不過(guò)最好在你的代碼中顯示調用CacheManager.getInstance().shutdown();

1.EhCache是什么
    EhCache是Hibernate的二級緩存技術(shù)之一,可以把查詢(xún)出來(lái)的數據存儲在內存或者磁盤(pán),節省下次同樣查詢(xún)語(yǔ)句再次查詢(xún)數據庫,大幅減輕數據庫壓力;

2.EhCache的使用注意點(diǎn)
    當用Hibernate的方式修改表數據(save,update,delete等等),這時(shí)EhCache會(huì )自動(dòng)把緩存中關(guān)于此表的所有緩存全部刪除掉(這樣能達到同步)。但對于數據經(jīng)常修改的表來(lái)說(shuō),可能就失去緩存的意義了(不能減輕數據庫壓力);

3.EhCache使用的場(chǎng)合
    3.1比較少更新表數據
        EhCache一般要使用在比較少執行write操作的表(包括update,insert,delete等)[Hibernate的二級緩存也都是這樣];
    3.2對并發(fā)要求不是很?chē)栏竦那闆r
        兩臺機子中的緩存是不能實(shí)時(shí)同步的;

4.在項目做的實(shí)現
    4.1在工程的src目錄下添加ehcache.xml文件,內容如下:
        <?xml version="1.0" encoding="UTF-8"?>
        <ehcache>    
            <diskStore path="java.io.tmpdir" />
          <defaultCache maxElementsInMemory="5"<!--緩存可以存儲的總記錄量-->
            eternal="false"<!--緩存是否永遠不銷(xiāo)毀-->
            overflowToDisk="true"<!--當緩存中的數據達到最大值時(shí),是否把緩存數據寫(xiě)入磁盤(pán)-->
            timeToIdleSeconds="15"<!--當緩存閑置時(shí)間超過(guò)該值,則緩存自動(dòng)銷(xiāo)毀-->
                timeToLiveSeconds="120"<!--緩存創(chuàng )建之后,到達該緩存自動(dòng)銷(xiāo)毀-->
          />
        </ehcache>
    4.2在Hibernate.cfg.xml中的mapping標簽上面加以下內容:

        <property name="show_sql">true</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
        <property name="hibernate.cache.use_query_cache">true</property>
    4.3在要緩存的bean的hbm.xml文件中的class標簽下加入以下內容:
       <cache usage="read-only" /><!--也可讀寫(xiě)-->
    4.4創(chuàng )建DAO,內容如下:
        Session s = HibernateSessionFactory.getSession();
        Criteria c = s.createCriteria(Xyz.class);
        c.setCacheable(true);//這句必須要有
        System.out.println("第一次讀取");
        List l = c.list();
        System.out.println(l.size());
        HibernateSessionFactory.closeSession();

        s = HibernateSessionFactory.getSession();
        c = s.createCriteria(Xyz.class);
        c.setCacheable(true);//這句必須要有
        System.out.println("第二次讀取");
        l = c.list();
        System.out.println(l.size());
        HibernateSessionFactory.closeSession();
   4.5這時(shí)你會(huì )看到打印出來(lái)的信息為(表示第二次并沒(méi)有去讀庫):
        第一次讀取
        Hibernate: *******
        13
        第二次讀取
        13

配置Spring+hibernate使用ehcache作為second-level cache

大量數據流動(dòng)是web應用性能問(wèn)題常見(jiàn)的原因,而緩存被廣泛的用于優(yōu)化數據庫應用。cache被設計為通過(guò)保存從數據庫里load的數據來(lái)減少應用和數據庫之間的數據流動(dòng)。數據庫訪(fǎng)問(wèn)只有當檢索的數據不在cache里可用時(shí)才必要。hibernate可以用兩種不同的對象緩存:first-level cache 和 second-level cache。first-level cache和Session對象關(guān)聯(lián),而second-level cache是和Session Factory對象關(guān)聯(lián)。

        缺省地,hibernate已經(jīng)使用基于每個(gè)事務(wù)的first-level cache。 Hibernate用first-level cache主要是減少在一個(gè)事務(wù)內的sql查詢(xún)數量。例如,如果一個(gè)對象在同一個(gè)事務(wù)內被修改多次,hibernate將只生成一個(gè)包括所有修改的 UPDATE SQL語(yǔ)句。為了減少數據流動(dòng),second-level cache在Session Factory級的不同事務(wù)之間保持load的對象,這些對象對整個(gè)應用可用,不只是對當前用戶(hù)正在運行的查詢(xún)。這樣,每次查詢(xún)將返回已經(jīng)load在緩存里的對象,避免一個(gè)或更多潛在的數據庫事務(wù)。

下載ehcache,hibernate3.2必須要ehcache1.2以上才能支持??梢孕薷膌og4j配置文件log4j.logger.net.sf.hibernate.cache=debug查看日志

1.在類(lèi)路徑上ehcache.xml:

<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"/>


     <!--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"/>
        
     <!-- See http://ehcache.sourceforge.net/documentation/#mozTocId258426 for how to configure caching for your objects -->
</ehcache>

2.applicationContext-hibernate.xml里Hibernate SessionFactory配置:

     <!-- Hibernate SessionFactory -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="dataSource" ref="dataSource"/>
         <property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property>
         <!-- The property below is commented out b/c it doesn't work when run via
              Ant in Eclipse.   It works fine for individual JUnit tests and in IDEA ??
         <property name="mappingJarLocations">
             <list><value>file:dist/appfuse-dao.jar</value></list>
         </property>
         -->
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">@HIBERNATE-DIALECT@</prop>
                 <!--<prop key="hibernate.show_sql">true</prop>-->
                 <prop key="hibernate.max_fetch_depth">3</prop>
                 <prop key="hibernate.hibernate.use_outer_join">true</prop>
                 <prop key="hibernate.jdbc.batch_size">10</prop>
                 <prop key="hibernate.cache.use_query_cache">true</prop>
                 <prop key="hibernate.cache.use_second_level_cache">true</prop>
                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                 <!--
                 <prop key="hibernate.use_sql_comments">false</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">0</prop> -->
             </props>
         </property>
         <property name="entityInterceptor">
            <ref local="auditLogInterceptor"/>
         </property>
     </bean>
說(shuō)明:如果不設置“查詢(xún)緩存”,那么hibernate只會(huì )緩存使用load()方法獲得的單個(gè)持久化對象,如果想緩存使用findall()、 list()、Iterator()、createCriteria()、createQuery()等方法獲得的數據結果集的話(huà),就需要設置 hibernate.cache.use_query_cache true 才行

3.model類(lèi)里采用Xdoclet生成*.hbm.xml里的cache xml標簽,即<cache usage="read-only"/>

/**
* @hibernate.class table="WF_WORKITEM_HIS"
* @hibernate.cache usage="read-write"
*
*/

4.對于"query cache",需要在程序里編碼:

         getHibernateTemplate().setCacheQueries(true);
         return getHibernateTemplate().find(hql);

 

 

使用spring和hibernate配置ehcache和query cache
1、applicationContext.xml
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>

這兩句加到hibernateProperties中
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
   <ref bean="sessionFactory" />
</property>
<property name="cacheQueries">
   <value>true</value>
</property>
</bean>

添加此bean到applicationcontext.xml中。在各個(gè)DAO的bean中,更改如下
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
改為
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>

2、ehcache.xml文件放在classes根目錄即可

3、pojo與ehcache.xml的配置關(guān)系
以com.ce.ceblog.pojos.CeblogJournal為例子
在CeblogJournal.hbm.xml中配置:
<class name="CeblogJournal" table="CEBLOG_JOURNAL" lazy="false">
<cache usage="read-write" region="ehcache.xml中的name的屬性值"/>
注意:這一句需要緊跟在class標簽下面,其他位置無(wú)效。

Ehcache.xml文件主體如下
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="true" />
<cache name="com.ce.ceblog.pojos.CeblogJournal" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的屬性值",則使用name名為 com.ce.ceblog.pojos.CeblogJournal的cache,如果不存在與類(lèi)名匹配的cache名稱(chēng),則用 defaultCache。
如果CeblogJournal包含set集合,則需要另行指定其cache
例如CeblogJournal包含ceblogReplySet集合,則需要
添加如下配置到ehcache.xml中
<cache name="com.ce.ceblog.pojos.CeblogJournal.ceblogReplySet"
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300"
timeToLiveSeconds="600" overflowToDisk="true" />

另,針對查詢(xún)緩存的配置如下:
<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"/>

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

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

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

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
配置Spring+hibernate使用ehcache作為second-level cache
二級 ehcache
hibernate之緩存使用二
Hibernate一級緩存和二級緩存
EHCache - 單落撒旦的日志 - 網(wǎng)易博客
EHCache 單獨使用
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久