Ehcache似乎不起作用



现在我想使用ehache的JPA二级缓存。我做了一些配置,看起来很有效。但是我仍然可以看到查询sql。我不确定ehcache是否工作。有人知道吗?谢谢

1.some part of persistence.xml
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" /> 
            <property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />   
            <property name="hibernate.generate_statistics" value="true" />  
            <property name="hibernate.cache.use_second_level_cache" value="true" /> 
            <property name="hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.show_sql" value="true" />
2.ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <defaultCache maxElementsInMemory="1" eternal="false" 
           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="true" clearOnFlush="true">
    </defaultCache>
    <cache name="org.test.persistent.entity.Scenario" 
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="3600"
           overflowToDisk="true">
    </cache>
    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" 
        maxElementsInMemory="10000"
        timeToIdleSeconds="1800"
        timeToLiveSeconds="3600"
        eternal="false">
    </cache>
</ehcache>
3. sql
TypedQuery<Scenario> query = em.createQuery(
                    "from Scenario as s where s.obsolete!=1 and s.parentId=? order by s.name, s.scenarioStatusId",
                    Scenario.class);
            query.setParameter(1, parentId);
            query.setHint("org.hibernate.cacheable", true);
            List<Scenario> scenarios = null;
            org.hibernate.Query hbQuery = null;
            if (query instanceof org.hibernate.ejb.QueryImpl) {
                hbQuery = ((org.hibernate.ejb.QueryImpl)query).getHibernateQuery();
                hbQuery.setCacheable(true);
                scenarios = hbQuery.list();
            } else {
                scenarios = query.getResultList();   
            }
Ehcache设置的默认值为maxElementsInMemory="10000"。它必须根据热门歌曲进行调整。

一种方法是,你可以增加maxElementsInMemory,并检查你是否想通过"试错"方法解决这个问题。

另一种方法(可靠的)是,您必须发布Mbeans,并注意JConsole中的缓存使用情况和缓存未命中情况。根据报告,您可以增加"maxElementsInMemory"。请参阅http://hibernate-jcons.sourceforge.net/usage.html

我通过添加"将缓存数据写入磁盘。它似乎运行良好,我在目录中找到了一些数据。但我仍然没有找到它仍然执行sql的原因。

最新更新