Spring 整合 Hibernate 时启用二级缓存实例详解
2023-09-14 09:08:26 时间
写在前面:
1. 本例使用 Hibernate3 + Spring3;
2. 本例的查询使用了 HibernateTemplate;
1. 导入 ehcache-x.x.x.jar 包;
2. 在 applicationContext.xml 文件中找到 sessionFactory 相应的配置信息并在设置 hibernateProperties 中添加如下代码:
1
2
3
4
5
6
|
<!-- 配置使用查询缓存 --> < prop key = "hibernate.cache.use_query_cache" >true</ prop > <!-- 配置启用二级缓存 --> < prop key = "hibernate.cache.use_second_level_cache" >true</ prop > <!-- 配置二级缓存的提供商 --> < prop key = "hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</ prop > |
3. 由于查询使用了 hibernateTemplate,所以还要在 hibernateTemplate 中做相应配置,找到 hibernateTemplate 的配置项,添加如下代码:
1
2
3
4
|
<!-- 使用查询缓存 --> < property name = "cacheQueries" > < value >true</ value > </ property > |
4. 在要缓存的实体类中加入如下注解:
1
|
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) |
注:
usage 可以有以下几个取值:
- CacheConcurrencyStrategy.NONE:不使用缓存,默认;
- CacheConcurrencyStrategy.READ_ONLY:只读模式,若对缓存的数据进行修改操作会抛出异常;
- CacheConcurrencyStrategy.NONSTRICT_READ_WRITE:不严格的读写模式,不会对缓存的数据加锁;
- CacheConcurrencyStrategy.READ_WRITE:读写模式,在更新缓存的时候会把缓存里面的数据换成一个锁,其它事务如果去取相应的缓存数据,发现被锁了,直接就去数据库查询;
- CacheConcurrencyStrategy.TRANSACTIONAL:事务模式,支持事务,当事务发生回滚时,缓存中的数据也回滚,只支持 JPA 。
5. 配置 ehcache.xml 文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<ehcache> <!-- 指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个目录下 --> <diskStore path= "java.io.tmpdir" /> <!-- name 设置缓存的名字,他的取值为类的完整名字或者类的集合的名字; maxElementsInMemory 设置基于内存的缓存可存放的对象的最大数目 eternal 如果为 true ,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds,默认为 false ; timeToIdleSeconds 设定允许对象处于空闲状态的最长时间,以秒为单位; timeToLiveSeconds 设定对象允许存在于缓存中的最长时间,以秒为单位; overflowToDisk 如果为 true ,表示当基于内存的缓存中的对象数目达到maxElementsInMemory界限,会把溢出的对象写到基于硬盘的缓存中; --> <!-- 设置缓存的默认数据过期策略 --> <defaultCache maxElementsInMemory= "1000" eternal= "false" timeToIdleSeconds= "1200" timeToLiveSeconds= "1200" overflowToDisk= "false" /> <!-- 设定具体的第二级缓存的数据过期策略 --> <cache name= "com.shawearn.model.User" maxElementsInMemory= "1000" eternal= "false" timeToIdleSeconds= "3000" timeToLiveSeconds= "3000" overflowToDisk= "false" /> </ehcache> |
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
- spring循环依赖-不仅仅是八股文
- Spring batch教程 之 spring batch简介
- 「漏洞分析」 CVE-2022-22965-Spring环境搭建及漏洞分析
- Spring+MyBatis实例详解「建议收藏」
- Spring中bean的生命周期(最详细)
- 详解Java中高级程序员必须要掌握的Spring Aop(上篇)
- Spring笔记
- Spring启动过程中实例化部分代码的分析(Bean的推断构造方法)
- Spring Boot整合Mybatis Plus[极简教程]
- spring任务调度scheduled_golang 任务调度
- Java框架Spring入门-第一个spring项目
- Spring Boot3.0升级,踩坑之旅,附解决方案(二)
- 如何定制化Spring Boot Starter,这次我终于学会了
- ldap客户端 spring boot ldap
- Spring Cloud Security的核心组件-Cloud OAuth2 Client
- Spring IOC 入门简介【自定义容器实例】
- windows环境下Redis+Spring缓存实例讲解
- Spring错误:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.B详解编程语言
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例详解编程语言
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- Spring Boot(十三):spring boot小技巧详解编程语言
- java spring boot @bean的用法详解编程语言
- Spring实例化Bean的三种方法
- Spring JDK动态代理(附带实例)
- Spring CGLlB动态代理(附带实例)
- Spring MVC数据绑定和表单标签的应用(附带实例)
- Spring MVC单文件上传(附带实例)
- spring架构利器:JFinal MySQL Spring(jfinalmysql)
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase