码迷,mamicode.com
首页 > 系统相关 > 详细

Ehcache入门经典:第二篇ehcache.xml的参数

时间:2018-04-04 00:38:04      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:访问时间   read   ict   有序   single   持久化   hits   堆内存   class   

继续第一篇

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的参数

标签:访问时间   read   ict   有序   single   持久化   hits   堆内存   class   

原文地址:https://www.cnblogs.com/tk55/p/8712159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!