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"));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Ehcache是一个纯Java的进程内缓存框架,具有快速、精干等特点。它支持内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。Ehcache最初是由Greg Luck于2003年开始开发,截止目前,Ehcache已经演进到了3.10.0版本,各方面的能力已经构建的非常完善。Ehcache官网上也毫不谦虚的描述自己是“Java's most widely-used cache”,即JAVA使用最广泛的缓存。 使用Ehcache可以提高应用程序的性能,减少数据库访问次数,提高响应速度。Ehcache可以用于缓存任何类型的对象,包括POJO、Hibernate对象、Spring对象等。Ehcache还支持缓存的过期时间、缓存的最大元素数、缓存的持久化等功能。 以下是一个使用Ehcache的例子: ```java import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; public class EhcacheDemo { public static void main(String[] args) { // 创建缓存管理器 CacheManager cacheManager = CacheManager.create(); // 创建缓存对象 Cache cache = new Cache("myCache", 10000, false, false, 5, 2); // 添加缓存对象到缓存管理器 cacheManager.addCache(cache); // 添加元素到缓存 Element element = new Element("key1", "value1"); cache.put(element); // 获取缓存中的元素 Element result = cache.get("key1"); System.out.println(result.getObjectValue()); // 关闭缓存管理器 cacheManager.shutdown(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值