`
hpgary
  • 浏览: 79168 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java EhCache

阅读更多

1、配置文件ehcache-setting.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
	<diskStore path="c:/tmp" />
<!--
缓存配置
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"
		overflowToDisk="true" timeToIdleSeconds="10" timeToLiveSeconds="20"
		diskPersistent="true" diskExpiryThreadIntervalSeconds="120" />
	<cache name="hpCache" maxElementsInMemory="3000" eternal="false"
		overflowToDisk="true" diskPersistent="true" timeToIdleSeconds="10000" timeToLiveSeconds="20000" />
</ehcache>

 2、Java 代码直接调用

CacheManager manager = new CacheManager(new ClassPathResource("ehcache-setting.xml").getFile().getAbsolutePath()) ;
		System.out.println(Arrays.toString(manager.getCacheNames()));
		Cache cache = manager.getCache("hpCache");
		Element e1 = new Element("key1", "黄飞");
		Element e2 = new Element("key2", "hf");
		cache.put(e1);
		cache.put(e2);
		System.out.println( cache.get("key1"));
	
		System.out.println( cache.get("key2")) ;
		//使用flush,和 shutdown后才回持久化到磁盘上,这里和IO一样,不然 数据还在缓冲区域,不会溢写到磁盘
		cache.flush(); 
		manager.shutdown(); 

 3、spring中使用

a、配置bean

@Bean(value = "ehcache")
	public EhCacheManagerFactoryBean ehcache() {
		EhCacheManagerFactoryBean ehcache = new EhCacheManagerFactoryBean();
		try {
			ehcache.setConfigLocation(new FileSystemResource(new ClassPathResource("ehcache-setting.xml").getFile()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ehcache;
	}

 b、需要缓存的方法上加入注解

@Cacheable(value="hpCache")
public long getTime(String param){
  return System.currentTimeMillis() ;
}

 c、调用测试

System.out.println(cacheService.getTime("1"));
Thread.sleep(1000);
System.out.println(cacheService.getTime("1"));

 

4、Ehcache主要特点:

a)快速,简单。在过去众多的测试中已经表明Ehcache是最快的Java缓存之一。

b)多种缓存策略。LRU、LFU和FIFO缓存策略

c)两级缓存数据。内存和磁盘,无需担心容量问题,缓存的数据可伸缩到GB,Ehcache为大数据做过存储优化,在大内存的情况下,所有的进程可以支持数百G的吞吐量。

d)缓存数据会在虚拟机重启的过程中写入磁盘

e)可以通过RMI、api等方式进行分布式缓存。

f)具有缓存和缓存管理器的监听接口。

g)提供hibernate的缓存实现。

 

5、介绍

 Ehcache是用来管理缓存的工具,缓存的数据可以放在内存里,也可以持久化到磁盘上。通过overflowToDisk和diskPersistent来配置。核心是CacheManager,Ehcache的应用都是从CacheManager开始的。一个用过可以有多CacheManager。

 

6、缓存过期策略

FIFO:根据数据写入时间,先进显出

LFU:最少呗使用缓存元素有一个hit属性,hit最小的将会被清除。

LRU:最近最少被使用。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics