带有Hibernate二级缓存的Spring Boot

时间:2018-04-05 16:06:40

标签: java spring hibernate spring-boot ehcache

我尝试使用二级缓存,所以我放入了我的pom.xml

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.2.2.Final</version>
    </dependency>

在我的application.properties

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

我把hibernate生成统计数据

spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate.stat=DEBUG

在我的实体中,我把

@Entity(name = "natureza")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Natureza extends AbstractEntity{
...
}

但我怎么能看到我的缓存是否真的有效。

我的hibernate记录了这个:

2018-04-05 12:56:44.954 DEBUG 33410 --- [nio-8080-exec-4] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 42ms, rows: 21 

2018-04-05 12:57:21.470 DEBUG 33410 --- [nio-8080-exec-7] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 21ms, rows: 21

2018-04-05 12:57:40.938 DEBUG 33410 --- [nio-8080-exec-9] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 27ms, rows: 21

2018-04-05 12:58:35.305 DEBUG 33410 --- [nio-8080-exec-2] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 18ms, rows: 21

2018-04-05 12:59:04.187 DEBUG 33410 --- [nio-8080-exec-9] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 14ms, rows: 21

然后我从我的app和hibernate日志中删除缓存:

2018-04-05 13:02:38.883 DEBUG 34669 --- [nio-8080-exec-3] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 50ms, rows: 21

2018-04-05 13:02:58.744 DEBUG 34669 --- [nio-8080-exec-6] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 23ms, rows: 21

2018-04-05 13:03:13.570 DEBUG 34669 --- [nio-8080-exec-4] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 12ms, rows: 21

2018-04-05 13:03:32.779 DEBUG 34669 --- [nio-8080-exec-4] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 11ms, rows: 21

2018-04-05 13:03:59.771 DEBUG 34669 --- [nio-8080-exec-4] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000117: HQL: SELECT * FROM natureza WHERE natureza_tipo_id = ?, time: 7ms, rows: 21

因此,在启用缓存的情况下,hibernate会更慢:D

可以验证休眠是否正确创建缓存?怎么样? TKS

2 个答案:

答案 0 :(得分:0)

您必须将ehcache映射到&#34; ehcache.xml&#34;文件(资源文件夹)

@click

信息: http://www.ehcache.org/documentation/2.7/integrations/hibernate.html

如果您没有在ehcache中映射实体,那么您没有使用它,这就是您在日志中看到该时间的原因。

答案 1 :(得分:0)

通过在记录器中记录“ org.hibernate.cache”类别。