ehcache二级缓存优化
来源:互联网 发布:淘宝怎么申请小号 编辑:程序博客网 时间:2024/05/19 17:25
ehcache二级缓存优化
系统架构采用ssh,目前系统出现页面需要加载数据量大,需要查询的表多的情况。chrome性能测试,大量的时间花费在数据查询上。
ssh架构直接默认使用一级缓存。对部分基础表(变化不经常变的表)等采用二级缓存,减少数据库的查询次数。
1. ehcahe介绍
EHCache:EHCache 是一个快速的、轻量级的、易于使用的、进程内的缓存。它支持 read-only 和 read/write 缓存,内存和磁盘缓存。是一个非常轻量级的缓存实现,而且从 1.2 之后就支持了集群,即分布式。
具体内容参考ehcache官网或参考文档。
1.1 EhCache是什么
EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力;2.EhCache的使用注意点
当用Hibernate的方式修改表数据(save,update,delete等等),这时EhCache会自动把缓存中关于此表的所有缓存全部删除掉(这样能达到同步)。但对于数据经常修改的表来说,可能就失去缓存的意义了(不能减轻数据库压力);3.EhCache使用的场合
3.1比较少更新表数据
EhCache一般要使用在比较少执行write操作的表(包括update,insert,delete等)[Hibernate的二级缓存也都是这样];
3.2对并发要求不是很严格的情况
两台机子中的缓存是不能实时同步的;
2. ehcache与ssh集成使用
2.1 添加EhCache
首先设置EhCache,导入ehcache.jar,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="java.io.tmpdir" /> <!-- Mandatory Default Cache configuration. These settings will be applied to caches created programmtically using CacheManager.add(String cacheName) --> <!-- name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。 仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 maxElementsOnDisk:硬盘最大缓存个数。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /></ehcache>
2.2 与spring集成
在spring配置文件中加入:
<prop key="hibernate.cache.provider_class">${hibernate.system.cache.provider_class}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.system.cache.use_query_cache}</prop>
如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);
Query q = getCurrentSession().createQuery(hql); q.setCacheable(true);
或者:
public List selectHql(String hql) { getHibernateTemplate().setCacheQueries(true); return getHibernateTemplate().find(hql); }
2.3 添加cache表
在需要缓存的表对应的Hbm文件中添加<cache usage=”read-only”/>
3. 测试
如果添加成功,在action的方法中,多次查询同一个表,只会出现一次sql查询语句。
4. 参考文档:
EHCache 简介:
http://apps.hi.baidu.com/share/detail/7491847
http://wangjicn.cn/data/read/9082403332378.html
http://blog.csdn.net/mgoann/archive/2009/04/16/4083179.aspx
http://yuanyong.javaeye.com/blog/691499
Spring 整合 EHCache :
http://wangjicn.cn/data/read/909291257438.html
http://www.yybean.com/ehcache-getting-started-series-5-a-distributed-cache-clus
ter-environment-configuration
http://zhyt710.javaeye.com/blog/333213
http://tech.ddvip.com/2010-04/1270187299149502.html
http://blog.csdn.net/goodboylllll/archive/2010/04/01/5442329.aspx
- ehcache二级缓存优化
- 二级缓存ehcache
- hibernate + ehcache 二级缓存技术
- Hibernate+ehcache二级缓存技术
- Hibernate+ehcache二级缓存技术
- Hibernate+ehcache二级缓存技术
- Hibernate ehcache二级缓存技术
- Hibernate+ehcache二级缓存技术
- Hibernate+ehcache二级缓存技术
- Hibernate使用EHCache二级缓存
- Hibernate+ehcache二级缓存技术
- Hibernate+ehcache二级缓存配置
- Hibernate+ehcache二级缓存技术
- Hibernate ehcache配置二级缓存
- hibernate + ehcache 二级缓存技术
- Hibernate+ehcache二级缓存技术
- EhCache二级缓存技术
- SSH ehcache二级缓存配置
- 多线程的那点儿事(之顺序锁) 读写锁的升级
- 基于Media Foundation获取音频视频输入设备列表
- 数组,指针sizeof
- <LeetCode><Easy> 205 Isomorphic Strings --HashTable
- 设计模式六大原则
- ehcache二级缓存优化
- 矩阵特征值分解与奇异值分解含义解析及应用
- Mybatis高级特性学习(一)
- C++ 中关于i++和++i的解析
- 声呐测绘介绍
- 基于DirectShow获取音频视频输入设备列表
- 这是我的第一篇博客
- JSP里的9大内置对象,背过,应聘笔试会用。
- 工作日志15.10.15