通常在访问量大数据更新频率不高的系统中会使用第三方的缓存组件来降低数据库服务的负载,鉴于模块独立分工独立的考虑,针对缓存组件操作的工作全部应该统一接口对其他业务提供服务,这样业务操作只需要关注业务实现不需要关注缓存的具体细节。
本例以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. 解决protobuf不能直接在IOS上使用,利用protobuf-net在IOS上通讯

    ---------------------------------------------------------------------------------------------------- ...

  2. iOS socket保持后台连接 ios9.0 xcode8.0

    可以保持后台,但申请上架是肯定会被拒的 本教程是基于AsyncSocket库的简单开发! socket机制今天就不说了,毕竟百度上太多太详尽了! 1.先new一个工程: ​2.要写socket的界面遵 ...

  3. JPA 各种基本用法

    查询部分属性 通常来说,都是针对 Entity 类的查询,返回的也是被查询的 Entity 类的实体.J P QL 也允许我们直接查询返回我们需要的属性,而不是返回整个 Entity .在一些 Ent ...

  4. C++关于Condition Variable

    #include <condition_variable> #include <mutex> #include <future> #include <iost ...

  5. 对于undefined和null,还有处理这一类的数组

    var total=0; var data=new Array(5);//定义了data数组,length为5,但是都是元素都是undefined. for(i=0;i<data.length; ...

  6. 好久没写Blog了

    上一年的经历: <炸年兽>搞了一阵后,美术去创业了.. 和另一个美术断断续续,做了个<斗战圣佛>,挺山寨的,都没敢跟别人说. 不管怎么说也算是自己上了一个appStore的游戏 ...

  7. (转)jQuery插件 -- Form表单插件jquery.form.js

    beforeSubmit: validate function validate(formData, jqForm, options) { //在这里对表单进行验证,如果不符合规则,将返回false来 ...

  8. 【杂】孔明锁6根解法 & 九连环的拆卸方法及还原

    **************************** Part1: 孔明锁6根解法: **************************** 第一步,编号: 第二步,按照编号组装: 第三步,完成 ...

  9. iOS UITextField限制输入数字

    有时候项目中要求文本框中只能输入数字,如:价格.公里数.费用等等,一般的文本框不限制输入的格式,这时候只能强制限制输入框的输入格式了,代码如下: #import "ViewControlle ...

  10. CURL常用命令

    下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://www.centos.org 通过-o/-O选项保存下载的文件到指定的文件中:-o:将文件保存为命令行中指定的文件名 ...