MemcacheClient 封装

本文介绍了一种针对MemCache的封装工具类实现方法,包括基本的缓存操作如获取、设置、删除对象等,并提供了对不同数据类型的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package org.digdata.swustoj.base;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public interface IMemCacheTemplate {

    public static final String JSON_KEY = "json";
    public static final String JSON_TYPE_KEY = "type";

    public static final Integer STRING_TYPE = 1;
    public static final Integer LIST_TYPE = 2;
    public static final Integer OBJECT_TYPE = 3;

    public static final TimeUnit SECOND_TIME_UNIT = TimeUnit.SECONDS;
    public static final TimeUnit MINUTE_TIME_UNIT = TimeUnit.MINUTES;
    public static final TimeUnit HOURS_TIME_UNIT = TimeUnit.HOURS;
    public static final TimeUnit DAYS_TIME_UNIT = TimeUnit.DAYS;

    // 默认5分钟
    public static final Integer DEFAULT_EXPIRE_TIME = 30 * 60 * 60;

    /**
     * 
     * @author wwhhf
     * @since 2016年6月1日
     * @comment 获取对象
     * @param key
     * @param clazz
     * @return
     */
    public Object getObject(String key, Class<?> clazz);

    public List<?> getList(String key, Class<?> clazz);

    public String getString(String key);

    public Map<String, String> getString(String... keys);

    /**
     * 
     * @author wwhhf
     * @since 2016年6月1日
     * @comment 设置对象
     * @param key
     * @param obj
     * @return
     */
    public IMemCacheTemplate set(String key, Object obj);

    public IMemCacheTemplate set(String key, Object obj, TimeUnit timeUnit,
            Integer time);

    /**
     * 
     * @author wwhhf
     * @since 2016年6月1日
     * @comment 清除对象
     * @return
     */
    public IMemCacheTemplate clear();

    public IMemCacheTemplate delete(String key);

    /**
     * 
     * @author wwhhf
     * @since 2016年6月1日
     * @comment 运算符
     * @param key
     * @return
     */
    public IMemCacheTemplate incr(String key);

    public IMemCacheTemplate decr(String key);

}
package org.digdata.swustoj.base;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.digdata.swustoj.exception.BusinessException;
import org.digdata.swustoj.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.danga.MemCached.MemCachedClient;

/**
 * 
 * @author wwhhf
 * @since 2016年6月1日
 * @comment MemCache 封装工具类
 */
@Repository
public class MemCacheTemplateImpl implements IMemCacheTemplate {

    @Autowired
    private MemCachedClient memCacheClient = null;

    @Override
    public Object getObject(String key, Class<?> clazz) {
        try {
            String value = (String) memCacheClient.get(key);
            return JsonUtil.parseObject(value, clazz);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    @Override
    public List<?> getList(String key, Class<?> clazz) {
        try {
            String value = (String) memCacheClient.get(key);
            return JsonUtil.parseList(value, clazz);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    @Override
    public String getString(String key) {
        return (String) memCacheClient.get(key);
    }

    @Override
    public IMemCacheTemplate set(String key, Object obj) {
        return set(key, obj, TimeUnit.MINUTES, DEFAULT_EXPIRE_TIME);
    }

    @Override
    public IMemCacheTemplate clear() {
        memCacheClient.flushAll();
        return this;
    }

    @Override
    public IMemCacheTemplate incr(String key) {
        memCacheClient.incr(key);
        return this;
    }

    @Override
    public IMemCacheTemplate decr(String key) {
        memCacheClient.decr(key);
        return this;
    }

    @Override
    public IMemCacheTemplate delete(String key) {
        memCacheClient.delete(key);
        return this;
    }

    @Override
    public Map<String, String> getString(String... keys) {
        Map<String, String> res = new HashMap<String, String>();
        for (String key : keys) {
            res.put(key, getString((key)));
        }
        return res;
    }

    @Override
    public IMemCacheTemplate set(String key, Object obj, TimeUnit timeUnit,
            Integer time) {
        Map<String, Object> map = object2Json(key, obj);
        Date expDate = null;
        // 当前的毫秒
        if (timeUnit.compareTo(SECOND_TIME_UNIT) == 0) {
            expDate = new Date(time * 1000);
        } else if (timeUnit.compareTo(MINUTE_TIME_UNIT) == 0) {
            expDate = new Date(time * 60 * 1000);
        } else if (timeUnit.compareTo(HOURS_TIME_UNIT) == 0) {
            expDate = new Date(time * 60 * 60 * 1000);
        } else {
            expDate = new Date(time * 60 * 60 * 60 * 1000);
        }
        try {
            this.memCacheClient.set(key, (String) map.get(JSON_KEY), expDate,
                    (Integer) map.get(JSON_TYPE_KEY));
        } catch (Exception e) {
            throw new BusinessException(e);
        }
        return this;
    }

    /**
     * @author wwhhf
     * @since 2016年6月1日
     * @comment
     * @param key
     * @param obj
     * @return
     */
    private Map<String, Object> object2Json(String key, Object obj) {
        Map<String, Object> res = new HashMap<>();
        if (obj.getClass() == String.class) {
            res.put(JSON_KEY, obj);
            res.put(JSON_TYPE_KEY, STRING_TYPE);
        } else if (obj.getClass() == List.class) {
            res.put(JSON_KEY, JsonUtil.getJson(obj, true));
            res.put(JSON_TYPE_KEY, LIST_TYPE);
        } else {
            res.put(JSON_KEY, JsonUtil.getJson(obj, true));
            res.put(JSON_TYPE_KEY, OBJECT_TYPE);
        }
        return res;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值