-
-
Save cayzlh/98eafbb5ed71966e2ac603b55c479f3a to your computer and use it in GitHub Desktop.
JedisUtils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cayzlh.utils.redis; | |
import org.apache.log4j.Logger; | |
import org.springframework.beans.BeansException; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.ApplicationContextAware; | |
import redis.clients.jedis.Jedis; | |
import redis.clients.jedis.JedisPool; | |
import java.util.List; | |
import java.util.Set; | |
/** | |
* @author Ant丶 | |
* @date 2018-04-20. | |
*/ | |
public class JedisUtils implements ApplicationContextAware { | |
private static ApplicationContext applicationContext = null; | |
private static JedisPool jedisPool = null; | |
private static volatile Jedis jedis = null; | |
private static Logger logger = Logger.getLogger("JedisUtils"); | |
public JedisUtils() { | |
} | |
@Override | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | |
if (JedisUtils.applicationContext == null) { | |
//初始化 spring applicationContext | |
JedisUtils.applicationContext = applicationContext; | |
} | |
} | |
public static Jedis getJedis() { | |
if (jedis == null) { | |
synchronized (Jedis.class) { | |
if (jedis == null) { | |
jedis = getJedisPool().getResource(); | |
} | |
} | |
} | |
return jedis; | |
} | |
public static JedisPool getJedisPool() { | |
if (jedisPool == null) { | |
synchronized (JedisPool.class) { | |
if (jedisPool == null) { | |
jedisPool = applicationContext.getBean("jedisPool", JedisPool.class); | |
} | |
} | |
} | |
return jedisPool; | |
} | |
/** | |
* 根据key查看是否存在 | |
* | |
* @param key | |
* @return | |
*/ | |
public static boolean hasKey(String key) { | |
return getJedis().exists(key); | |
} | |
/** | |
* 设置key -value 形式数据 | |
* | |
* @param key | |
* @param value | |
* @return | |
*/ | |
public static boolean set(String key, String value) { | |
boolean result = false; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = "OK".equals(jedis.set(key, value)); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.set falid", e); | |
} | |
return result; | |
} | |
/** | |
* 设置 一个过期时间 | |
* | |
* @param key key | |
* @param value value | |
* @param timeOut 单位秒 | |
* @return | |
*/ | |
public static boolean set(String key, String value, int timeOut) { | |
boolean result = false; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = "OK".equals(jedis.setex(key, timeOut, value)); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.set falid", e); | |
} | |
return result; | |
} | |
public static boolean set(byte[] key, byte[] value) { | |
boolean result = false; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = "OK".equals(jedis.set(key, value)); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.set falid", e); | |
} | |
return result; | |
} | |
public static boolean set(byte[] key, byte[] value, int timeOut) { | |
boolean result = false; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = "OK".equals(jedis.setex(key, timeOut, value)); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.setex falid", e); | |
} | |
return result; | |
} | |
public static byte[] get(byte[] key) { | |
byte[] result = null; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = jedis.get(key); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.get falid", e); | |
} | |
return result; | |
} | |
/** | |
* 根据key获取value | |
* | |
* @param key | |
* @return | |
*/ | |
public static String get(String key) { | |
String result = null; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = jedis.get(key); | |
closeJedis(jedis); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.get falid", e); | |
} | |
return result; | |
} | |
/** | |
* 根据通配符获取所有匹配的key | |
* | |
* @param pattern | |
* @return | |
*/ | |
public static Set<String> getKeys(String pattern) { | |
return getJedis().keys(pattern); | |
} | |
/** | |
* 根据key删除 | |
* | |
* @param key | |
*/ | |
public static void del(String key) { | |
getJedis().del(key); | |
} | |
/** | |
* 根据key获取过期时间 | |
* | |
* @param key | |
* @return | |
*/ | |
public static long getTimeOut(String key) { | |
return getJedis().ttl(key); | |
} | |
/** | |
* 清空数据 【慎用啊!】 | |
*/ | |
public static void flushDB() { | |
getJedis().flushDB(); | |
} | |
/** | |
* 刷新过期时间 | |
* | |
* @param key | |
* @param timeOut | |
* @return | |
*/ | |
public static long refreshLiveTime(String key, int timeOut) { | |
return getJedis().expire(key, timeOut); | |
} | |
/** | |
* 返回 key 所储存的值的类型; | |
* <p> | |
* 返回值: none (key不存在) string (字符串) list (列表) set (集合) zset (有序集) hash (哈希表) | |
* | |
* @param key | |
* @return | |
*/ | |
public static String type(String key) { | |
String result = ""; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = jedis.type(key); | |
closeJedis(jedis); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.type falid", e); | |
} | |
return result; | |
} | |
/** | |
* 同时设置一个或多个 key-value 对。 如果某个给定 key 已经存在,那么 MSET | |
* 会用新值覆盖原来的旧值,如果这不是你所希望的效果,请考虑使用 MSETNX 命令:它只会在所有给定 key 都不存在的情况下进行设置操作。 | |
* MSET 是一个原子性(atomic)操作,所有给定 key 都会在同一时间内被设置,某些给定 key 被更新而另一些给定 key | |
* 没有改变的情况,不可能发生。 可用版本: >= 1.0.1 时间复杂度: O(N), N 为要设置的 key 数量。 返回值: 总是返回 OK | |
* (因为 MSET 不可能失败) | |
* | |
* @param array | |
* @return | |
*/ | |
public static boolean mset(String[] array) { | |
boolean result = false; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
String status = jedis.mset(array); | |
if ("OK".equalsIgnoreCase(status)) { | |
result = true; | |
} | |
closeJedis(jedis); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.set falid", e); | |
} | |
return result; | |
} | |
/** | |
* 返回所有(一个或多个)给定 key 的值。 | |
* <p> | |
* 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。因此,该命令永不失败。 | |
* <p> | |
* 可用版本: >= 1.0.0 时间复杂度: O(N) , N 为给定 key 的数量。 返回值: 一个包含所有给定 key 的值的列表。 | |
* | |
* @param array | |
* @return | |
*/ | |
public static List<String> mget(String[] array) { | |
List<String> mget = null; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
mget = jedis.mget(array); | |
closeJedis(jedis); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.set falid", e); | |
} | |
return mget; | |
} | |
/** | |
* 将哈希表 key 中的域 field 的值设为 value 。 如果 key 不存在,一个新的哈希表被创建并进行 HSET 操作。 如果域 | |
* field 已经存在于哈希表中,旧值将被覆盖。 | |
* <p> | |
* 返回值: 如果 field 是哈希表中的一个新建域,并且值设置成功,返回 1 。 如果哈希表中域 field 已经存在且旧值已被新值覆盖,返回 0 | |
* 。 | |
* | |
* @param key | |
* @param field | |
* @param value | |
* @return | |
*/ | |
public static long hset(String key, String field, String value) { | |
long result = -10000; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = jedis.hset(key, field, value); | |
closeJedis(jedis); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.hset falid", e); | |
} | |
return result; | |
} | |
/** | |
* 获取哈希表 key 中给定域 field 的值。 | |
* <p> | |
* 返回值: 给定域的值。 当给定域不存在或是给定 key 不存在时,返回 nil 。 | |
* | |
* @param key | |
* @param field | |
* @return | |
*/ | |
public static String hget(String key, String field) { | |
String result = null; | |
Jedis jedis = null; | |
try { | |
jedis = getJedis(); | |
result = jedis.hget(key, field); | |
closeJedis(jedis); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("JedisCache.hget falid", e); | |
} | |
return result; | |
} | |
public static void closeJedis(Jedis jedis) { | |
try { | |
jedis.close(); | |
} catch (Exception e) { | |
closeBrokenJedis(jedis); | |
logger.error("reids sentinel closeJedis falid.{}", e); | |
} | |
} | |
/** | |
* 归还不可用的Jedis给池 | |
* | |
* @param jedis | |
*/ | |
private static void closeBrokenJedis(Jedis jedis) { | |
try { | |
jedis.close(); | |
} catch (Exception e) { | |
logger.error("reids sentinel closeBrokenJedis falid", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment