CacheHelper工具类的使用

package com.bbcmart.util;


import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
//import net.sf.ehcache.store.MemoryStoreEvictionPolicy;


public class CacheHelper {
private static CacheManager cacheManager;
public enum CacheType {
policy, airline, jinri, result, other,
bulletin, linkImage, linkChar, advertisement,waiter
}


public enum OtherType {
str_airline

}



static {
init();

}


public static void init() {
try {
cacheManager = CacheManager.create();


cacheManager.addCache(createCacheByType(CacheType.policy));
cacheManager.addCache(createCacheByType(CacheType.airline));
cacheManager.addCache(createCacheByType(CacheType.jinri));
cacheManager.addCache(createCacheByType(CacheType.result));
cacheManager.addCache(createCacheByType(CacheType.other));
cacheManager.addCache(createCacheByType(CacheType.bulletin));
cacheManager.addCache(createCacheByType(CacheType.linkImage));
cacheManager.addCache(createCacheByType(CacheType.linkChar));
cacheManager.addCache(createCacheByType(CacheType.advertisement));
cacheManager.addCache(createCacheByType(CacheType.waiter));
}
catch (Exception e) {
e.printStackTrace();
}
}

private static Cache createCacheByType(CacheType aCacheType) {
Cache result = null;
if (CacheType.policy.equals(aCacheType))
result = new Cache(CacheType.policy.toString(), 4000, false, true, 0, 0);
if (CacheType.airline.equals(aCacheType))
result = new Cache(CacheType.airline.toString(), 4000, false, false, 300, 300);
if (CacheType.jinri.equals(aCacheType))
result = new Cache(CacheType.jinri.toString(), 4000, false, false, 300, 300);
if (CacheType.result.equals(aCacheType))
result = new Cache(CacheType.result.toString(), 4000, false, false, 300, 300);
if (CacheType.other.equals(aCacheType))
result = new Cache(CacheType.other.toString(), 100, false, true, 0, 0);
if (CacheType.bulletin.equals(aCacheType))
result = new Cache(CacheType.bulletin.toString(), 1000, false, false, 900, 600);
if (CacheType.linkImage.equals(aCacheType))
result = new Cache(CacheType.linkImage.toString(), 1000, false, false, 900, 600);
if (CacheType.linkChar.equals(aCacheType))
result = new Cache(CacheType.linkChar.toString(), 1000, false, false, 900, 600);
if (CacheType.advertisement.equals(aCacheType))
result = new Cache(CacheType.advertisement.toString(), 1000, false, false, 900, 600);
if (CacheType.waiter.equals(aCacheType))
result = new Cache(CacheType.waiter.toString(), 1000, false, false, 900, 600);

//Cache black = new Cache(Const.CACHE_BLACK, 2000, MemoryStoreEvictionPolicy.LFU, false, null, true, 0, 0, false, 0, null);
return result;
}

private static Cache getCacheByType(CacheType aCacheType) {
Cache result = cacheManager.getCache(aCacheType.toString());
if (result==null)
result = createCacheByType(aCacheType);
return result;
}

public static boolean exist(CacheType aType, String aKey) {
boolean result = false;
if (aType != null && aKey != null) {
Cache cache = cacheManager.getCache(aType.toString());
if (cache!=null)
result = cache.isKeyInCache(aKey);
}
return result;
}

public static void put(CacheType aType, String aKey, Object aValue) {
if (aType != null && aKey != null) {
getCacheByType(aType).put(new Element(aKey, aValue));
}
}


public static void remove(CacheType aType, String aKey) {
getCacheByType(aType).remove(aKey);
}

public static Object get(CacheType aType, String aKey) {
Object result = null;
if (aType != null && aKey != null) {
try {
Element element = getCacheByType(aType).get(aKey);
if (element!=null)
result = element.getValue();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}

public static String getStrFromCache(CacheType aType, String aKey) {
String result = "";
Object temp = get(aType, aKey);
if (temp != null)
result = temp.toString();
return result;
}

public static void putOther(OtherType aKey, Object aValue) {
if (aKey != null) {
getCacheByType(CacheType.other).put(new Element(aKey.toString(), aValue));
}
}


public static void removeOther(OtherType aKey) {
getCacheByType(CacheType.other).remove(aKey.toString());
}

public static Object getOther(OtherType aType) {
Object result = null;
if (aType != null) {
try {
Element element = getCacheByType(CacheType.other).get(aType.toString());
if (element!=null)
result = element.getValue();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}

public static String getStrFromCacheOther(OtherType aKey) {
String result = "";
Object temp = getOther(aKey);
if (temp != null)
result = temp.toString();
return result;
}

public static void destroy() {
try {
CacheManager.getInstance().removalAll();
cacheManager = null;
}
catch (Exception ex){
ex.printStackTrace();
}
}
}

原文地址:https://www.cnblogs.com/dyllove98/p/3187154.html