文档章节

Hibernate ehcache缓存配置详解+实战

北有风雪
 北有风雪
发布于 2017/02/16 13:49
字数 2272
阅读 6
收藏 1

依赖管理

我这里使用的maven来管理的依赖,pom如下:

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

开始配置缓存

使用ehcache需要配置文件:ehcache.xml 并修改hibernate的配置

ehcache.xml配置详解

配置文件如下:

<ehcache>  
  <!--   指定二级缓存存放在磁盘上的位置,可以使用磁盘目录,也可以使用Java System Property目录,user.home是用户目录、user.dir是用户当前工作目录、java.io.tmpdir是默认临时文件路径   -->
   <diskStore path="java.io.tmpdir/cache"/>

  <defaultCache  maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />

  <!--可以给每个实体类指定一个配置文件,通过name属性指定,要使用类的全名-->
    <cache name="com.teemo.entity.User" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />

    <cache name="sample" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" />
</ehcache>

cache字段说明:

  1. name:Cache的唯一标识。
  2. maxElementsInMemory:内存中最大缓存对象数。
  3. eternal:Element是否永久有效,一旦设置true,timeout将不起作用。
  4. timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
  5. timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。
  6. overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。
  7. maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。
  8. memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略
  9. 去清理缓存中的内容。默认策略是LRU(最近最少使用),你也可以设置为FIFO(先进先出)或是LFU(较少使用)

Hibernate二级缓存逻辑:

会先根据entity的region到ehcache的缓存区域查找缓存,region对应的是ehcache配置中的name字段,如果找不到,会使用entity的全类名来查找缓存,如果还找不到会使用default设置

Hibernate缓存设置

xml配置如下:

<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 开启查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 高速缓存提供程序 -->
<!-- Hibernate4.0 以前使用该设置<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>-->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>

实体类注解:

@Cache(region = "sample", usage = CacheConcurrencyStrategy.READ_WRITE)
public class User {
    @Cache(region = "all", usage = CacheConcurrencyStrategy.READ_WRITE)
    private Set<Role> roles = new HashSet<Role>();
}

usage提供的事务隔离级别有NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL,但是ehcache不支持TRANSACTIONAL。

使用并测试缓存效果

二级缓存适用于以下数据:

  1. 很少被修改,大量查询的
  2. 不是很重要的数据,允许出现偶尔并发访问的

Hibernate的缓存模式

Hibernate的二级缓存只会根据ID进行缓存,也就是使用get/load方法会将查询对象缓存,如果需要对HQL或SQL进行缓存,则需要在代码中指定setCacheable(true)
查询缓存所缓存的key值就是查询所使用的HQL或SQL语句,需要注意的是:查询缓存不仅要求所使用的HQL语句、SQL语句相同,甚至是要求所传入的参数也相同,Hibernate才能从缓存中查去数据。

1、get方法缓存测试

测试代码:

    /** * service.get方法直接使用Hibernate Session.get(class, id) */
    @Test
    public void testGet() {
        DynamicProperty dp1 = service.get(1L);
        System.out.println(dp1);
        DynamicProperty dp2 = service.get(1L);
        System.out.println(dp2);
    }

输出结果:

Hibernate: select dynamicpro0_.id as id1_1_0_, dynamicpro0_.author as author2_1_0_(省略)from dynamic_property dynamicpro0_ where dynamicpro0_.id=?
com.teemo.entity.DynamicProperty@5147788d
com.teemo.entity.DynamicProperty@50ab025b

从结果中可以看到,第二次查询命中缓存,没有发出SQL查询。

2、根据字段查询,不指定setCacheable(true)

测试代码:

    /** * service.get方法使用Query query = getSession().createQuery(hql); * query.list来返回结果,没有指定query的setCacheable */
    @Test
    public void testGetByProperty() {
        String key = "System.Version";
        DynamicProperty dp1 = service.get("dynamicPropertyKey", key);
        System.out.println(dp1);
        DynamicProperty dp2 = service.get("dynamicPropertyKey", key);
        System.out.println(dp2);
    }

输出结果:

Hibernate: select dynamicpro0_.id as id1_1_, dynamicpro0_.author as author2_1_(省略)from dynamic_property dynamicpro0_ where dynamicpro0_.property_key=?
com.teemo.entity.DynamicProperty@5e7e7944

Hibernate: select dynamicpro0_.id as id1_1_, dynamicpro0_.author as author2_1_(省略)from dynamic_property dynamicpro0_ where dynamicpro0_.property_key=?
com.teemo.entity.DynamicProperty@74542394

对于没有指定query.setCacheable(true)的查询,没有使用缓存对象

3、根据字段查询,不指定setCacheable(true),但是主对象包含List/Set

List/Set中的对象,属于查询缓存

测试代码:

    @Test
    public void testGetByEmail() {
        String email = "mayun@alibaba.com";
        User user1 = userService.get("email", email);
        System.out.println(user1);
        User user2 = userService.get("email", email);
        System.out.println(user2);
    }

输出结果:

Hibernate: select user0_.id as id1_6_, user0_.create_time as create_t2_6_(省略)from user user0_ where user0_.email=?

Hibernate: select roles0_.user_id as user_id1_8_0_, roles0_.role_id as role_id2_8_0_(省略)from user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.id where roles0_.user_id=?

Hibernate: select resourcepe0_.role_id as role_id4_5_0_, resourcepe0_.id as id1_5_0_, resourcepe0_.id as id1_5_1_(省略)from role_resource_permission resourcepe0_ where resourcepe0_.role_id=?
com.teemo.entity.User@1e6923ba

Hibernate: select user0_.id as id1_6_, user0_.create_time as create_t2_6_(省略)from user user0_ where user0_.email=?
com.teemo.entity.User@4cd29ffa

从结果中可以看出,在第一次查询User实体时,会出现1+N的情况,先查出User实体,在根据User实体查出关联Role,但是此时会把Role的id放到查询缓存中,对象放到二级缓存中,在第二次查询时,发现查询缓存存在这些Role id,那么就会到二级缓存中查询到响应的对象,所以第二次查询没有发出Role相关的SQL。

4、根据字段查询,指定setCacheable(true)

测试代码:

    @Test
    public void testGetByEmail() {
        String email = "mayun@alibaba.com";
        User user1 = userService.get("email", email);
        System.out.println(user1);
        User user2 = userService.get("email", email);
        System.out.println(user2);
    }

输出结果:

2017-01-10 16:21:19.843 [main] DEBUG org.hibernate.cache.internal.StandardQueryCache  - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache
2017-01-10 16:21:19.843 [main] DEBUG org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion  - key: sql: select user0_.id as id1_6_, user0_.create_time as create_t2_6_, user0_.deleted as deleted3_6_, user0_.department_key as departme4_6_, user0_.email as email5_6_, user0_.mobile_phone as mobile_p6_6_, user0_.modify_time as modify_t7_6_, user0_.nickname as nickname8_6_, user0_.password as password9_6_, user0_.salt as salt10_6_, user0_.status as status11_6_, user0_.username as usernam12_6_ from user user0_ where user0_.email=?; parameters: ; named parameters: {email_0=mayun@alibaba.com}; transformer: org.hibernate.transform.CacheableResultTransformer@110f2
2017-01-10 16:21:19.844 [main] DEBUG org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion  - Element for key sql: select user0_.id as id1_6_, user0_.create_time as create_t2_6_, user0_.deleted as deleted3_6_, user0_.department_key as departme4_6_, user0_.email as email5_6_, user0_.mobile_phone as mobile_p6_6_, user0_.modify_time as modify_t7_6_, user0_.nickname as nickname8_6_, user0_.password as password9_6_, user0_.salt as salt10_6_, user0_.status as status11_6_, user0_.username as usernam12_6_ from user user0_ where user0_.email=?; parameters: ; named parameters: {email_0=mayun@alibaba.com}; transformer: org.hibernate.transform.CacheableResultTransformer@110f2 is null
2017-01-10 16:21:19.845 [main] DEBUG org.hibernate.cache.internal.StandardQueryCache  - Query results were not found in cache
Hibernate: select user0_.id as id1_6_, user0_.create_time as create_t2_6_, user0_.deleted as deleted3_6_, user0_.department_key as departme4_6_, user0_.email as email5_6_, user0_.mobile_phone as mobile_p6_6_, user0_.modify_time as modify_t7_6_, user0_.nickname as nickname8_6_, user0_.password as password9_6_, user0_.salt as salt10_6_, user0_.status as status11_6_, user0_.username as usernam12_6_ from user user0_ where user0_.email=?
Hibernate: select roles0_.user_id as user_id1_8_0_, roles0_.role_id as role_id2_8_0_, role1_.id as id1_4_1_, role1_.available as availabl2_4_1_, role1_.description as descript3_4_1_, role1_.role_key as role_key4_4_1_, role1_.role_value as role_val5_4_1_ from user_role roles0_ inner join role role1_ on roles0_.role_id=role1_.id where roles0_.user_id=?
Hibernate: select resourcepe0_.role_id as role_id4_5_0_, resourcepe0_.id as id1_5_0_, resourcepe0_.id as id1_5_1_, resourcepe0_.permission_ids as permissi2_5_1_, resourcepe0_.resource_id as resource3_5_1_, resourcepe0_.role_id as role_id4_5_1_ from role_resource_permission resourcepe0_ where resourcepe0_.role_id=?
2017-01-10 16:21:20.064 [main] DEBUG org.hibernate.cache.internal.StandardQueryCache  - Caching query results in region: org.hibernate.cache.internal.StandardQueryCache; timestamp=6078613420920832
2017-01-10 16:21:20.064 [main] DEBUG org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion  - key: sql: select user0_.id as id1_6_, user0_.create_time as create_t2_6_, user0_.deleted as deleted3_6_, user0_.department_key as departme4_6_, user0_.email as email5_6_, user0_.mobile_phone as mobile_p6_6_, user0_.modify_time as modify_t7_6_, user0_.nickname as nickname8_6_, user0_.password as password9_6_, user0_.salt as salt10_6_, user0_.status as status11_6_, user0_.username as usernam12_6_ from user user0_ where user0_.email=?; parameters: ; named parameters: {email_0=mayun@alibaba.com}; transformer: org.hibernate.transform.CacheableResultTransformer@110f2 value: [6078613420920832, 1]

// 查询出第一个对象,并缓存
com.teemo.entity.User@1a115fb4

2017-01-10 16:21:20.077 [main] DEBUG org.hibernate.cache.internal.StandardQueryCache  - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache
2017-01-10 16:21:20.077 [main] DEBUG org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion  - key: sql: select user0_.id as id1_6_, user0_.create_time as create_t2_6_, user0_.deleted as deleted3_6_, user0_.department_key as departme4_6_, user0_.email as email5_6_, user0_.mobile_phone as mobile_p6_6_, user0_.modify_time as modify_t7_6_, user0_.nickname as nickname8_6_, user0_.password as password9_6_, user0_.salt as salt10_6_, user0_.status as status11_6_, user0_.username as usernam12_6_ from user user0_ where user0_.email=?; parameters: ; named parameters: {email_0=mayun@alibaba.com}; transformer: org.hibernate.transform.CacheableResultTransformer@110f2
2017-01-10 16:21:20.078 [main] DEBUG org.hibernate.cache.internal.StandardQueryCache  - Checking query spaces are up-to-date: [user]
2017-01-10 16:21:20.079 [main] DEBUG org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion  - key: user
2017-01-10 16:21:20.079 [main] DEBUG org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion  - Element for key user is null
2017-01-10 16:21:20.080 [main] DEBUG org.hibernate.cache.internal.StandardQueryCache  - Returning cached query results

// 根据SQL/HQL比对缓存key,命中
com.teemo.entity.User@5b867c31

从结果中看出,在query中设置query.setCacheable(true);之后,所有被标记为可缓存的对象都会被缓存下来,所以第二次查询User时,直接命中二级缓存,没有发出SQL查询。

小结

我觉得,Hibernate的缓存模式应用场景太单一,对于通用的DAO或者底层查询方法来说,如果设置为query.setCacheable(true) 显然不太灵活,这样会缓存所有的调用该方法查询到的数据,如果不设置的话,又只能缓存get/load查询,结合自己的系统优化缓存模式还是很必要的,例如使用AOP进行Service切面缓存,这样就与DAO的ORM级别缓存分开了,相对更灵活。

上面是我对Hibernate和ehcache的一点了解,如果有描述不当或者错误的地方欢迎指正!

© 著作权归作者所有

共有 人打赏支持
北有风雪
粉丝 4
博文 33
码字总数 55091
作品 2
崇明
程序员
私信 提问
Maven搭建SpringMVC+Hibernate项目详解 【转】

今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这篇主要搭建SpringMVC4.1.4和Hiber...

梵蒂冈考虑过
2016/10/19
37
1
Maven搭建SpringMVC+Hibernate项目详解 【转】

。搭建SpringMVC+Hibernate的框架的思路如下: 1、设计数据库:设计好表结构,最好符合3NF,采用Hibernate tools将设计好的表自动生成对应的实体entity。 1、创建Maven项目,按需映入Maven包...

如何让他和
2016/09/30
75
3
Maven搭建SpringMVC+Hibernate项目详解

前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这篇主要搭建SpringMVC4.1.4和H...

Airship
2016/12/12
19
0
Java程序员从笨鸟到菜鸟之(七十七)细谈Hibernate(十九)Hibernate二级缓存详解

与Session相对的是,SessionFactory也提供了相应的缓存机制。SessionFactory缓存可以依据功能和目的的不同而划分为内置缓存和外置缓存。 SessionFactory的内置缓存中存放了映射元数据和预定义...

长平狐
2012/11/12
90
0
在Spring、Hibernate中使用Ehcache缓存

前一篇http://blog.csdn.net/ibmhoojo/article/details/7739181介绍了Ehcache整合Spring缓存,使用页面、对象缓存;这里将介绍在Hibernate中使用查询缓存、一级缓存、二级缓存,整合Spring在...

ibm_hoojo
2012/07/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Java 如何实现线程间通信?

正常情况下,每个子线程完成各自的任务就可以结束了。不过有的时候,我们希望多个线程协同工作来完成某个任务,这时就涉及到了线程间通信了。 本文涉及到的知识点: thread.join(), object....

颖辉小居
17分钟前
1
0
记一次阿里云服务器运行慢排除

公司测试环境用的阿里云服务器+docker部署的,一共跑了14个项目。之前几个月一直OK,最近几天突然很卡很慢。刚开始以为是项目问题,又是扩大内存,又是清减插件,甚至停了一半项目。结果CPU...

李玉长
18分钟前
1
0
统一客服消息返回错误:{"errcode":43004,"errmsg":"require subscribe hint: [9Vv08633952]"}

公众号或者小程序发送客服消息错误: {"errcode":43004,"errmsg":"require subscribe hint: [9Vv08633952]"} 场景:小程序使用公众号的服务消息,推送消息,如果接收人没有关注公众号,就会出...

tianma3798
29分钟前
1
0
Rainbond V5.0 Beta公测公告

Rainbond支撑企业应用的开发、架构、交付和运维的全流程,通过“无侵入”架构无缝衔接各类企业应用,底层资源可以对接和管理IaaS、虚拟机和物理服务器 Rainbond V5.0即日起开启Beta版本公测,...

好雨云帮
47分钟前
2
0
Word Pattern(leetcode290)

Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empt......

woshixin
53分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部