使用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后解压,然后 ...
随机推荐
- C语言文法分析
程序 → <外部声明>|<程序><外部声明> <外部声明> → <函数定义> | <声明> <函数定义> → < ...
- 推荐一个Android Studio很实用的插件android-butterknife-zelezny
当你按钮很多,你又懒得写代码,虽然通过重写onClick比较方便,那么我们能不能连这个switch都省略掉呢? 答案是肯定的,下面这个插件就帮我们解决了这个问题! Android-butterknif ...
- android优秀Github源码整理
1.https://github.com/sd6352051/NiftyNotification 2.https://github.com/sd6352051/NiftyDialogEffects 3 ...
- 用jQuery做一个三级菜单,鼠标移动到二级菜单的选项上,然后再迅速离开后,当鼠标再移动到该一级菜单或其他二级菜单选项,三级菜单也会显示。
用jQuery做一个三级菜单,鼠标移动到二级菜单的选项上,然后再迅速离开后,当鼠标再移动到该一级菜单或其他二级菜单选项,三级菜单也会显示. 原因:在为一个元素绑定hover事件之后,用户把光标移入元素 ...
- const,static,extern简介
一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 编译时刻:宏是预编译(编译之前处理),const是编译阶段. 编 ...
- HTTP 响应头信息
HTTP 响应头信息 HTTP请求头提供了关于请求,响应或者其他的发送实体的信息. 在本章节中我们将具体来介绍HTTP响应头信息.
- Android WebView 保存cache至External Storage
源博客: http://www.devahead.com/blog/2012/01/saving-the-android-webview-cache-on-the-sd-card/ 其中心思想是,通过 ...
- Javascript 事件对象进阶(二)拖拽的应用 - 登录框的拖拽
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- .htaccess详解及.htaccess参数说明【转】
目录(?)[-] htaccess 详解 htaccess rewrite 规则详细说明 RewriteEngine OnOff RewriteBase URL-path RewriteCond Te ...
- [WPF]GridView或DataGrid中自定义样式:依据某一列设定其对应行的样式(背景色,字体等)
附效果照一张: 本方法使用StyleSelector来 获得依据自定义逻辑的style. ① class ConditionalStyleSelector : StyleSelector { publ ...