当前位置: 首页 > 知识库问答 >
问题:

Hibernate 2级缓存通过ehcache与Terracotta-根本不缓存?

山森
2023-03-14

我有一个spring/hibernate项目,我试图通过ehcache和terracotta将二级缓存添加到hibernate。一切似乎都很好,我甚至可以在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%。

这里有一些屏幕截图从兵马俑控制台,而我的测试正在运行。

为了让它工作,我需要的最后一块是什么?

共有1个答案

陆曜文
2023-03-14

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

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

请使用此选项(注意没有 .cache。

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

而不是

<prop key="hibernate.cache.generate_statistics">true</prop>
  • 查询没有被缓存,因为我没有对负责列出/加载实体的方法使用“. setCacable(true)”。
 类似资料:
  • 我正在使用ehcache(2.10.1)和terracotta(开源4.3.1)实现分布式缓存。我在单台机器上尝试了两个JVM实例和一个terracotta服务器,代码按预期工作,没有错误。 现在,我尝试运行相同的terracotta服务器,但在虚拟机上使用客户端。在虚拟机上运行客户端时,会收到以下消息和错误: 使用的 tc-config.xml 文件: 使用的 ehcache.xml 文件: 在

  • 主要问题是EHCAHCE2.6.2是否基于ehcache配置驱逐过期元素;还是我必须按程序驱逐? 在阅读了文档的各个部分(数据寿命和缓存大小)之后,我们将当前配置设置为使用TTI/TTL和CacheManager级别MaxBytesLocalHeap=1024M。在设置了这个之后,我希望ehcache只能使用1GB的堆空间进行缓存,但是从测试(和prodcution)中,我们总是看到JVM堆已经满

  • 问题内容: 在我的Spring驱动的应用程序中,我使用Hibernate(4.2.15.Final)和EhCache(2.6.9)作为第二级缓存,在标准的持久层中进行了设置。 一切正常。但是,将条目放入第二级缓存有时会花费一些时间。 我已经在一个显式文件中配置了域模型类的缓存(我没有配置默认缓存): 在持久性上下文启动时,我收到以下INFO消息记录: 以及执行期间的以下警告 AFAIK 必须调整放

  • 当你使用本地(在内存中)缓存时,服务器可以缓存一些信息并快速地检索它,但是其他服务器不能访问这个缓存数据,他们需要到数据库中查询同样的信息。 如果你喜欢使用分布式缓存让其他服务器访问缓存的数据,由于它有一些序列化/反序列化和网络延迟开销,则需要注意:在某些情况下,它可能会降低性能。 缓存需要处理的另一个问题:缓存失效。 There are only two hard things in Compu

  • 问题内容: 一级缓存与hibernate中的查询缓存是否不同?我看过有关第一级和查询缓存的文章,所以我很困惑。 问题答案: 默认情况下,第一级缓存处于启用状态,并且基于每个会话。默认情况下,查询缓存未启用,它在多个会话之间共享,应始终与二级缓存一起使用。 要启用查询缓存,应使用以下属性:

  • 1、一级缓存:指的是mybatis中sqlSession对象的缓存,当我们执行查询以后,查询的结果会同时存入sqlSession中,再次查询的时候,先去sqlSession中查询,有的话直接拿出,当sqlSession消失时,mybatis的一级缓存也就消失了,当调用sqlSession的修改、添加、删除、commit()、close()等方法时,会清空一级缓存。 2、二级缓存:指的是mybati