今天看了公司的项目,使用了hibernate开源框架,发现项目中好多配置数据(黑名单等)都是保存在static变量中,个人认为可使用hibernate的二级缓存来存储!
启用二级缓存步骤简介如下:
一、在hibernate.cfg.xml中启用二级缓存(参阅%HIBERNATE_HOME%\project\hibernate-ehcache\src\test\resources\hibernate-config\hibernate.cfg.xml配置文件)
<property name="hibernate.cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
备注:从以上配置中看出是使用EHCache进行缓存管理,需将ehcache.xml放置到class路径中并导入jar包hibernate-ehcache-4.1.7.Final.jar、ehcache-core-2.4.3.jar、slf4j-api-1.6.1.jar,本次测试用的hibernate版本是hibernate-core-4.1.7.Final.jar
二、指定需要缓存的实体对象(entity.BlackList)
在BlackList.hbm.xml中加入如下配置
<cache usage="read-only"/>
在hibernate.cfg.xml中也可指定需要缓存的实体对象
<class-cache usage="read-only" class="entity.Blacklist"/>
在ehcache.xml中也可指定需要缓存的实体对象
<!-- 库中黑名单有563条记录,所以maxElementsInMemory指定为563 -->
<cache name="entity.Blacklist"
maxElementsInMemory="563"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
可以看出在ehcache.xml中配置灵活性比较好!
补充:使用session.getSessionFactory().getCache().evictXXX()可清空指定的缓存
Hibernate+EhCache配置二级缓存
Hibernate4.1.4配置二级缓存EHCache步骤
张卫_Struts2.3.4_Hibernate4.1.4_Spring3.1.1视频教程源码、笔记下载
在Spring、Hibernate中使用Ehcache缓存
Ehcache 整合Spring 使用页面、对象缓存
分享到:
相关推荐
-- 启用二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true <!-- 查询的二级缓存配置 --> <property name="hibernate.cache.use_query_cache">true ...
它可以使用不同的缓存实现,如 EhCache 、 JBossCache 、 OsCache 等 (二级缓存是缓存实体对象的) 还有一个类型的 CACHE 就是 QueryCache . 它的作用就是缓存一个 Query 以及 Query 返回对象的 Identifier 以及...
Spring 整合 Hibernate 时启用二级缓存实例详解 写在前面: 1. 本例使用 Hibernate3 + Spring3; 2. 本例的查询使用了 HibernateTemplate; 1. 导入 ehcache-x.x.x.jar 包; 2. 在 applicationContext.xml ...
非常实用的一个例子,有关于缓存对象 list 或缓存地址或jsp或其它页面,在实例中都有,须望可以帮到大家
第一步:修改Hibernate配置文件,启用Hibernate的第二级缓存.在配置文件中加入EhCache缓存插件的实现类. 第二步:拷贝ehcache.xml文件到类路径下(位于Hibernate3的etc文件夹下) 第三步:对需要进行缓存的...
13.4.5 控制二级高速缓存 13.5 小结 第14章 利用HQL和JPA QL查询 14.1 创建和运行查询 14.1.1 准备查询 14.1.2 执行查询 14.1.3 使用具名查询 14.2 基本的HQL和JPA QL查询 14.2.1...
2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4.3.1. 访问类型 2.4.3.2. ...
2.2.7. 映射二级表(secondary tables) 2.3. 映射查询 2.3.1. 映射EJBQL/HQL查询 2.3.2. 映射本地化查询 2.4. Hibernate独有的注解扩展 2.4.1. 实体 2.4.2. 标识符 2.4.3. 属性 2.4.3.1. 访问类型 2.4.3.2. 公式 2.4....
认为应该针对那些最高级、最完善的浏览器来开发网站。而将那些被认为“过时”或有功能缺失的浏览器下的测试工作安排在开发周期的最后阶段 渐进增强 一开始只构建站点的最少特性,然后不断针对各浏览器追加功能。先让...
99、说下Hibernate的缓存机制 22 100、Hibernate的查询方式 23 101、如何优化Hibernate? 23 102、Struts工作机制?为什么要使用Struts? 23 103、Struts的validate框架是如何验证的? 24 104、说下Struts的设计模式...
99、说下Hibernate的缓存机制 22 100、Hibernate的查询方式 23 101、如何优化Hibernate? 23 102、Struts工作机制?为什么要使用Struts? 23 103、Struts的validate框架是如何验证的? 24 104、说下Struts的设计模式...