为所有实体启用Ehcache

时间:2015-06-30 14:14:55

标签: spring hibernate ehcache

我在Wildfly 8.2上使用Spring Data JPA,Hibernate 4.3.10.Final,Ehcache。我想测试应用程序服务器如何处理缓存中的所有数据。

问题是:默认情况下是否可以为所有扫描的实体启用二级缓存?

实际上,我想避免在每个实体上添加@Cache(因为项目有100 +)。

JPA属性

... data source definition ...
<property name="sharedCacheMode" value="ALL" />

Hibernate属性

<prop key="hibernate.generate_statistics">true</prop>

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>

ehcache.xml中

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

    <defaultCache maxElementsInMemory="1000000000"
                  eternal="true"
                  overflowToDisk="false"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU"
                  statistics="true"
                  >
    </defaultCache>
    <!-- 
    <cache name="org.hibernate.cache.internal.StandardQueryCache"
            eternal="false"
            timeToLiveSeconds="120">
            <persistence strategy="localTempSwap"/>
    </cache> -->
</ehcache>

修改 按照Dragan Bozanovic的建议尝试<prop key="javax.persistence.sharedCache.mode">ALL</prop>,但这并不影响这个过程。

以下是SELECT e FROM MyEntity e执行2次的统计日志:

17:00:00,597 | INFO  | StatisticalLoggingSessionE:275  |  | Session Metrics {
    71157 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    145393 nanoseconds spent preparing 1 JDBC statements;
    386233 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    822075 nanoseconds spent performing 41 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
17:00:00,597 | INFO  | StatisticalLoggingSessionE:275  |  | Session Metrics {
    63973 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    127262 nanoseconds spent preparing 1 JDBC statements;
    282918 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    452598 nanoseconds spent performing 41 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}

返回了41行,这是正确的。但我看到41 L2C puts 2次,不应该41 L2C puts然后是41 L2C hits吗?

1 个答案:

答案 0 :(得分:2)

是的,如上所述here,请添加到persistence.xml

<shared-cache-mode>ALL</shared-cache-mode>

如果您未通过persistence.xml配置Hibernate,请在javax.persistence.sharedCache.mode配置属性中设置所需的值。

但是,您可能希望通过以下方式实现相同的效果:

<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>

这样,如果您以后决定关闭缓存,可以使用@Cacheable(false)注释某些实体。

相关问题