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
- MemcacheClient 封装
- MemcacheClient设置过期时间
- 跨平台 C/C++ memcached 客户端 memcacheclient 介绍
- 跨平台 C/C++ memcached 客户端 memcacheclient 介绍
- 不要根据MemcacheClient的delete结果来决定是否删除数据库记录
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 关于智能DNS和CDN加速介绍
- SICP 习题1.12 帕斯卡三角形
- 基于Spring的包含特定注解bean的package扫描工具
- 第一个Java程序
- Service IntentService区别 (面试)
- MemcacheClient 封装
- 六一儿童节题解QAQ
- @Cacheable
- PullToRefresh实现listview刷新加载
- 笔记二:关于在Windows环境下mysql数据库乱码的问题
- Objective C类方法load和initialize的区别
- C++ Primer 中文版(第五版)课后题答案 事前说明
- 欢迎使用CSDN-markdown编辑器
- UIView加任意边框(Masonry)