读书人

memcahce施用

发布时间: 2013-07-09 09:50:47 作者: rapoo

memcahce使用
??? ??? 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);
??? ??? }
??? }
}

读书人网 >编程

热点推荐