package com.zhph.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
* @ClassName:CacheManagerUtil
* @Description:缓存工具类
* @company:zhph
* @author tkq
* @date 2017年2月7日 下午2:46:29
*/
public class CacheManagerUtil {
private CacheManagerUtil(){}
private static CacheManager manager;
static {
manager = CacheManager.create();
}
/**
* @Title: putObjectToCash
* @param:
* @return:void
* @Description:放置相应的数据到缓存中
* @author tkq
* @date 2017年2月7日 下午3:15:52
* @throws
*/
@SuppressWarnings("rawtypes")
public static void putObjectToCash(String CashName,Map map){
Cache cache = manager.getCache(CashName);
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry value = (Map.Entry)iterator.next();
Element element = new Element(value.getKey(), value.getValue());
cache.put(element);
}
}
/**
* @Title: getObjectForCash
* @param:@return
* @return:Object
* @Description:在相应的缓存中得到相应的数据
* @author tkq
* @date 2017年2月7日 下午3:29:59
* @throws
*/
public static Object getObjectForCash(String CashName,String KeyName){
Cache cache = manager.getCache(CashName);
Element element = cache.get(KeyName);
return element == null ? null : element.getObjectValue();
}
/**
* @Title: removeAllCash
* @param:@param CashName
* @return:void
* @Description:删除所有的缓存
* @author tkq
* @date 2017年2月7日 下午3:36:24
* @throws
*/
public static void removeAllCash(String CashName){
Cache cache = manager.getCache(CashName);
cache.removeAll();
}
/**
* @Title: removeCashByKey
* @param:@param CashName
* @param:@param keyName
* @return:void
* @Description:移除相应的缓存
* @author tkq
* @date 2017年2月7日 下午3:38:12
* @throws
*/
public static void removeCashByKey(String CashName,String keyName){
Cache cache = manager.getCache(CashName);
cache.remove(keyName);
}
/**
* 将map放置到缓存中
* @param cashName 缓存范围
* @param key
* @param value
*/
public static <T> void put(String cashName,String key,T value){
if (StringUtils.isNotBlank(key)) {
Cache cache = manager.getCache(cashName);
cache.put(new Element(key, value));
}
}
/**
* 将map放置到缓存中
* @param cashName 缓存范围
* @param key
* @param value
*/
public static <T> void put(String cashName,String key,T value, int expire){
if (StringUtils.isNotBlank(key)) {
Cache cache = manager.getCache(cashName);
Element e = new Element(key, value);
e.setTimeToLive(expire);
cache.put(e);
}
}
/**
* 获得指定的缓存控件的key
* @param cacheName
* @param key
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T get(String cacheName,String key) {
T value = null;
if (StringUtils.isNotBlank(key)) {
Cache cache = manager.getCache(cacheName);
try{
Element e1 = cache.get(key);
value = (T) e1.getObjectValue();
}catch(Throwable e){
e.printStackTrace();
}
}
return value;
}
/**
* 更新缓存对象
* @param cashName
* @param key
* @param value
*/
public static <T> void update(String cashName,String key,T value){
if (StringUtils.isNotBlank(key)) {
Cache cache = manager.getCache(cashName);
if(null!=cache.get(key)){
removeCashByKey(cashName,key);
}else{
cache.put(new Element(key, value));
}
}
}
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("key", "123456");
//修改缓存中的值
CacheManagerUtil.putObjectToCash("cacheKey", map);
}
}