簡體   English   中英

如何驗證我的ehcache配置是否正常工作?

[英]How to verify my ehcache configuration is working properly?

我正在嘗試驗證我的ehcache配置是否正常工作。 所以我把這段代碼放在select之前和之后。 我的應用程序執行同一行的所有選擇...我使用本教程完成了所有配置。 https://balamaci.wordpress.com/2009/12/07/caching-with-ehcache-part-i/

String msg = "select blockIscsi: " + storage.getStorageIndex();
System.out.println(msg);
blockIscsi = blockIscsiDAO.getByKey(storage.getStorageIndex(), Long.valueOf(storage.getPartitionId()));
System.out.println("done!");


select blockIscsi: 757
Hibernate: select this_.STORAGEINDEX as STORAGEI1_32_0_, this_.PARTITIONID as PARTITIO2_32_0_, this_.BLOCK_STATUS as BLOCK3_32_0_, this_.BLOCK_TYPE as BLOCK4_32_0_, this_.USTORE_ID as USTORE5_32_0_ from blocks this_ where this_.STORAGEINDEX=? and this_.PARTITIONID=?
done!
select blockIscsi: 757
Hibernate: select this_.STORAGEINDEX as STORAGEI1_32_0_, this_.PARTITIONID as PARTITIO2_32_0_, this_.BLOCK_STATUS as BLOCK3_32_0_, this_.BLOCK_TYPE as BLOCK4_32_0_, this_.USTORE_ID as USTORE5_32_0_ from blocks this_ where this_.STORAGEINDEX=? and this_.PARTITIONID=?
done!
select blockIscsi: 757
Hibernate: select this_.STORAGEINDEX as STORAGEI1_32_0_, this_.PARTITIONID as PARTITIO2_32_0_, this_.BLOCK_STATUS as BLOCK3_32_0_, this_.BLOCK_TYPE as BLOCK4_32_0_, this_.USTORE_ID as USTORE5_32_0_ from blocks this_ where this_.STORAGEINDEX=? and this_.PARTITIONID=?
done!

編輯:appContext.xml:

<property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
        <prop key="hibernate.show_sql">false</prop>
        <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
        <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
        <!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory 
            </prop> -->
        <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
        </prop>
        <!-- enable second level cache and query cache -->
        <prop key="hibernate.cache.use_second_level_cache">true</prop>
        <prop key="hibernate.cache.use_query_cache">false</prop>
        <prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>

        <prop key="hibernate.jdbc.batch_size">20</prop>
        <prop key="hibernate.jdbc.fetch_size">25</prop>
        <prop key="hibernate.order_inserts">true</prop>
        <prop key="hibernate.order_updates">true</prop>
        <prop key="hibernate.jdbc.batch_versioned_data">true</prop>
        <prop key="hibernate.hbm2ddl.auto">none</prop>

        <prop key="hibernate.c3p0.min_size">2</prop>
        <prop key="hibernate.c3p0.max_size">100</prop>
        <prop key="hibernate.c3p0.timeout">100</prop>
        <prop key="hibernate.c3p0.max_statements">0</prop>
        <prop key="hibernate.c3p0.maxIdle">-1</prop>
        <prop key="hibernate.c3p0.idle_test_period">100</prop>
        <prop key="hibernate.c3p0.acquire_increment">1</prop>
        <prop key="hibernate.c3p0.unreturnedConnectionTimeout">30</prop>
        <prop key="hibernate.c3p0.debugUnreturnedConnectionStackTraces">false</prop>
    </props>
</property>

ehcache.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">

    <diskStore path="user.dir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
        timeToIdleSeconds="0" timeToLiveSeconds="1800" diskSpoolBufferSizeMB="80"
        maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>

    <cache name="blockiscsi" maxEntriesLocalHeap="10000" eternal="false"
        timeToIdleSeconds="5" timeToLiveSeconds="10">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.internal.StandardQueryCache"
        maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxEntriesLocalHeap="5000" eternal="true">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>

DAO:

public BlockIscsi getByKey(Long id, Long partitionId) {
Session session = null;
BlockIscsi blockIscsi = null;
try {
    session = currentSession();

    Criteria criteria = session.createCriteria(BlockIscsi.class);
    criteria.add(Restrictions.eq("id", id));
    criteria.add(Restrictions.eq("partitionId", partitionId));
    criteria.setCacheable(true);
    criteria.setCacheRegion("query.blockiscsi");

    blockIscsi = (BlockIscsi) criteria.uniqueResult();

} catch (GenericJDBCException e) {
    e.printStackTrace();
} catch (NullPointerException e) {
    e.printStackTrace();
} finally {
    session.close();
}
return blockIscsi;
}

您已禁用查詢緩存:

<prop key="hibernate.cache.use_query_cache">false</prop>

這意味着單個實體會被緩存(因為您的二級緩存已啟用),但是自定義查詢的結果(因為您正在執行Criteria查詢,而不是Session.get() )不會被緩存。 因此,Hibernate必須轉到數據庫並執行查詢。

您需要同時啟用查詢緩存(以記住查詢返回的實體的標識符)和二級緩存(以記住實體本身)。

有關更多信息,請參閱Hibernate文檔: https//docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch06.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM