自定义ehcache工具类实现缓存

摘要: 缓存是为提升性能而开辟的内存空间, 其主要目的是暂存数据的处理结果,以供下次使用。缓存的使用很多,比如浏览器会缓存网页,在服务器后台的API 中加入缓存提高系统的性能,在常用的开发中,经常会用到hibernate, hibernate 开启二级缓存就有可能用到ehcache, 当然现成的缓存框架很多,比如:ehcache,oscache,jboss cache等。这些缓存框架其实也是可以单独使用的。比如下面用使用ehcache 做的缓存。

缓存是为提升性能而开辟的内存空间, 其主要目的是暂存数据的处理结果,以供下次使用。缓存的使用很多,比如浏览器会缓存网页,在服务器后台的API 中加入缓存提高系统的性能,在常用的开发中,经常会用到hibernate, hibernate 开启二级缓存就有可能用到ehcache, 当然现成的缓存框架很多,比如:ehcache,oscache,jboss cache等。这些缓存框架其实也是可以单独使用的。比如下面用使用ehcache 做的缓存。

[b]EHCache 缓存配置文件 [/ b]

	
	
	

	

	



maxElementsInMemory:该缓存中允许存放的最大条目数量。
eternal:缓存内容是否永久储存。
overflowToDisk:如果内存中的数据超过maxElementsInMemory,是否使用磁盘存储。
timeToIdleSeconds:如果不是永久储存的缓存,那么在timeToIdleSeconds指定时间内没有访问一个条目,则移除它。
timeToLiveSeconds:如果不是永久储存的缓存,一个条目可以存在的最长时间。
diskPersistent:磁盘储存的条目是否永久保存。
diskExpiryThreadIntervalSeconds:磁盘清理线程的运行时间间隔。

ehcache的初始化
要使用ehcache就首先要进行初始化, 最好是做成单例类,以供后面使用,这里测试,没这样写。
static CacheManager manager=null;
static String configfile="ehcache.xml";
static{
		try {
			manager = CacheManager.create(EHCacheUtil.class.getClassLoader().getResourceAsStream(configfile));
		} catch (CacheException e) {
			e.printStackTrace();
		}
	}


缓存,最常见的方式,自己也可以用hashmap来实现,所以即使你用了ehcache,map中最常见的方法put,get 等方法仍然需要你自己实现。
package com.yihaomen.cache;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

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

public class EHCacheUtil {

	static CacheManager manager=null;
	static String configfile="ehcache.xml";
	static{
		try {
			manager = CacheManager.create(EHCacheUtil.class.getClassLoader().getResourceAsStream(configfile));
		} catch (CacheException e) {
			e.printStackTrace();
		}
	}
	
	public static void put(String cachename,Serializable key,Serializable value){
		manager.getCache(cachename).put(new Element(key, value));
	}
	
	public static Serializable get(String cachename,Serializable key){
		try {
			Element e=manager.getCache(cachename).get(key);
			if(e==null)return null;
			return e.getValue();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (CacheException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void clearCache(String cachename){
		try {
			manager.getCache(cachename).removeAll();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
	}
	
	public static void remove(String cachename,Serializable key){
		manager.getCache(cachename).remove(key);
	}
}



在完成了上面的工具类之后,可以写一个测试代码:
package com.yihaomen.cache;

import static org.junit.Assert.*;

import org.junit.Test;

public class TestEHCacheUtil {

	@Test
	public void test1() throws InterruptedException {
		EHCacheUtil.put("cache1", "key1","value1");
		System.out.println(EHCacheUtil.get("cache1", "key1"));
		for(int i=0;i<7;i++){
			Thread.sleep(1000);
		}
		System.out.println(EHCacheUtil.get("cache1", "key1"));
	}
	
	@Test
	public void test2() throws InterruptedException {
		for(int i=0;i<200;i++){
			EHCacheUtil.put("cache1", "key"+i,"value"+i);
			System.out.println("i="+i+" key"+ i +"=" + EHCacheUtil.get("cache1", "key"+i));
		}
	}
}



这样就完成了自己基于ehcache的缓存框架定义。

上一篇: spring security JDBC 数据库实现,5个表, 例子下载
下一篇: delphi directui 做界面的一个例子
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号