首页 文章

使用Terracotta通过ehcache暂停二级缓存 - 根本不缓存?

提问于
浏览
4

我有这个spring / hibernate项目,我试图通过ehcache和terracotta添加二级缓存来休眠 . 一切似乎都很好,我甚至可以在terracota控制台中看到我试图缓存的实体的条目 . 但根据统计数据和数据库记录,根本没有缓存!

负载命中率为0%,负载统计也为0 . 我做错了什么?

这是我做的,我通过maven添加了所需的 jar .

<dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache-core</artifactId>
                <version>2.5.2</version>
        </dependency>
        <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache-terracotta</artifactId>
                <version>2.5.2</version>
        </dependency>
        <dependency>
                <groupId>org.terracotta</groupId>
                <artifactId>terracotta-toolkit-1.5-runtime</artifactId>
                <version>4.2.0</version>
        </dependency>

更改了我的hibernate属性以启用二级缓存

<property name="hibernateProperties">
            <props>
                ...
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_structured_entries">true</prop>
                <prop key="hibernate.cache.generate_statistics">true</prop>
            </props>
        </property>

将@Cache批注添加到我的测试实体

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User implements java.io.Serializable
 {
...
}

这是我非常简单的ehcache.xml(我也试过为我的实体设置一个具有相同结果的缓存条目)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache >    
    <defaultCache  
        maxElementsInMemory="10000"   
        eternal="false" 
        maxEntriesLocalHeap="10"         
        timeToIdleSeconds="120"         
        timeToLiveSeconds="120">        
        <terracotta/>    
    </defaultCache>        
    <terracottaConfig         
        url="localhost:9510"/>
</ehcache>

在我启动我的兵马俑服务器并运行我的测试代码之后

@Test
    @Transactional
    @Rollback(false)
    public void testCache() {
        long start = System.currentTimeMillis();
        List<User> list = userRepository.listAll(0, 100);
        long end = System.currentTimeMillis();
        log.info("Total time "+(end-start));
        assertNotNull(list);
        assertThat(list.size(), is(100));

        for (int i=0; i<100; i++) {
            long start2 = System.currentTimeMillis();
            list = userRepository.listAll(0, 100);
            long end2 = System.currentTimeMillis();
            log.info("Total time 2 "+(end2-start2));
        }
        assertNotNull(list);
        assertThat(list.size(), is(100));
    }

我的日志显示了不应发生的100 SQL . 它还显示命中率为0% .

以下是我的测试运行时来自兵马俑控制台的一些屏幕截图 .

为了让这个工作起作用,我需要的最后一件是什么?

2nd level cache statistics

ehcache overview

Entity statistics

1 回答

  • 3

    找到了我正在试验的问题的解决方案,以下是详细信息:

    • 由于hibernate属性中的键错误,统计信息未设置

    使用此(注意没有 .cache.

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

    代替

    <prop key="hibernate.cache.generate_statistics">true</prop>
    
    • 查询未被缓存,因为我没有使用".setCachable(true)"来负责列出/加载实体的方法 .

相关问题