memcahce施用
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.danga.MemCached.MemCachedClient;
import com.howbuy.common.utils.StringUtil;
/**
* @ClassName: MemcacheUtil
* @Description: 缓存操作工具类,缓存机制采用memcache,如果
* memcache缓存服务器异常,则采用本地缓存方式存储信息
* @author Polo Yuan lizhuan.yuan@howbuy.com
* @date 2013-5-31 下午04:28:50
*
*/
public class MemcacheUtil {
/**
* logger
*/
private static final Logger logger = LoggerFactory.getLogger(MemcacheUtil.class);
/**
* 本地缓存存储
*/
private static Map<String, Object> localCacheMap = new HashMap<String, Object>();
/**
* 本地缓存过期信息存储
*/
private static Map<String, Long> localCacheExpiryMap = new HashMap<String, Long>();
private static MemCachedClient memCachedClient = new MemCachedClient("memcache");
public static MemCachedClient getMemCachedClient() {
logger.info("memCache缓存服务器状态:" + (memCachedClient == null ? "异常" : "正常"));
return memCachedClient;
}
/**
* 根据键获取对象
* @param key 键
* @return Object 对象
*/
public static Object getObject(String key) {
Object obj = null;
if(StringUtil.isEmpty(key)) {
return obj;
}
MemCachedClient memCachedClient = getMemCachedClient();
if(null != memCachedClient) {
obj = memCachedClient.get(key);
} else {// 如果远端缓存出错
if(localCacheMap.containsKey(key)) {
obj = localCacheMap.get(key);
}
}
return obj;
}
/**
* 保存对象到缓存,保存为永久有效
* @param key 健
* @param obj 对象
*/
public static void saveObject(String key, Object obj) {
if(StringUtil.isEmpty(key) || null == obj) {
return;
}
MemCachedClient memCachedClient = getMemCachedClient();
if(null != memCachedClient) {
memCachedClient.set(key, obj);
} else {// 如果远端缓存出错
localCacheMap.put(key, obj);
}
}
/**
* 保存对象到缓存
* @param key 健
* @param obj 对象
* @param expiryTimes 过期时间,毫秒数,如果过期时间小于1000,则认为永久有效
*/
public static void saveObject(String key, Object obj, long expiryTimes) {
if(StringUtil.isEmpty(key) || null == obj) {
return;
}
if(expiryTimes < 1000) {// 如果过期时间小于1000,则认为永久有效
saveObject(key, obj);
}
MemCachedClient memCachedClient = getMemCachedClient();
if(null != memCachedClient) {
memCachedClient.set(key, obj, new Date(expiryTimes));
} else {// 如果远端缓存出错
localCacheMap.put(key, obj);
localCacheExpiryMap.put(key, System.currentTimeMillis() + expiryTimes);
}
}
/**
* 获取指定键的值是否过期
* @param key 健
* @param expiryTimes 过期时间,毫秒数
* @return boolean true:已过期,false:未过期
*/
public static boolean isHasExpired(String key, long expiryTimes) {
boolean isHasExpired = true;
if(StringUtil.isEmpty(key)) {
return isHasExpired;
}
MemCachedClient memCachedClient = getMemCachedClient();
if(null != memCachedClient) {
Object obj = memCachedClient.get(key);
if(null == obj) {// 如果已经过期
isHasExpired = true;
} else {// 没有过期就更新
saveObject(key, obj, expiryTimes);
isHasExpired = false;
}
} else {// 如果远端缓存出错
if(localCacheExpiryMap.containsKey(key)) {
long expiredTimes = localCacheExpiryMap.get(key);
if(expiredTimes < System.currentTimeMillis()) {// 如果已经过期
localCacheExpiryMap.remove(key);
localCacheMap.remove(key);
isHasExpired = true;
} else {// 没有过期就更新
Object obj = localCacheMap.get(key);
saveObject(key, obj, expiryTimes);
isHasExpired = false;
}
}
}
return isHasExpired;
}
/**
* 删除指定键的值
* @param key 键
*/
public static void removeObject(String key) {
if(StringUtil.isEmpty(key)) {
return;
}
MemCachedClient memCachedClient = getMemCachedClient();
if(null != memCachedClient) {
memCachedClient.delete(key);
} else {// 如果远端缓存出错
localCacheMap.remove(key);
localCacheExpiryMap.remove(key);
}
}
}