MemcacheClient 封装

来源:互联网 发布:oa协同办公平台源码 编辑:程序博客网 时间:2024/05/14 11:01
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 封装工具类 */@Repositorypublic 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;    }}
0 0