Ehcache入门经典:第二篇ehcache.xml的参数
继续第一篇
diskStore
path:指定在硬盘上存储对象的路径
path属性可以配置的目录有:
user.home(用户的家目录)
user.dir(用户当前的工作目录)
java.io.tmpdir(默认的临时目录)
ehcache.disk.store.dir(ehcache的配置目录)
绝对路径(如:c:\\ehcache)
<diskStore path="G:\\eclipse\\workspace8\\Ehcache\\src\\com\\ij34\\cache" />
System.out.println("userHome:"+System.getProperty("user.home"));
System.out.println("userDir:"+System.getProperty("user.dir"));
System.out.println("tmpDir:"+System.getProperty("java.io.tmpdir"));
一、以下属性是必须的:
1、name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。
2、maxElementsInMemory:在内存中缓存的element的最大数目。
3、maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
4、eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
5、overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
二、以下属性是可选的:
cache元素中可以指定的属性也有很多,但只有一个是必须的。那就是name属性。
name:指定cache的名称。
1、diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
2、memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
3、maxEntriesLocalDisk:指定允许在硬盘上存放元素的最大数量,0表示不限制。这个属性我们也可以在运行期通过CacheConfiguration来更改。
4、 maxEntriesLocalHeap:指定允许在内存中存放元素的最大数量,0表示不限制。这个属性也可以在运行期动态修改。
5、maxEntriesInCache:指定缓存中允许存放元素的最大数量。这个属性也可以在运行期动态修改。但是这个属性只对Terracotta分布式缓存有用。
6、maxBytesLocalDisk:指定当前缓存能够使用的硬盘的最大字节数,其值可以是数字加单位,单位可以是K、M或者G,不区分大小写,如:30G。当在CacheManager级别指定了该属性后,Cache级别也可以用百分比来表示,如:60%,表示最多使用CacheManager级别指定硬盘容量的60%。该属性也可以在运行期指定。当指定了该属性后会隐式的使当前Cache的overflowToDisk为true。
7、maxBytesLocalHeap:指定当前缓存能够使用的堆内存的最大字节数,其值的设置规则跟maxBytesLocalDisk是一样的。
8、maxBytesLocalOffHeap:指定当前Cache允许使用的非堆内存的最大字节数。当指定了该属性后,会使当前Cache的overflowToOffHeap的值变为true,如果我们需要关闭overflowToOffHeap,那么我们需要显示的指定overflowToOffHeap的值为false。
9、 overflowToDisk:boolean类型,默认为false。当内存里面的缓存已经达到预设的上限时是否允许将按驱除策略驱除的元素保存在硬盘上,默认是LRU(最近最少使用)。当指定为false的时候表示缓存信息不会保存到磁盘上,只会保存在内存中。该属性现在已经废弃,推荐使用cache元素的子元素persistence来代替,如:<persistence strategy=”localTempSwap”/>。
10、diskSpoolBufferSizeMB:当往磁盘上写入缓存信息时缓冲区的大小,单位是MB,默认是30。
11、overflowToOffHeap:boolean类型,默认为false。表示是否允许Cache使用非堆内存进行存储,非堆内存是不受Java GC影响的。该属性只对企业版Ehcache有用。
12、copyOnRead:当指定该属性为true时,我们在从Cache中读数据时取到的是Cache中对应元素的一个copy副本,而不是对应的一个引用。默认为false。
13、copyOnWrite:当指定该属性为true时,我们在往Cache中写入数据时用的是原对象的一个copy副本,而不是对应的一个引用。默认为false。
14、timeToIdleSeconds:单位是秒,表示一个元素所允许闲置的最大时间,也就是说一个元素在不被请求的情况下允许在缓存中待的最大时间。默认是0,表示不限制。
15、timeToLiveSeconds:单位是秒,表示无论一个元素闲置与否,其允许在Cache中存在的最大时间。默认是0,表示不限制。
16、eternal:boolean类型,表示是否永恒,默认为false。如果设为true,将忽略timeToIdleSeconds和timeToLiveSeconds,Cache内的元素永远都不会过期,也就不会因为元素的过期而被清除了。
17、diskExpiryThreadIntervalSeconds :单位是秒,表示多久检查元素是否过期的线程多久运行一次,默认是120秒。
18、clearOnFlush:boolean类型。表示在调用Cache的flush方法时是否要清空MemoryStore。默认为true。
三、子元素
3.1、persistence:表示Cache的持久化,它只有一个属性strategy,表示当前Cache对应的持久化策略。其可选值如下:
localTempSwap:当堆内存或者非堆内存里面的元素已经满了的时候,将其中的元素临时的存放在磁盘上,一旦重启就会消失。
localRestartable:该策略只对企业版Ehcache有用。它可以在重启的时候将堆内存或者非堆内存里面的元素持久化到硬盘上,重启之后再从硬盘上恢复元素到内存中。
none:不持久化缓存的元素
distributed:该策略不适用于单机,是用于分布式的。
3.2、copyStrategy:当我们指定了copyOnRead或copyOnWrite为true时,就会用到我们的copyStrategy,即拷贝策略了。默认的copyStrategy是通过序列化来实现的,我们可以通过实现net.sf.ehcache.store.compound.CopyStrategy接口来实现自己的CopyStrategy,然后只需在cache元素下定义一个copyStrategy元素并指定其class属性为我们的CopyStrategy实现类。如:<copyStrategy class="xxx.xxx.xxx"/>。
3.3、pinning:表示将缓存内的元素固定住,除非过期,否则不会对它进行删除和驱除到其它储存容器中。pinning元素只定义了一个属性store,表示将把元素固定在哪个位置。其可选值有localMemory和inCache。
localMemory:表示将元素固定在内存中。
inCache:表示将元素固定在任何其正在保存的容器中。
四、缓存的3 种清空策略 :
1、FIFO ,first in first out (先进先出).
2、LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
3、LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
五、defaultCache
defaultCache:
默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
eternal:代表对象是否永不过期
timeToIdleSeconds:最大的发呆时间
timeToLiveSeconds:最大的存活时间
overflowToDisk:是否允许对象被写入到磁盘
六、方法
创建CacheManager 的方法:
方法一:
CacheManager manager = new CacheManager();
方法二:
CacheManager manager = new CacheManager("src/config/ehcache.xml");
方法三:
URL url = getClass().getResource("/anotherconfigurationname.xml");
CacheManager manager = new CacheManager(url);
方法四:
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
CacheManager manager = new CacheManager(fis);
} finally {
fis.close();
}
获取cacheNames 列表:
方法一:
CacheManager.create();
String[] cacheNames = CacheManager.getInstance().getCacheNames();
方法二:
CacheManager manager = new CacheManager();
String[] cacheNames = manager.getCacheNames();
方法三:
CacheManager manager1 = new CacheManager("src/config/ehcache1.xml");
CacheManager manager2 = new CacheManager("src/config/ehcache2.xml");
String[] cacheNamesForManager1 = manager1.getCacheNames();
String[] cacheNamesForManager2 = manager2.getCacheNames();
添加和删除缓存元素:
设置一个名为testCache 的新cache,属性为默认:
CacheManager singletonManager = CacheManager.create();
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");
设置一个名为testCache 的新cache,并定义其属性:
CacheManager singletonManager = CacheManager.create();
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
singletonManager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");
Cache 属性说明:
构造函数:
public Cache(String name,
int maxElementsInMemory,
boolean overflowToDisk,
boolean eternal,
long timeToLiveSeconds,
long timeToIdleSeconds)
参数说明:
name :元素名字。
maxElementsInMemory :设定内存中创建对象的最大值。
overflowToDisk : 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘上。
eternal : 设置元素是否永久驻留。
timeToIdleSeconds : 设置某个元素消亡前的停顿时间。也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。只能在元素不是永久驻留时有效。
timeToLiveSeconds : 设置某个元素消亡前的生存时间。也就是一个元素从构建到消亡的最大时间间隔值。只能在元素不是永久驻留时有效。
删除缓存元素:
CacheManager singletonManager = CacheManager.create();
singletonManager.removeCache("testCache");
关闭缓存管理器 CacheManager
CacheManager.getInstance().shutdown();
对于缓存对象的操作:
放入一个简单的对象到缓存元素;
Cache cache = manager.getCache("testCache");
Element element = new Element("key1", "value1");
cache.put(element);
得到一个序列化后的对象属性值;
Cache cache = manager.getCache("testCache");
Element element = cache.get("key1");
Serializable value = element.getValue();
得到一个没有序列化后的对象属性值;
Cache cache = manager.getCache("testCache");
Element element = cache.get("key1");
Object value = element.getObjectValue();
删除一个对象从元素;
Cache cache = manager.getCache("testCache");
Element element = new Element("key1", "value1");
cache.remove("key1");
对于永固性磁盘存储,立即存储到磁盘:
Cache cache = manager.getCache("testCache");
cache.flush();
获得缓存大小:
得到缓存的对象数量;
Cache cache = manager.getCache("testCache");
int elementsInMemory = cache.getSize();
得到缓存对象占用内存的数量
Cache cache = manager.getCache("testCache");
long elementsInMemory = cache.getMemoryStoreSize();
得到缓存对对象占用磁盘的数量
Cache cache = manager.getCache("testCache");
long elementsInMemory = cache.getDiskStoreSize();
关于缓存的读取和丢失的记录:
得到缓存读取的命中次数;
Cache cache = manager.getCache("testCache");
int hits = cache.getHitCount();
得到内存中缓存读取的命中次数;
Cache cache = manager.getCache("testCache");
int hits = cache.getMemoryStoreHitCount();
得到磁盘中缓存读取的命中次数;
Cache cache = manager.getCache("testCache");
int hits = cache.getDiskStoreCount();
得到缓存读取的丢失次数;
Cache cache = manager.getCache("testCache");
int hits = cache.getMissCountNotFound();
得到缓存读取的已经被销毁的对象丢失次数;
Cache cache = manager.getCache("testCache");
int hits = cache.getMissCountExpired();
Ehcache入门经典:第二篇ehcache.xml的参数的更多相关文章
- Ehcache入门经典:第一篇
ehcache主要是轻量级的缓存实现 ehcache.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)
第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)
这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)
第一题:401 - Palindromes UVA : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8 ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 1(Lists)
127 - "Accordian" Patience 题目大意:一个人一张张发牌,如果这张牌与这张牌前面的一张或者前面的第三张(后面称之为一位置和三位置)的点数或花式相同,则将这张 ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 2(Binary Trees)
112 - Tree Summing 题目大意:给出一个数,再给一颗树,每个头节点的子树被包含在头节点之后的括号里,寻找是否有从头节点到叶子的和与给出的数相等,如果有则输出yes,没有输出no! 解题 ...
- JS入门经典第二章总结
document:在对网页编写脚本时,我们使用document对象代表网页.要引用一个属性,只需在document对象后加一个“.”号,然后再加上要引用的属性名. alert():该函数弹出一个消息框 ...
- Flask 入门(第二篇)
1. 数据库 Flask 没有限定使用哪种数据库,不管是 SQL 还是 NoSQL.如果你不想自己编写负责的 SQL语句的话,你也可以使用 ORM,通过 SQLALchemy 即可实现. 1.1 SQ ...
- 【php学习】PHP 入门经典第二章笔记
问题答疑: 1.默认情况下,Apache服务器的配置文件名.MySQL服务器的配置文件名以及PHP预处理器配置文件名分别是什么?Apache默认主配置文件:根目录下config文件夹下httpd.co ...
随机推荐
- [Swift]LeetCode565. 数组嵌套 | Array Nesting
A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest ...
- [Swift]LeetCode622. 设计循环队列 | Design Circular Queue
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- [Swift]LeetCode950. 按递增顺序显示卡牌 | Reveal Cards In Increasing Order
In a deck of cards, every card has a unique integer. You can order the deck in any order you want. ...
- ubuntu16.04安装lnmp环境
1.安装mysql sudo apt install mysql-server 2.安装nginx和php #添加nginx和php的ppa源 sudo apt-add-repository ppa ...
- Python内置函数(59)——sorted
英文文档: sorted(iterable[, key][, reverse]) Return a new sorted list from the items in iterable. Has tw ...
- 7.Ajax
优先级 如果发送的是[普通数据] jQuery XMLHttpRequest iframe 如果发送的是[文件] iframe jQuery(FormData) XMLHttpRequest(Form ...
- 「造个轮子」——设计 HTTP 请求全局上下文
前言 本次 Cicada 已经更新到了 v1.0.3. 主要是解决了两个 issue,#9(Boss线程数好像设置有误 ) #8(怎么返回纯字符串内容不要JSON格式?). 所以本次的主要更新为: C ...
- 今天俺要说一说简单工厂模式(Simple Factory)
前言:简单工厂不能说是设计模式,它是一种变成习惯,因为它不是23种设计模式之一,但是它在实际开发中经常用到,而且也非常简单,可以说是工厂模式的一个引导. 大多程序员都是单身狗,为了弥补一下你们的心情, ...
- SpringBoot上传文件到本服务器 目录与jar包同级
前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了. 当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源.但是当你 ...
- Chapter 4 Invitations——23
The next morning, when I pulled into the parking lot, I deliberately parked as far as possible from ...