ehcache缓存的详细配置
用到缓存,主要是用来解决并发问题的。
其中ehcache是一个纯Java的过程中缓存实现Hibernate2.1,Spring都支持EHcache嵌入。
本文主要写Spring中引入ehcache而不是用hibernate.
ehcache部署起来很简单,主要分两步:
1.首先要给他写个核心配置XML文件
- <ehcache>
- <diskStore path="java.io.tmpdir"/>
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU"
- />
- <cache name="cache1"
- maxElementsInMemory="10000"
- eternal="false" maxElementsOnDisk="1000"
- overflowToDisk="true"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- memoryStoreEvictionPolicy="LFU"
- />
- </ehcache>
属性解释:
简单配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好将其删除掉,自己配置。
缓存名cache1,内存中最多可缓存10000个Element,其中的element会在闲置5分钟或是存活10分钟之后失效。
超过10000element时,element将会输出到磁盘中,输出路径是java.io.tmpdir。
从其他文章找到其详细解释:
· Cache配置
· name:Cache的唯一标识
· maxElementsInMemory:内存中最大缓存对象数。
· maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。
· eternal:Element是否永久有效,一但设置了,timeout将不起作用。
· overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。
· timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
· timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。
· diskPersistent:是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么,有高人还希望能指点一二)。
· diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
· diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
· memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。这里比较遗憾,Ehcache并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想。
2.实际要缓存的类调用
写一个实例类,这样大家就明白差不多了:
- import java.io.Serializable;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import net.sf.ehcache.Element;
- public class Demo {
- static CacheManager manager= new CacheManager();
- /**
- *##############################################################################
- *
- * @DESCRIBE
- * @param args
- * @throws InterruptedException
- *
- *##############################################################################
- */
- public static void main(String[] args) throws InterruptedException {
- String[] cacheNames = manager.getCacheNames();
- System.out.println("读取的缓存列表为:");
- for(int i=0;i<cacheNames.length;i++){
- System.out.println("-- "+(i+1)+" "+cacheNames[i]);
- }
- Cache cache = manager.getCache("cache1");
- Element element = new Element("key1", "value1");
- cache.put(element);
- element = cache.get("key1");
- Serializable value = element.getValue();
- System.out.println("序列化后的值为:"+value.toString());
- element = cache.get("key1");
- Object value1 = element.getObjectValue();
- System.out.println("未序列化的值为:"+value1.toString());
- int elementsInMemory = cache.getSize();
- System.out.println("得到缓存的对象数量:"+elementsInMemory);
- long elementsInMemory1 = cache.getMemoryStoreSize();
- System.out.println("得到缓存对象占用内存的数量:"+elementsInMemory1);
- long elementsInMemory2 = cache.getDiskStoreSize();
- System.out.println("得到缓存对对象占用磁盘的数量:"+elementsInMemory2);
- int hits = cache.getHitCount();
- System.out.println("得到缓存读取的命中次数:"+hits);
- int hits1 = cache.getMemoryStoreHitCount();
- System.out.println("得到内存中缓存读取的命中次数:"+hits1);
- int hits2 =cache.getDiskStoreHitCount();
- System.out.println("得到磁盘中缓存读取的命中次数:"+hits2);
- int hits3 = cache.getMissCountNotFound();
- System.out.println("得到缓存读取的丢失次数:"+hits3);
- int hits4 = cache.getMissCountExpired();
- System.out.println("得到缓存读取的已经被销毁的对象丢失次数:"+hits4);
- }
- }
另:附件里需要的ehcache.jar包
ehcache缓存的详细配置相关推荐
- javaweb项目搭建ehcache缓存系统
转载自 javaweb项目搭建ehcache缓存系统 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider,同时在项目开发中 ...
- Ehcache缓存配置和基本使用
Ehcache在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS中取出来的高花费.高延迟采取的一种缓存方案.正因为Ehcache具有健壮性(基于java开发).被认证(具有apac ...
- mybatis配置ehcache缓存
1:在spring配置文件中加载缓存配置文件 <!-- 使用ehcache缓存 --> <bean id="ehCacheManager" class=" ...
- Ehcache 缓存监控配置
监控 ehcache缓存: 1,下载: http://terracotta.org/downloads/open-source/destination?name=ehcache-monitor-kit ...
- 一文玩转 EhCache 缓存框架!
Ehcache 介绍 EhCache 从 Hibernate 发展而来,是一个纯Java的进程内缓存框架,具有快速.精干等特点.Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存, ...
- 在集群环境中使用 EhCache 缓存系统|RMI 集群模式
RMI 是 Java 的一种远程方法调用技术,是一种点对点的基于 Java 对象的通讯方式.EhCache 从 1.2 版本开始就支持 RMI 方式的缓存集群.在集群环境中 EhCache 所有缓存对 ...
- EhCache缓存学习
在看hibernate的官方文档时,看到关于缓存的介绍. hibernate在缓存管理上做的很好,具体使用,本章不做讲解,本篇主要研究EhCache的用法.其中hibernate使用到的缓存提供商列表 ...
- Varnish 4.0.3详细配置
最近在折腾varnish 4.0,话说从3.0到4.0变化挺大的,许多配置做了调整 对比Varnish 3.x的主要改进点 (1).完全支持流对象: (2).可后台获取失效的对象,即Client/ba ...
- Spring Boot 2.x基础教程:使用EhCache缓存集群
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 上一篇我们介绍了在Spring Boot中整合EhCac ...
最新文章
- 组网中交换机虚拟化技术的应用
- 传孙正义领投滴滴无人车3亿美元新融资,此前软银宣布出售阿里股份等资产
- python的时间差计算
- ObjectStateManager 中已存在具有同一键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象...
- 将d:\java目录下的所有.java文件复制到d:\jad 目录下,并将原来文件的扩展名从.java 改为.jad
- 都在讨论高并发,结果连并发量、TPS、QPS都分不清
- bootstrap 兼容哪些浏览器
- SPSS Sobel检验(图文+数据集)【SPSS 044期】
- php++l+函数,关于PHP中usort()函数的解读
- JSP标签,jsp:include,jsp:forward用法介绍及示例
- TCP/IP报文格式及通讯
- PuTTYgen使用教程
- 畅捷通T+任意文件上传(CNVD-2022-60632 )漏洞复现
- java digester map_Tomcat7启动分析(三)Digester的使用(转载)
- 未来教育计算机二级学生文档,未来教育计算机二级MS-office题库(解题步骤)题库.docx...
- MATLAB 计算 确定系数R2
- java运行期类型鉴定
- 2021人工智能状况分析报告
- MT7682参考手册,MT7682芯片资料介绍
- c语言编程符号函数sgn,在C/C中是否有标准符号函数(signum,sgn)?
热门文章
- 无法从计算机中删除,【求助】Windows无法从该家庭组中删除你的计算机
- adb failed to start daemon 彻底解决
- 数据结构之查找算法:折半查找
- (王道408考研操作系统)第五章输入/输出(I/O)管理-第一节7:缓冲区管理
- poj2965 The Pilots Brothers' refrigerator
- 理解Linux下的SELinux(MAC)
- Windows安装.net Framework时安装不上,提示已处理证书链,但是在不受信任提供程序信任的根证书中终止
- DNS常用记录类型和服务发现(DNS解析)
- pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail
- Linux的chattr与lsattr命令详解(重点-i参数,锁定文件,禁止修改文件)