package com.liying.monkey.core.util;

import java.io.IOException;
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 org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import com.liying.monkey.core.common.Constants.Zero; public class RedisUtil {
private static final Logger log = Logger.getLogger(RedisUtil.class);
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 15 * 1000;
//超时时间
private static int TIMEOUT = 10 * 1000; private static JedisPool jedisPool = null; /**
* Jedis实例获取返回码
*
* @author jqlin
*
*/
public static class JedisStatus{
/**Jedis实例获取失败*/
public static final long FAIL_LONG = -5L;
/**Jedis实例获取失败*/
public static final int FAIL_INT = -5;
/**Jedis实例获取失败*/
public static final String FAIL_STRING = "-5";
} private static void initialPool() {
//Redis服务器IP
String HOST = PropertyReader.get("redis.native.host");
//Redis的端口号
int PORT = NumberUtils.toInt(PropertyReader.get("redis.native.port"), 6379);
//访问密码
String AUTH = PropertyReader.get("redis.native.password"); try {
JedisPoolConfig config = new JedisPoolConfig();
//最大连接数,如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxTotal(NumberUtils.toInt(PropertyReader.get("redis.native.maxTotal"), 400));
//最大空闲数,控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
config.setMaxIdle(NumberUtils.toInt(PropertyReader.get("redis.native.maxIdle"), 50));
//最小空闲数
config.setMinIdle(NumberUtils.toInt(PropertyReader.get("redis.native.minIdle"), 10));
//是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
config.setTestOnBorrow(true);
//在return给pool时,是否提前进行validate操作
config.setTestOnReturn(true);
//在空闲时检查有效性,默认false
config.setTestWhileIdle(true);
//表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;
//这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
config.setMinEvictableIdleTimeMillis(30000);
//表示idle object evitor两次扫描之间要sleep的毫秒数
config.setTimeBetweenEvictionRunsMillis(60000);
//表示idle object evitor每次扫描的最多的对象数
config.setNumTestsPerEvictionRun(1000);
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(MAX_WAIT); if (StringUtils.isNotBlank(AUTH)) {
jedisPool = new JedisPool(config, HOST, PORT, TIMEOUT, AUTH);
} else {
jedisPool = new JedisPool(config, HOST, PORT, TIMEOUT);
}
} catch (Exception e) {
if(jedisPool != null){
jedisPool.close();
}
log.error("初始化Redis连接池失败", e);
}
} /**
* 初始化Redis连接池
*/
static {
initialPool();
} /**
* 在多线程环境同步初始化
*/
private static synchronized void poolInit() {
if (jedisPool == null) {
initialPool();
}
} /**
* 同步获取Jedis实例
*
* @return Jedis
*/
public static Jedis getJedis() {
if (jedisPool == null) {
poolInit();
} Jedis jedis = null;
try {
if (jedisPool != null) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
log.error("同步获取Jedis实例失败" + e.getMessage(), e);
returnBrokenResource(jedis);
} return jedis;
} /**
* 释放jedis资源
*
* @param jedis
*/
@SuppressWarnings("deprecation")
public static void returnResource(final Jedis jedis) {
if (jedis != null && jedisPool != null) {
jedisPool.returnResource(jedis);
}
} @SuppressWarnings("deprecation")
public static void returnBrokenResource(final Jedis jedis) {
if (jedis != null && jedisPool != null) {
jedisPool.returnBrokenResource(jedis);
}
} /**
* 设置值
*
* @param key
* @param value
* @return -5:Jedis实例获取失败<br/>OK:操作成功<br/>null:操作失败
* @author jqlin
*/
public static String set(String key, String value) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_STRING;
} String result = null;
try {
result = jedis.set(key, value);
} catch (Exception e) {
log.error("设置值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 设置值
*
* @param key
* @param value
* @param expire 过期时间,单位:秒
* @return -5:Jedis实例获取失败<br/>OK:操作成功<br/>null:操作失败
* @author jqlin
*/
public static String set(String key, String value, int expire) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_STRING;
} String result = null;
try {
result = jedis.set(key, value);
jedis.expire(key, expire);
} catch (Exception e) {
log.error("设置值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 获取值
*
* @param key
* @return
* @author jqlin
*/
public static String get(String key) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_STRING;
} String result = null;
try {
result = jedis.get(key);
} catch (Exception e) {
log.error("获取值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 设置key的过期时间
*
* @param key
* @param value -5:Jedis实例获取失败,1:成功,0:失败
* @return
* @author jqlin
*/
public static long expire(String key, int seconds) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_LONG;
} long result = Zero.LONG;
try {
result = jedis.expire(key, seconds);
} catch (Exception e) {
log.error(String.format("设置key=%s的过期时间失败:" + e.getMessage(), key), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 判断key是否存在
*
* @param key
* @return
* @author jqlin
*/
public static boolean exists(String key) {
Jedis jedis = getJedis();
if(jedis == null){
log.warn("Jedis实例获取为空");
return false;
} boolean result = false;
try {
result = jedis.exists(key);
} catch (Exception e) {
log.error(String.format("判断key=%s是否存在失败:" + e.getMessage(), key), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 删除key
*
* @param keys
* @return -5:Jedis实例获取失败,1:成功,0:失败
* @author jqlin
*/
public static long del(String... keys) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_LONG;
} long result = JedisStatus.FAIL_LONG;
try {
result = jedis.del(keys);
} catch (Exception e) {
log.error(String.format("删除key=%s失败:" + e.getMessage(), keys), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* set if not exists,若key已存在,则setnx不做任何操作
*
* @param key
* @param value key已存在,1:key赋值成功
* @return
* @author jqlin
*/
public static long setnx(String key, String value) {
long result = JedisStatus.FAIL_LONG; Jedis jedis = getJedis();
if(jedis == null){
return result;
} try {
result = jedis.setnx(key, value);
} catch (Exception e) {
log.error("设置值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* set if not exists,若key已存在,则setnx不做任何操作
*
* @param key
* @param value key已存在,1:key赋值成功
* @param expire 过期时间,单位:秒
* @return
* @author jqlin
*/
public static long setnx(String key, String value, int expire) {
long result = JedisStatus.FAIL_LONG; Jedis jedis = getJedis();
if(jedis == null){
return result;
} try {
result = jedis.setnx(key, value);
jedis.expire(key, expire);
} catch (Exception e) {
log.error("设置值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 在列表key的头部插入元素
*
* @param key
* @param values -5:Jedis实例获取失败,>0:返回操作成功的条数,0:失败
* @return
* @author jqlin
*/
public static long lpush(String key, String... values) {
long result = JedisStatus.FAIL_LONG; Jedis jedis = getJedis();
if(jedis == null){
return result;
} try {
result = jedis.lpush(key, values);
} catch (Exception e) {
log.error("在列表key的头部插入元素失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 在列表key的尾部插入元素
*
* @param key
* @param values -5:Jedis实例获取失败,>0:返回操作成功的条数,0:失败
* @return
* @author jqlin
*/
public static long rpush(String key, String... values) {
long result = JedisStatus.FAIL_LONG; Jedis jedis = getJedis();
if(jedis == null){
return result;
} try {
result = jedis.rpush(key, values);
} catch (Exception e) {
log.error("在列表key的尾部插入元素失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 返回存储在key列表的特定元素
*
*
* @param key
* @param start 开始索引,索引从0开始,0表示第一个元素,1表示第二个元素
* @param end 结束索引,-1表示最后一个元素,-2表示倒数第二个元素
* @return redis client获取失败返回null
* @author jqlin
*/
public static List<String> lrange(String key, long start, long end) {
Jedis jedis = getJedis();
if(jedis == null){
return null;
} List<String> result = null;
try {
result = jedis.lrange(key, start, end);
} catch (Exception e) {
log.error("查询列表元素失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 获取List缓存对象
*
* @param key
* @param start
* @param end
* @return List<T> 返回类型
* @author jqlin
*/
public static <T> List<T> range(String key, long start, long end, Class<T> clazz){
List<String> dataList = lrange(key, start, end);
if(CollectionUtils.isEmpty(dataList)){
return new ArrayList<T>();
} return JavaJsonConvert.json2List(dataList.toString(), clazz);
} /**
* 获取列表长度
*
* @param key -5:Jedis实例获取失败
* @return
* @author jqlin
*/
public static long llen(String key) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_LONG;
} long result = 0;
try {
result = jedis.llen(key);
} catch (Exception e) {
log.error("获取列表长度失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 移除等于value的元素<br/><br/>
* 当count>0时,从表头开始查找,移除count个;<br/>
* 当count=0时,从表头开始查找,移除所有等于value的;<br/>
* 当count<0时,从表尾开始查找,移除count个
*
* @param key
* @param count
* @param value
* @return -5:Jedis实例获取失败
* @author jqlin
*/
public static long lrem(String key, long count, String value) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_LONG;
} long result = 0;
try {
result = jedis.lrem(key, count, value);
} catch (Exception e) {
log.error("获取列表长度失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 对列表进行修剪
*
* @param key
* @param start
* @param end
* @return -5:Jedis实例获取失败,OK:命令执行成功
* @author jqlin
*/
public static String ltrim(String key, long start, long end) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_STRING;
} String result = "";
try {
result = jedis.ltrim(key, start, end);
} catch (Exception e) {
log.error("获取列表长度失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 设置对象
*
* @param key
* @param obj
* @return
* @author jqlin
*/
public static <T> String setObject(String key ,T obj){
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_STRING;
} String result = null;
try {
byte[] data=SerializeUtil.serialize(obj);
result = jedis.set(key.getBytes(), data);
} catch (Exception e) {
log.error("设置对象失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 获取对象
*
* @param key
* @return
* @author jqlin
*/
@SuppressWarnings("unchecked")
public static <T> T getObject(String key){
Jedis jedis = getJedis();
if(jedis == null){
return null;
} T result = null;
try {
byte[] data = jedis.get(key.getBytes());
if(data != null && data.length > 0){
result=(T)SerializeUtil.unserialize(data);
}
} catch (Exception e) {
log.error("获取对象失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 设置List集合(慎用)
*
* @param key
* @param dataList
* @return
* @author jqlin
*/
public synchronized static <T> T setList(String key, List<T> dataList){
Jedis jedis = getJedis();
if(jedis == null){
return null;
} T result = null;
try {
List<T> list = getList(key);
if(CollectionUtils.isNotEmpty(list)){
dataList.addAll(list);
} if(CollectionUtils.isNotEmpty(dataList)){
byte[] data = SerializeUtil.serializeToList(dataList);
jedis.set(key.getBytes(), data);
}else{//如果list为空,则设置一个空
jedis.set(key.getBytes(), "".getBytes());
}
} catch (Exception e) {
log.error("设置List集合失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 获取List集合(慎用)
*
* @param key
* @return
* @author jqlin
*/
public static <T> List<T> getList(String key){
Jedis jedis = getJedis();
if(jedis == null){
return null;
} try {
byte[] data = getJedis().get(key.getBytes());
if(data != null && data.length > 0){
return SerializeUtil.unserializeToList(data);
}
} catch (Exception e) {
log.error("获取List集合失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return null;
} /**
* 缓存Map赋值
*
* @param key
* @param field
* @param value
* @return -5:Jedis实例获取失败
* @author jqlin
*/
public static long hset(String key, String field, String value) {
Jedis jedis = getJedis();
if(jedis == null){
return JedisStatus.FAIL_LONG;
} long result = 0L;
try {
result = jedis.hset(key, field, value);
} catch (Exception e) {
log.error("缓存Map赋值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 获取缓存的Map值
*
* @param key
* @return
*/
public static String hget(String key, String field){
Jedis jedis = getJedis();
if(jedis == null){
return null;
} String result = null;
try {
result = jedis.hget(key, field);
} catch (Exception e) {
log.error("获取缓存的Map值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return result;
} /**
* 获取map所有的字段和值
*
* @param key
* @return
* @author jqlin
*/
public static Map<String, String> hgetAll(String key){
Map<String, String> map = new HashMap<String, String>(); Jedis jedis = getJedis();
if(jedis == null){
log.warn("Jedis实例获取为空");
return map;
} try {
map = jedis.hgetAll(key);
} catch (Exception e) {
log.error("获取map所有的字段和值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return map;
} /**
* 查看哈希表 key 中,指定的field字段是否存在。
*
* @param key
* @param field
* @return
* @author jqlin
*/
public static Boolean hexists(String key, String field){
Jedis jedis = getJedis();
if(jedis == null){
log.warn("Jedis实例获取为空");
return null;
} try {
return jedis.hexists(key, field);
} catch (Exception e) {
log.error("查看哈希表field字段是否存在失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return null;
} /**
* 获取所有哈希表中的字段
*
* @param key
* @return
* @author jqlin
*/
public static Set<String> hkeys(String key) {
Set<String> set = new HashSet<String>();
Jedis jedis = getJedis();
if(jedis == null){
log.warn("Jedis实例获取为空");
return set;
} try {
return jedis.hkeys(key);
} catch (Exception e) {
log.error("获取所有哈希表中的字段失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return null;
} /**
* 获取所有哈希表中的值
*
* @param key
* @return
* @author jqlin
*/
public static List<String> hvals(String key) {
List<String> list = new ArrayList<String>();
Jedis jedis = getJedis();
if(jedis == null){
log.warn("Jedis实例获取为空");
return list;
} try {
return jedis.hvals(key);
} catch (Exception e) {
log.error("获取所有哈希表中的值失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return null;
} /**
* 从哈希表 key 中删除指定的field
*
* @param key
* @param field
* @return
* @author jqlin
*/
public static long hdel(String key, String... fields){
Jedis jedis = getJedis();
if(jedis == null){
log.warn("Jedis实例获取为空");
return JedisStatus.FAIL_LONG;
} try {
return jedis.hdel(key, fields);
} catch (Exception e) {
log.error("map删除指定的field失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return 0;
} public static Set<String> keys(String pattern){
Set<String> keyList = new HashSet<String>();
Jedis jedis = getJedis();
if(jedis == null){
log.warn("Jedis实例获取为空");
return keyList;
} try {
keyList = jedis.keys(pattern);
} catch (Exception e) {
log.error("操作keys失败:" + e.getMessage(), e);
returnBrokenResource(jedis);
} finally {
returnResource(jedis);
} return keyList;
} public static void main(String[] args) throws IOException {
// System.out.println(hset("map", "a","3"));
// System.out.println(hset("map", "b","3"));
// System.out.println(hset("map", "c","3"));
Set<String> set = keys("lock*");
for(String key : set){
System.out.println(key);
}
}
}

Redis+Jedis封装工具类的更多相关文章

  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. JAVA之旅(五)——this,static,关键字,main函数,封装工具类,生成javadoc说明书,静态代码块

    JAVA之旅(五)--this,static,关键字,main函数,封装工具类,生成javadoc说明书,静态代码块 周末收获颇多,继续学习 一.this关键字 用于区分局部变量和成员变量同名的情况 ...

  8. Android OkHttp网络连接封装工具类

    package com.lidong.demo.utils; import android.os.Handler; import android.os.Looper; import com.googl ...

  9. 泛型(二)封装工具类CommonUtils-把一个Map转换成指定类型的javabean对象

    1.commons-beanutils的使用 commons-beanutils-1.9.3.jar 依赖 commons-logging-1.2.jar 代码1: String className ...

随机推荐

  1. DS8800后端的光纤通道交换式互连方式

    DS8800 使用SAS 硬盘.使用了FC 到SAS 转换,光纤通道交换技术被用于DS8800 后端. FC 技术是普遍用于在一个光纤通道仲裁环路(Fibre Channel Arbitrated L ...

  2. Swift3翻天覆地的改变

    经历了从swift 1.0 到2.0,一个版本之后代码居然就不兼容了.这如何在团队推广呢?没有想到3.0居然变化更加的大.有多大,来体会一下: UIFont.preferredFontForTextS ...

  3. android include使用[转]

    需要包含的xml文件,我这里就放了一个Button按钮: btn.xml: <?xml version="1.0" encoding="utf-8"?&g ...

  4. AWS–Sysops notes

    Monitoring, Metrics and Analysis 1.CouldWatch Introduction2.EC2 Status Troubleshooting3.Create A Cou ...

  5. HDU 1465 2045 已知结果往前推

    1465 不容易系列之一 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java ...

  6. vs2017常用快捷键

    项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示 Solution Explorer(解决方案资源管理器) Ctrl + Shift + A = ...

  7. SignalR2简易数据看板演示

    软件环境: 1.vs2015.windows7..net4.5 演示说明: 当点击按钮的时候,柱状图数值加1并实时变化 1.首先打开vs2015创建一个mvc项目,并安装SignalR2,具体操作可参 ...

  8. C# .NET 根据Url链接保存Image图片到本地磁盘

    根据一个Image的Url链接可以在浏览器中显示一个图片,如果要通过代码将图片保存在本地磁盘可以通过以下方式: 1.首先获取图片的二进制数组. static public byte[] GetByte ...

  9. CentOS 7 - 创建新用户

    当进行服务器操作时,我们尽量不要使用root用户进行操作,特别是当我们使用生产环境时. 本文我们将介绍CentOS 7下用户的创建. 创建新用户 adduser 用户名 更改用户密码 passwd 用 ...

  10. 第十三章 ReentrantLock 简介

    Java 5.0 提供的新的加锁机制:当内置加锁机制不适合时 , 作为一种可选择的高级功能 一个可重入的互斥锁 Lock,它具有与使用 synchronized 方法和语句所访问的隐式监视器锁相同的一 ...