java - 如何验证我的 ehcache 配置是否正常工作?

标签 java spring hibernate ehcache

我正在尝试验证我的 ehcache 配置是否正常工作。所以我把这段代码放在选择之前和之后。我的应用程序始终对同一行执行选择...我使用本教程完成了所有配置。 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

关于java - 如何验证我的 ehcache 配置是否正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27784982/

相关文章:

java - 如何使用 Spring 将 session bean 注入(inject) POJO

java - Hibernate - 在字段中存储 oneToMany-Relation 中的元素数量? (例如评论数量)

java - JPA @SecondaryTable 外键违规

hibernate - 无论如何,是否要检查域类中的属性是否为主键?

java - 如何通过测试 Java 中指定复杂程度的方法来检查代码覆盖率

java - 从服务器编码 RMI 客户端回调时遇到问题 - UnmarshalException ClassNotFoundException

java - Apache POI HSSF/XSSF 到 XLS/XLSX 的映射

java - Spring @Transactional - 隔离、传播

java - 使用idea编译spring5源码出现错误

java - 您可以通过在后台运行中调用的方法使用发布吗?