ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring – 通过ehcache与Terracotta一起休眠二级缓存 – 根本不缓存?

2019-06-30 06:06:50  阅读:244  来源: 互联网

标签:spring hibernate ehcache terracotta


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

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

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

        <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%.

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

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

解决方法:

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

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

使用此(注意没有.cache.)

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

代替

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

>查询未被缓存,因为我没有使用“.setCachable(true)”作为负责列出/加载实体的方法.

标签:spring,hibernate,ehcache,terracotta
来源: https://codeday.me/bug/20190630/1333634.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有