1.EhCache是什么
EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力;
2.EhCache的使用注意点
当用Hibernate的方式修改表数据(save,update,delete等等),这时EhCache会自动把缓存中关于此表的所有缓存全部删除掉(这样能达到同步)。但对于数据经常修改的表来说,可能就失去缓存的意义了(不能减轻数据库压力);
3.EhCache使用的场合
3.1比较少更新表数据
EhCache一般要使用在比较少执行write操作的表(包括update,insert,delete等)[Hibernate的二级缓存也都是这样];
3.2对并发要求不是很严格的情况
两台机子中的缓存是不能实时同步的;
4.在项目做的实现
4.1在工程的src目录下添加ehcache.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="5"<!--缓存可以存储的总记录量-->
eternal="false"<!--缓存是否永远不销毁-->
overflowToDisk="true"<!--当缓存中的数据达到最大值时,是否把缓存数据写入磁盘-->
timeToIdleSeconds="15"<!--当缓存闲置时间超过该值,则缓存自动销毁-->
timeToLiveSeconds="120"<!--缓存创建之后,到达该缓存自动销毁-->
/>
</ehcache>
4.2在Hibernate.cfg.xml中的mapping标签上面加以下内容:
<property name="show_sql">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
4.3在要缓存的bean的hbm.xml文件中的class标签下加入以下内容:
<cache usage="read-only" /><!--也可读写-->
4.4创建DAO,内容如下:
Session s = HibernateSessionFactory.getSession();
Criteria c = s.createCriteria(Xyz.class);
c.setCacheable(true);//这句必须要有
System.out.println("第一次读取");
List l = c.list();
System.out.println(l.size());
HibernateSessionFactory.closeSession();
s = HibernateSessionFactory.getSession();
c = s.createCriteria(Xyz.class);
c.setCacheable(true);//这句必须要有
System.out.println("第二次读取");
l = c.list();
System.out.println(l.size());
HibernateSessionFactory.closeSession();
4.5这时你会看到打印出来的信息为(表示第二次并没有去读库):
第一次读取
Hibernate: *******
13
第二次读取
13
分享到:
相关推荐
hibernate+ehcache hibernate+ehcache hibernate+ehcache hibernate+ehcache hibernate+ehcache
使用spring boot 创建的一个 集成了hibernate+shiro+ehcache的项目 分享
Hibernate+ehcache二级缓存技术 Hibernate+ehcache二级缓存技术
自己整合的Spring+Hibernate+Ehcache 整合的项目 需要struts的自己添加。 里面带有注释,而且带Jar.写的很清楚,良心分。
6.Hibernate继承 HibernateDaoSupport。 7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-...
内含Springmvc+hibernate+Ehcache+shior+mysql+Oracle+fastjson,jar包整合,自己的加个jdk1.7便可使用。
spring+springmvc+hibernate+ehcache JavaWeb后台框架,不仅提高了开发程序的速度,且其中还是用到hibernate和ehcache缓存的使用,加快了程序运行的数据,该框架亲测好用。值得注意的是该种框架现在还算是用的比较多...
struts2+hibernate+ehcache二级缓存的 步骤和需要的代码及jar包
ssh,struts+hibernate+spring+ehcache集成
包含jar包:ehcache-core-2.6.8.jar、hibernate-ehcache-4.1.0.Final.jar
maven环境下如何整合spring+hibernate+mysql+ehcache的方法
spring+springMVC+hibernate+cxf+quartz(定时器)+ehcache(缓存)
使用Intellj IDEA 集成开发工具 搭建的maven项目,使用Spring+Hibernate+Shiro+Ehcache集成,完成一个简单的用户角色菜单项目
spring3+hibernate4+struts2+dbcp+mysql+json+ehcache+dom4j 合集包
SpringMVC+Spring4.2+hibernate4.3+shiro1.2.4+ehcache2.0所需jar包,非常有用,你可能会需要。
NULL 博文链接:https://llying.iteye.com/blog/492307
hibernate使用的一些例子,二级缓存ehcache的使用 一些设计模式的demo代码
2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构...
2.Hibernate几个级别缓存对比。见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构...
Spring4MVC+Hibernate4+Freemarker+Ehcache+EasyUi整合,里面包含数据库,数据库使用的是Mysql