①配置文件   ehcache.xml


  
     
       
      
      
        

②EhCacheUtil工具类

package com.kentrasoft.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

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

public class EhCacheUtil {
	private static Cache kentrasoftCache;
	private static CacheManager manager;
	private static EhCacheUtil instance;

	static {
		// init();
	}

	public static Cache getKentrasoftCache() {
		return kentrasoftCache;
	}


	public static CacheManager getManager() {
		return manager;
	}

	public static EhCacheUtil init() {
		System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
		if (instance == null) {
			instance = new EhCacheUtil();
			manager = CacheManager.create(EhCacheUtil.class.getClassLoader().getResourceAsStream("ehcache/ehcache.xml"));
			kentrasoftCache = manager.getCache("kentrasoftCache");
		}
		return instance;

	}

	public static EhCacheUtil init(String path) {
		System.setProperty("net.sf.ehcache.enableShutdownHook", "true");
		if (instance == null) {
			instance = new EhCacheUtil();
			manager = CacheManager.create(EhCacheUtil.class.getClassLoader().getResourceAsStream(path));
			kentrasoftCache = manager.getCache("kentrasoftCache");
		}
		return instance;

	}

	private static boolean isNull(Element e) {
		return e == null || e.getObjectValue() == null || e.getObjectValue() == null;
	}

	/**
	 * 存入
	 * @param 
	 * @param cache 缓存库
	 * @param key   键
	 * @param value 值
	 */
	public static  void put(Cache cache, String key, T value) {
		Element e = new Element(key, value);
		cache.put(e);
		cache.flush();
	}

	/**
	 * 存入 并设置元素是否永恒保存
	 * @param 
	 * @param cache  缓存库
	 * @param key 键
	 * @param value 值
	 */
	public static  void put(Cache cache, String key, T value, boolean eternal) {
		Element element = new Element(key, value);
		element.setEternal(eternal);
		cache.put(element);
		cache.flush();
	}

	/**
	 * 存入
	 * 
	 * @param 
	 * @param cache 缓存库
	 * @param key 键
	 * @param value 值
	 * @param timeToLiveSeconds 最大存活时间
	 * @param timeToIdleSeconds 最大访问间隔时间
	 */
	public static  void put(Cache cache, String key, T value, int timeToLiveSeconds,
			int timeToIdleSeconds) {
		Element element = new Element(key, value);
		element.setTimeToLive(timeToLiveSeconds);
		element.setTimeToIdle(timeToIdleSeconds);
		cache.put(element);
		cache.flush();
	}

	public static Object getCacheElement(Cache cache, String key) {
		Element e = cache.get(key);
		return e;
	}

	public static Object get(Cache cache, String key) {
		Element e = cache.get(key);
		if (e != null) {
			return e.getObjectValue();
		}
		return null;
	}

	public static void remove(Cache cache, String key) {
		cache.remove(key);
	}

	public static void removeAll(Cache cache, Collection keys) {
		cache.removeAll(keys);
	}

	@SuppressWarnings("unchecked")
	public static void addToList(Cache cache, String key, Serializable value) {
		Element e = cache.get(key);
		if (isNull(e)) {
			List list = Collections.synchronizedList(new LinkedList());
			list.add(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		} else {
			List list = (List) e.getObjectValue();
			list.add(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		}

		cache.flush();
	}

	@SuppressWarnings("unchecked")
	public static void addAllToList(Cache cache, String key, Collection value) {
		Element e = cache.get(key);
		if (isNull(e)) {
			List list = Collections.synchronizedList(new LinkedList());
			list.addAll(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		} else {
			List list = (List) e.getObjectValue();
			list.addAll(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		}

		cache.flush();
	}

	@SuppressWarnings("unchecked")
	public static void addToHashSet(Cache cache, String key, Serializable value) {
		Element e = cache.get(key);
		if (isNull(e)) {
			Set list = Collections.synchronizedSet(new HashSet());
			list.add(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		} else {
			Set list = (Set) e.getObjectValue();
			list.add(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		}

		cache.flush();
	}

	@SuppressWarnings("unchecked")
	public static void addAllToHashSet(Cache cache, String key, Collection value) {
		Element e = cache.get(key);
		if (isNull(e)) {
			Set list = Collections.synchronizedSet(new HashSet());
			list.addAll(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		} else {
			Set list = (Set) e.getObjectValue();
			list.addAll(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		}

		cache.flush();
	}

	@SuppressWarnings("unchecked")
	public static void addToArrayList(Cache cache, String key, Serializable value) {
		Element e = cache.get(key);
		if (isNull(e)) {
			List list = Collections.synchronizedList(new ArrayList());
			list.add(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		} else {
			List list = (List) e.getObjectValue();
			list.add(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		}

		cache.flush();
	}

	@SuppressWarnings("unchecked")
	public static void addAllToArrayList(Cache cache, String key, Collection value) {
		Element e = cache.get(key);
		if (isNull(e)) {
			List list = Collections.synchronizedList(new ArrayList());
			list.addAll(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		} else {
			List list = (List) e.getObjectValue();
			list.addAll(value);
			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
		}

		cache.flush();
	}

	@SuppressWarnings("unchecked")
	public static  T popFromList(Cache cache, String key, Class T) {
		Element e = cache.get(key);
		if (e != null) {
			List list = (List) e.getObjectValue();
			Iterator it = list.iterator();
			if (list.size() > 0) {
				Serializable obj = it.next();
				it.remove();
				e = new Element(key, list);
				e.setEternal(true);
				cache.put(e);
				cache.flush();
				return (T) obj;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static  List popFromList(Cache cache, String key, int count, Class T) {
		Element e = cache.get(key);
		if (e != null) {
			List list = (List) e.getObjectValue();

			if (count < 1) {
				List result = (List) new ArrayList(list);
				list.clear();
				e = new Element(key, list);
				e.setEternal(true);
				cache.put(e);
				cache.flush();
				return result;
			}

			List result = new ArrayList(count);
			Iterator it = list.iterator();
			for (int i = 0; i < count && it.hasNext(); i++) {
				Serializable obj = it.next();
				it.remove();
				result.add((T) obj);
			}

			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
			cache.flush();
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static  T popFromHashSet(Cache cache, String key, Class T) {
		Element e = cache.get(key);
		if (e != null) {
			Set list = (Set) e.getObjectValue();
			Iterator it = list.iterator();
			if (list.size() > 0) {
				Serializable obj = it.next();
				it.remove();
				e = new Element(key, list);
				e.setEternal(true);
				cache.put(e);
				cache.flush();
				return (T) obj;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static  List popFromHashSet(Cache cache, String key, int count, Class T) {
		Element e = cache.get(key);
		if (e != null) {
			Set list = (Set) e.getObjectValue();

			if (count < 1) {
				List result = (List) new ArrayList(list);
				list.clear();
				e = new Element(key, list);
				e.setEternal(true);
				cache.put(e);
				cache.flush();
				return result;
			}

			List result = new ArrayList(count);
			Iterator it = list.iterator();
			for (int i = 0; i < count && it.hasNext(); i++) {
				Serializable obj = it.next();
				it.remove();
				result.add((T) obj);
			}

			e = new Element(key, list);
			e.setEternal(true);
			cache.put(e);
			cache.flush();
			return result;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static int getCollectionSize(Cache cache, String key) {
		Element e = cache.get(key);
		if (e != null) {
			Collection list = (Collection) e.getObjectValue();
			return list.size();
		}
		return 0;
	}

	@SuppressWarnings("rawtypes")
	public static List getKeys(Cache cache) {
		return cache.getKeys();
	}
	
	/**获取缓存名称集合
	 * @param cache Cache对象
	 * @param start 开始位置
	 * @return
	 */
	public static List getKeys(Cache cache, String start) {
		List list = cache.getKeys();
		List result = new ArrayList(list.size());
		for (Object obj : list) {
			if (obj != null && obj.getClass() == String.class) {
				String s = (String) obj;
				if (s.startsWith(start))
					result.add(s);
			}
		}
		return result;
	}
}

③使用的时候,初始化echach对象,然后put对象进去。

//tokenId放入缓存中
				EhCacheUtil.init();
				EhCacheUtil.put(EhCacheUtil.getKentrasoftCache(), "TOKEN_ID","JIASJDFIOJIOAJI12341");

缓存可以设置有效期

//生成动态令牌 并设置有效期
//							user.setAuthToken(UuidUtil.get32UUID());
//							EhCacheUtil.init();
							EhCacheUtil.put(EhCacheUtil.getKentrasoftCache(), user.getAuthToken(), user.getAuthToken(), 1000*60*30, 1000*60*30);
							EhCacheUtil.put(cache, key, value, timeToLiveSeconds, timeToIdleSeconds);
							EhCacheUtil.put(cache, key, value, eternal);

④在需要使用的地方get该对象就可以了

// 获取令牌 tokenId
String tokenId = EhCacheUtil.get(EhCacheUtil.getKentrasoftCache(), "TOKEN_ID").toString();

⑤清除缓存中的数据

//清除缓存信息
EhCacheUtil.remove(EhCacheUtil.getKentrasoftCache(), loginName+"code");

⑥ehcache需要的jar包依赖

	
			net.sf.ehcache
			ehcache
			2.9.1
		

ehcache-core-2.9.1.jar 
slf4j-api-1.5.11.jar 
slf4j-jdk14-1.5.11.jar