通常在访问量大数据更新频率不高的系统中会使用第三方的缓存组件来降低数据库服务的负载,鉴于模块独立分工独立的考虑,针对缓存组件操作的工作全部应该统一接口对其他业务提供服务,这样业务操作只需要关注业务实现不需要关注缓存的具体细节。
本例以Redis缓存组件为例,制定操作Redis的工具类。

 package com.luwei.console.mg.util;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations; /**
*
* <Description> Redis工具类<br>
* 使用该工具类的JavaBean必须实现Serializable接口
*
* @author lu.wei<br>
* @email 1025742048@qq.com <br>
* @date 2016年11月27日 <br>
* @since V1.0<br>
* @see com.luwei.console.mg.util <br>
*/
public class RedisUtil { private static Logger logger = LoggerFactory.getLogger(RedisUtil.class); @SuppressWarnings("unchecked")
private static RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ContextUtil.getBean("redisTemplate");; /**
*
* <Description> 根据前缀进行清除缓存<br>
*
* @author lu.wei<br>
* @email wei.lu@qq.com<br>
* @date 2016年8月25日 下午2:41:29
* @param prefix
* <br>
*/
public static void cleanRedis(String prefix) {
logger.info("cleanRedis prefix: {}", prefix);
try {
if (null != prefix) {
if (null != redisTemplate) {
Set<String> keys = redisTemplate.keys(prefix + "*");
for (String key : keys) {
redisTemplate.delete(key);
}
}
} } catch (Exception e) {
logger.error("cleanRedis error : {} ", e.getMessage(), e);
}
} /**
*
* <Description> 根据KEY进行清除缓存<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年10月28日 上午10:50:52
* @param key
* <br>
*/
public static void cleanRedisByKey(String key) {
logger.info("cleanRedisByKey key: {}", key);
try {
if (null != key) {
if (null != redisTemplate) {
redisTemplate.delete(key);
}
} } catch (Exception e) {
logger.error("cleanRedisByKey error : {} ", e.getMessage(), e);
}
} /**
*
* <Description> 缓存字符串<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午5:47:44
* @param key
* @param data
* @param minus
* <br>
*/
public static void putCacheStr(String key, String data, Long minus) {
logger.info("putCacheStr : {}, {}, {} minute", key, data, minus);
try {
ValueOperations<String, Object> opsValue = null;
if (null != redisTemplate) {
opsValue = redisTemplate.opsForValue();
if (null != opsValue) {
opsValue.set(key, data);
}
}
} catch (Exception e) {
logger.error("putCacheStr error : {} ", e.getMessage(), e);
}
} /**
*
* <Description> 获取缓存字符串<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午5:48:00
* @param key
* @return <br>
*/
public static String getCacheStr(String key) {
logger.info("getCacheStr : {}", key); String retStr = null;
try {
ValueOperations<String, Object> opsValue = null;
if (null != redisTemplate) {
opsValue = redisTemplate.opsForValue();
if (null != opsValue) {
retStr = String.valueOf(opsValue.get(key));
}
}
} catch (Exception e) {
logger.error("getCacheStr error : {} ", e.getMessage(), e);
}
return retStr;
} /**
*
* <Description> 缓存简单对象<br>
* 基本数据类型和简单对象
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午4:47:14
* @param key
* @param value
* @param minus
* <br>
*/
public static void putCacheSimple(String key, Object data, Long minus) {
logger.info("putCacheSimple : {}, {}, {} minute", key, data, minus);
try {
ValueOperations<String, Object> opsValue = null;
if (null != redisTemplate) {
opsValue = redisTemplate.opsForValue();
if (null != opsValue) {
opsValue.set(key, data); if (null != minus) {
redisTemplate.expire(key, minus, TimeUnit.MINUTES);
}
}
}
} catch (Exception e) {
logger.error("putCacheSimple error : {} ", e.getMessage(), e);
}
} /**
*
* <Description> 获取缓存的简单对象<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午4:50:16
* @param key
* @return <br>
*/
public static Object getCacheSimple(String key) {
logger.info("getCacheSimple : {}", key); Object object = null;
try {
ValueOperations<String, Object> opsValue = null;
if (null != redisTemplate) {
opsValue = redisTemplate.opsForValue();
if (null != opsValue) {
object = (Object) opsValue.get(key);
}
}
} catch (Exception e) {
logger.error("getCacheSimple error : {} ", e.getMessage(), e);
}
return object;
} /**
*
* <Description> 缓存List数据<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午4:52:43
* @param key
* @param datas
* @param minus
* <br>
*/
public static void putCacheList(String key, List<?> datas, Long minus) {
logger.info("putCacheList : {}, {}, {} minute", key, datas, minus);
try {
ListOperations<String, Object> opsList = null;
if (null != redisTemplate) {
opsList = redisTemplate.opsForList();
if (null != opsList) {
int size = datas.size();
for (int i = 0; i < size; i++) {
opsList.leftPush(key, datas.get(i));
} if (null != minus) {
redisTemplate.expire(key, minus, TimeUnit.MINUTES);
}
}
}
} catch (Exception e) {
logger.error("putCacheList error : {} ", e.getMessage(), e);
}
} /**
*
* <Description> 获取缓存的List对象<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午4:50:16
* @param key
* @return <br>
*/
public static List<Object> getCacheList(String key) {
logger.info("getCacheList : {}", key); List<Object> dataList = new ArrayList<Object>();
try {
ListOperations<String, Object> opsList = null;
if (null != redisTemplate) {
opsList = redisTemplate.opsForList();
if (null != opsList) {
Long size = opsList.size(key);
for (int i = 0; i < size; i++) {
dataList.add(opsList.index(key, i));
}
}
}
} catch (Exception e) {
logger.error("getCacheList error : {} ", e.getMessage(), e);
}
return dataList;
} /**
*
* <Description> 缓存SET数据<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午5:21:30
* @param key
* @param datas
* @param minus
* <br>
*/
public static void putCacheSet(String key, Set<?> datas, Long minus) {
logger.info("putCacheList : {}, {}, {} minute", key, datas, minus);
try {
SetOperations<String, Object> opsSet = null;
if (null != redisTemplate) {
opsSet = redisTemplate.opsForSet();
if (null != opsSet) {
opsSet.add(key, datas); if (null != minus) {
redisTemplate.expire(key, minus, TimeUnit.MINUTES);
}
}
}
} catch (Exception e) {
logger.error("putCacheList error : {} ", e.getMessage(), e);
}
} /**
*
* <Description> 获取缓存的SET对象<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午4:50:16
* @param key
* @return <br>
*/
public static Set<Object> getCacheSet(String key) {
logger.info("getCacheSet : {}", key); Set<Object> dataSet = new HashSet<Object>();
try {
SetOperations<String, Object> opsSet = null;
if (null != redisTemplate) {
opsSet = redisTemplate.opsForSet();
if (null != opsSet) {
dataSet = opsSet.members(key);
}
}
} catch (Exception e) {
logger.error("getCacheSet error : {} ", e.getMessage(), e);
}
return dataSet;
} /**
*
* <Description> 缓存MAP数据<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午5:21:30
* @param key
* @param datas
* @param minus
* <br>
*/
public static void putCacheMap(String key, Map<Object, Object> datas, Long minus) {
logger.info("putCacheMap : {}, {}, {} minute", key, datas, minus);
try {
HashOperations<String, Object, Object> opsHash = null;
if (null != redisTemplate) {
opsHash = redisTemplate.opsForHash();
if (null != opsHash) {
opsHash.putAll(key, datas); if (null != minus) {
redisTemplate.expire(key, minus, TimeUnit.MINUTES);
}
}
}
} catch (Exception e) {
logger.error("putCacheMap error : {} ", e.getMessage(), e);
}
} /**
*
* <Description> 获取缓存的MAP对象<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年12月22日 下午4:50:16
* @param key
* @return <br>
*/
public static Map<Object, Object> getCacheMap(String key) {
logger.info("getCacheMap : {}", key); Map<Object, Object> dataMap = new HashMap<Object, Object>();
try {
HashOperations<String, Object, Object> opsHash = null;
if (null != redisTemplate) {
opsHash = redisTemplate.opsForHash();
if (null != opsHash) {
dataMap = opsHash.entries(key);
}
}
} catch (Exception e) {
logger.error("getCacheMap error : {} ", e.getMessage(), e);
}
return dataMap;
} /**
*
* <Description> TODO<br>
*
* @author lu.wei<br>
* @email 1025742048@qq.com<br>
* @date 2016年10月27日 下午1:54:58
* @return <br>
*/
public static RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplate;
}
}

使用RedisTemplate进行Redis存取的工具类设计的更多相关文章

  1. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  2. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  3. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  4. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  5. SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(五): 数据表设计、使用 jwt、redis、sms 工具类完善注册登录逻辑

    (1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y-h/p ...

  6. Redis,JedisPool工具类

    Redis,JedisPool工具类 1.JedisPool 详细配置解释代码 2.Jedis工具类 导入相关依赖: commons-pool2-2.3.jar jedis-2.7.0.jar 1.J ...

  7. JFreeChart绘制XY折线图(工具类设计)

    准备用Java写通信的仿真平台作为毕业设计,相比matlab绘图,Java绘图需要自己去写很多工具类,博主在这采用了JFreeChart的开源解决方案,摸索着自己写了一个XY折线图工具类,话不多说贴源 ...

  8. SpringBoot整合Redis并完成工具类

    SpringBoot整合Redis的资料很多,但是我只需要整合完成后,可以操作Redis就可以了,所以不需要配合缓存相关的注解使用(如@Cacheable),而且我的系统框架用的日志是log4j,不是 ...

  9. Redis使用及工具类

    原地址:https://www.cnblogs.com/wyy123/p/6078593.html [学会安装redis] 从redis.io下载最新版redis-X.Y.Z.tar.gz后解压,然后 ...

随机推荐

  1. ios上架报错90080,90087,90209,90125 解决办法

    ERROR ITMS-90087: "Unsupported Architectures. The executable for yht.temp_caseinsensitive_renam ...

  2. 0427 scrum & 读后感

    5.Scrum团队成立 5.1 团队名称,团队目标.团队口号.团队照: 5.2 角色分配 6. 团队项目选题 详情见团队博客:http://www.cnblogs.com/alfredzhu/ htt ...

  3. MySQL数据库3 - MySQL常用数据类型

    一. MySql常用数据类型 数据类型:整数(tinyint smailint int bigint) 定点数 decimal(p,s) ------ 小数点位置固定的       ---> 数 ...

  4. setInterval()与clearInterval()的一个有趣小现象

    今天在使用setInterval()时,发现了一个有意思的事情 代码如下: var box=document.getElementById("box");//获取id为“box”的 ...

  5. android的apk逆向工程后的文件目录介绍

    一.用压缩软件打开apk,得到五个文件或文件夹 1.META-INF 2.res 3.AndroidManifest.xml 4.classes.dex 5.resources.arsc 二.xml文 ...

  6. jdk 编译器 对final字段的处理

    class FinalTest{     void a(){         final int i=10;         int j=10;     } }            stack=2, ...

  7. MFC 屏幕截图方法

    //获取当前屏幕的并且保存图片 LRESULT CFeetScanView::SaveViewBMP(WPARAM wParam, LPARAM lParam) { CRect rect; this- ...

  8. 阿里云弹性Web托管的URL重写问题

    今天将ThinkPHP写的网站搭到阿里云的弹性Web托管服务器上,出现路由问题 诸如访问 www.xxx.com/home/index.html会发生错误如下 页面报错: No input file ...

  9. 哈希(Hash)与加密(Encrypt)相关内容

    1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...

  10. MISC-极客大挑战-pen_and_apple.rar

    额   一道MISC的题目 关键词:NTFS数据流隐藏 链接:http://pan.baidu.com/s/1c14PM3A 密码:d7hn 拿到的是一个rar压缩文件,解压得到一个无节操的最近很流行 ...