关于ehcache缓存的使用

首先大家得先去下载一个ehcache的jar包

这个很多地方都是需要会员才能进行下载

大家耐心找找会找到免费的那种的。


关于ehcache这个缓存 顾名思义就是为了提高性能而做出的第三方插件

1、下好了jar包之后你将压缩包中的jar文件copy到lib里面

接下来

2、你得要有一个ehcache.xml文件

代码如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ehcache [
	<!ELEMENT ehcache (diskStore,defaultCache,cache)>
	<!ELEMENT diskStore EMPTY>
	<!ELEMENT defaultCache EMPTY>
	<!ELEMENT cache EMPTY>
	<!ATTLIST diskStore 
		path CDATA #REQUIRED
	>
	<!ATTLIST defaultCache 
		maxElementsInMemory CDATA #REQUIRED
		eternal (true|false) "false"
		timeToIdleSeconds CDATA #REQUIRED
		timeToLiveSeconds CDATA #REQUIRED
		overflowToDisk (true|false) "true"
	>
	<!ATTLIST cache 
		name ID #REQUIRED
		maxElementsInMemory CDATA #REQUIRED
		eternal (true|false) "false"
		timeToIdleSeconds CDATA #REQUIRED
		timeToLiveSeconds CDATA #REQUIRED
		overflowToDisk (true|false) "true"
	>
]>
<ehcache>  
    <diskStore path="java.io.tempdir" />  
    <defaultCache maxElementsInMemory="1000" eternal="false"  
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />  
    <cache name="ehcacheName" maxElementsInMemory="10000"  
        eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"  
        overflowToDisk="true" />  
</ehcache>  


里面的配置文件的一个标签的意思可以去百度的到我这里给大家搜到一点点

搜索内容如下:

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并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想。



3、这个时候来个工具类

这个工具类可以根据自己的需求来进行修改

package util;



import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class Ehcache {
	
	  private static final CacheManager cacheManager = new CacheManager();  
	    private Cache cache;  
	    public Ehcache(){  
	        this.cache=cacheManager.getCache("ehcacheName");
	    }  
	  
	    public Cache getCache() {  
	        return cache;  
	    }  
	  
	    public void setCache(Cache cache) {  
	        this.cache = cache;  
	    }  
	  
	  
	  
	        /* 
	     * 通过名称从缓存中获取数据 
	     */  
	    public Object getCacheElement(Integer cacheKey) throws Exception {  
	            Element e = cache.get(cacheKey);  
	        if (e == null) {  
	            return null;  
	        }  
	        return e.getValue();  
	    }  
	    /* 
	     * 将对象添加到缓存中 
	     */  
	    public void addToCache(Integer cacheKey, Object result) throws Exception {  
	        Element element = new Element(cacheKey, result);  
	        cache.put(element);  
	    }  
	  
}

4、最后是测试

我这里做的是自定义标签的案列  主要的代码我已经用特别的颜色标注的起来了

//实例化对象
private Ehcache ea=new Ehcache();

private String findName(Integer bookCategoryId){
		String bname="";
		
		BookCategory b=null;
		//首先取出集合当中的那个值
		//b=map.get(bookCategoryId);
		try {
			b=(BookCategory)ea.getCacheElement(bookCategoryId);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(null!=b)
		{
			System.out.println("aaaaaaaa");
			bname=b.getBookCategoryName();
		}else{
			System.out.println("bbbbb");
			//实例化对象然后进行赋值
			BookCategory bc=new BookCategory();
			bc.setBookCategoryId(bookCategoryId);
			//调用方法找到那个对象
			b=new BookCategoryDao().load(bc);
			if(null!=b){//如果不为空则找到了 进行赋值
				bname=b.getBookCategoryName();
				//添加到map集合当中
				//map.put(bookCategoryId, b);
				try {
					ea.addToCache(bookCategoryId, b);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return bname;
	}

这样子之后一个简单的缓存就完成了  

当数据很大的时候就会很明显

我上面输出的

System.out.println("aaaaaaaa");
System.out.println("bbbbb");
这两个是我自己在进行测试  看看他是从数据库中取的值 还是从缓存中




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值