Java操作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;

/**
 * @author 
 * @version 
 * @CreationTime:
 * @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();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值