Memcached的工具類

package com.cpp.core.common.cache.memcached;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by IntelliJ IDEA.
 *
 * @author YZP
 * @version 1.0
 * @CreationTime: 13-2-17 下午1:32
 * @Description: XmemcachedClient的Wrapper,做些泛型類型轉換,屏蔽Checked Exception之類的雜事.
 * 
 */
public class XMemcachedClientWrapper implements DisposableBean {

    private static Logger logger = LoggerFactory.getLogger(XMemcachedClientWrapper.class);

    private MemcachedClient memcachedClient;

    /**
     * Get方法, 轉換結果類型,失敗時屏蔽異常只返回null.
     */
    public <T> T get(String key) {
        try {
            return (T) memcachedClient.get(key);
        } catch (Exception e) {
            handleException(e, key);
            return null;
        }
    }
    /**
     * Get方法,同時更新過期時間, 轉換結果類型,失敗時屏蔽異常只返回null.
     * @param exp 過期秒數
     */
    public <T> T get(String key,int exp) {
        try {
            return (T) memcachedClient.getAndTouch(key,exp);
        } catch (Exception e) {
            handleException(e, key);
            return null;
        }
    }
    
    /**
     * 功能描述:判斷key是否存在
     *
     * @param key
     * @return
     * 
     * @author zhangzg
     *
     * @since 2014年8月8日
     *
     * @update:[變更日期YYYY-MM-DD][更改人姓名][變更描述]
     */
    public boolean keyIsExist(String key){
        try {
            if(null == memcachedClient.get(key))
                return false;
            return true;
        } catch (TimeoutException e) {
            return false;
        } catch (InterruptedException e) {
            return false;
        } catch (MemcachedException e) {
            return false;
        }
    }

    /**
     * GetBulk方法, 轉換結果類型, 失敗時屏蔽異常只返回null.
     */
    public <T> Map<String, T> getBulk(Collection<String> keys) {
        try {
            return (Map<String, T>) memcachedClient.get(keys);
        } catch (Exception e) {
            handleException(e, StringUtils.join(keys, ","));
            return null;
        }
    }

    /**
     * Set方法, 不等待操作返回結果, 失敗拋出RuntimeException..
     */
    public void asyncSet(String key, int expiredTime, Object value) {
        try {
            memcachedClient.setWithNoReply(key, expiredTime, value);
        } catch (Exception e) {
            handleException(e, key);
        }
    }

    /**
     * Set方法,等待操作返回結果,失敗拋出RuntimeException..
     */
    public boolean set(String key, int expiredTime, Object value) {
        try {
            return memcachedClient.set(key, expiredTime, value);
        } catch (Exception e) {
            throw handleException(e, key);
        }
    }

    /**
     * Delete方法, 失敗拋出RuntimeException.
     */
    public boolean delete(String key) {
        try {
            return memcachedClient.delete(key);
        } catch (Exception e) {
            throw handleException(e, key);
        }
    }

    /**
     * Incr方法, 失敗拋出RuntimeException.
     */
    public long incr(String key, int by, long defaultValue) {
        try {
            return memcachedClient.incr(key, by, defaultValue);
        } catch (Exception e) {
            throw handleException(e, key);
        }
    }

    /**
     * Decr方法, 失敗RuntimeException.
     */
    public long decr(String key, int by, long defaultValue) {
        try {
            return memcachedClient.decr(key, by, defaultValue);
        } catch (Exception e) {
            throw handleException(e, key);
        }
    }

    private RuntimeException handleException(Exception e, String key) {
        logger.warn("xmemcached client receive an exception with key:" + key, e);
        return new RuntimeException(e);
    }

    public MemcachedClient getMemcachedClient() {
        return memcachedClient;
    }

    @Autowired(required = false)
    public void setMemcachedClient(MemcachedClient memcachedClient) {
        this.memcachedClient = memcachedClient;
        if (memcachedClient != null) {
            this.memcachedClient.setOpTimeout(5000L);
//            this.memcachedClient.setOptimizeGet(false);
        }
    }

    public void destroy() throws Exception {
        if (memcachedClient != null) {
            memcachedClient.shutdown();
        }

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章