使用RedisTemplate进行Redis存取的工具类设计
通常在访问量大数据更新频率不高的系统中会使用第三方的缓存组件来降低数据库服务的负载,鉴于模块独立分工独立的考虑,针对缓存组件操作的工作全部应该统一接口对其他业务提供服务,这样业务操作只需要关注业务实现不需要关注缓存的具体细节。
本例以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存取的工具类设计的更多相关文章
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(五): 数据表设计、使用 jwt、redis、sms 工具类完善注册登录逻辑
(1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y-h/p ...
- Redis,JedisPool工具类
Redis,JedisPool工具类 1.JedisPool 详细配置解释代码 2.Jedis工具类 导入相关依赖: commons-pool2-2.3.jar jedis-2.7.0.jar 1.J ...
- JFreeChart绘制XY折线图(工具类设计)
准备用Java写通信的仿真平台作为毕业设计,相比matlab绘图,Java绘图需要自己去写很多工具类,博主在这采用了JFreeChart的开源解决方案,摸索着自己写了一个XY折线图工具类,话不多说贴源 ...
- SpringBoot整合Redis并完成工具类
SpringBoot整合Redis的资料很多,但是我只需要整合完成后,可以操作Redis就可以了,所以不需要配合缓存相关的注解使用(如@Cacheable),而且我的系统框架用的日志是log4j,不是 ...
- Redis使用及工具类
原地址:https://www.cnblogs.com/wyy123/p/6078593.html [学会安装redis] 从redis.io下载最新版redis-X.Y.Z.tar.gz后解压,然后 ...
随机推荐
- Three.js基本 Demo
对于新手来说,几个简单的例子非常实用,偶然发现几个不错的Demo,分享给大家! Three.js基本 Demo 1.最基本的Hello World:http://stemkoski.github.io ...
- 华为交换机netstream配置
1.配置交换机的流发送 [系统视图]ip netstream timeout active 100 流活跃时间 [系统视图]ip netstream timeout inactive ...
- JUnit4参数的使用
用JUnit4进行参数化测试 参数化测试是一个JUnit 3不具备的功能. 基本使用方法 @RunWith 当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解 ...
- flash 居中问题
如果舞台是1000的宽度,要剧中比较容易 mc1.x = (1000-400)/2; 这样就居中了,来看原理,首先我们要舞台居中,很容易就想到一个数字 1000/2 结果是500 但是x对舞台的中央是 ...
- 在VS的EF中连接MySQL
VS没有主动提供那些繁多的连接器,需要的话得自己再安装这些第三方程序包. MySQL为windows平台开发者提供了许多程序包:http://dev.mysql.com/downloads/windo ...
- C# 字符编码解码 Encoder 和Decoder
在网络传输和文件操作中,如果数据量很大,需要将其划分为较小的快,此时可能出现一个数据块的末尾是一个不匹配的高代理项,而与其匹配的低代理项在下一个数据块. 这时候使用Encoding的GetBytes方 ...
- 采用OLEDB数据库方式向指定的Excel添加数据,怪像!
我们都知道,对Excel进行操作,其实方法是多种多样的,例如采用Office.Interop;例如采用ASPCell:例如采用NPOI:再例如采用数据库连接的方式OLEDB,etc. 还是先说说背景吧 ...
- 手动刷新magento的索引管理方法
当我们网站商品很多的时候,比如有几千件,我们刷新Magento的索引管理(Index Management)经常会失败.那么后台刷新不了,我们还可以通过命令行来刷新. 使用命令行来刷新索引管理会极大降 ...
- REDIS 字典数据结构
对于REDIS来讲 其实就是一个字典结构,key ---->value 就是一个典型的字典结构 [当然 对于vaule来讲的话,有不同的内存组织结构 这是后话] 试想一个这样的存储场景: ...
- Python 基礎 - 淺copy補充說明
在 import copy 這個模塊裡 基於第一個列表來做淺copy,實際上第二個列表裡的元素,是第一個列表的 引用. 接下來介紹 淺copy有三種方式可以使用 #!/usr/bin/env pyth ...