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

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  2. Android深度探索--HAL与驱动开发----第十章读书笔记

    printk函数的用法于-printf 函数类似,只不过printk函数运行在内核空间, printf函数运行在用户空间.也就是说,像Linux 驱动这样的Linux内核程序只能使用printk 函数 ...

  3. 7.2.3 使用RenderTargetBitmap类生成图片

    RenderTargetBitmap类可以将可视化对象转换为位图,也就是说它可以将任意的UIElement以位图的形式呈现.那么我们在实际的编程中通常会利用RenderTargetBitmap类来对U ...

  4. layabox 引擎

    layabox H5引擎性能之王LayaAir http://ldc.layabox.com/

  5. Nginx配置指定媒体类型文件强制下载

    由于业务需要,在点击显示链接(如www.xxx.com/2015-01-15/xxx.png)显示媒体资源(如图片.视频.音频.文档),而在点击下载链接(如www.xxx.com/2015-01-15 ...

  6. C# xpath

    XPath最通俗的教程(ZZ)   以下是本人找到的最完整最易懂的XPath教程,不敢私藏,拿出来与大家分享.帮我点旁边的google广告呀. 实例 1基本的XPath语法类似于在一个文件系统中定位文 ...

  7. C#常用操作类库四(File操作类)

    public class FileHelper : IDisposable { private bool _alreadyDispose = false; #region 构造函数 public Fi ...

  8. Oracle导出表(即DMP文件)的两种方法

    转载:http://blog.csdn.net/lanpy88/article/details/7580691   方法一:利用PL/SQL Developer工具导出: 菜单栏---->Too ...

  9. div中显示页面

    在css中显示页面,在页面布局中很多时候都要在一个div 里显示某些页面.在这里写下我用到的一种方式. <script type="text/javascript"> ...

  10. Logistic回归分类算法原理分析与代码实现

    前言 本文将介绍机器学习分类算法中的Logistic回归分类算法并给出伪代码,Python代码实现. (说明:从本文开始,将接触到最优化算法相关的学习.旨在将这些最优化的算法用于训练出一个非线性的函数 ...