|
|
@ -2,7 +2,10 @@ package wiki.tall.ccmq.common.util; |
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.util.CollectionUtils; |
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
import java.util.Set; |
|
|
import java.util.Set; |
|
|
@ -11,12 +14,19 @@ import java.util.concurrent.TimeUnit; |
|
|
/** |
|
|
/** |
|
|
* @author __zHangSan |
|
|
* @author __zHangSan |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
@Component |
|
|
public class RedisUtil { |
|
|
public class RedisUtil { |
|
|
|
|
|
|
|
|
public static RedisTemplate<String,Object> getRedisTemplate(){ |
|
|
|
|
|
return SpringContextUtils.getBean("redisTemplate",RedisTemplate.class); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RedisTemplate<String, Object> redisTemplate; |
|
|
|
|
|
private static RedisUtil util; |
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
|
|
public void init(){ |
|
|
|
|
|
util = this; |
|
|
|
|
|
util.redisTemplate = this.redisTemplate; |
|
|
|
|
|
} |
|
|
/** |
|
|
/** |
|
|
* 指定缓存失效时间 |
|
|
* 指定缓存失效时间 |
|
|
* |
|
|
* |
|
|
@ -27,7 +37,7 @@ public class RedisUtil { |
|
|
public static boolean expire(String key, long time) { |
|
|
public static boolean expire(String key, long time) { |
|
|
try { |
|
|
try { |
|
|
if (time > 0) { |
|
|
if (time > 0) { |
|
|
getRedisTemplate().expire(key, time, TimeUnit.SECONDS); |
|
|
util.redisTemplate.expire(key, time, TimeUnit.SECONDS); |
|
|
} |
|
|
} |
|
|
return true; |
|
|
return true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
@ -43,7 +53,7 @@ public class RedisUtil { |
|
|
* @return 时间(秒) 返回0代表为永久有效 |
|
|
* @return 时间(秒) 返回0代表为永久有效 |
|
|
*/ |
|
|
*/ |
|
|
public static long getExpire(String key) { |
|
|
public static long getExpire(String key) { |
|
|
return getRedisTemplate().getExpire(key, TimeUnit.SECONDS); |
|
|
return util.redisTemplate.getExpire(key, TimeUnit.SECONDS); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -54,7 +64,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean hasKey(String key) { |
|
|
public static boolean hasKey(String key) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().hasKey(key); |
|
|
return util.redisTemplate.hasKey(key); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return false; |
|
|
return false; |
|
|
@ -70,9 +80,9 @@ public class RedisUtil { |
|
|
public static void del(String... keys) { |
|
|
public static void del(String... keys) { |
|
|
if (keys != null && keys.length > 0) { |
|
|
if (keys != null && keys.length > 0) { |
|
|
if (keys.length == 1) { |
|
|
if (keys.length == 1) { |
|
|
getRedisTemplate().delete(keys[0]); |
|
|
util.redisTemplate.delete(keys[0]); |
|
|
} else { |
|
|
} else { |
|
|
getRedisTemplate().delete(CollectionUtils.arrayToList(keys)); |
|
|
util.redisTemplate.delete(CollectionUtils.arrayToList(keys)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -85,7 +95,7 @@ public class RedisUtil { |
|
|
* @return 值 |
|
|
* @return 值 |
|
|
*/ |
|
|
*/ |
|
|
public static Object get(String key) { |
|
|
public static Object get(String key) { |
|
|
return key == null ? null : getRedisTemplate().opsForValue().get(key); |
|
|
return key == null ? null : util.redisTemplate.opsForValue().get(key); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -97,7 +107,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean set(String key, Object value) { |
|
|
public static boolean set(String key, Object value) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForValue().set(key, value); |
|
|
util.redisTemplate.opsForValue().set(key, value); |
|
|
return true; |
|
|
return true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -117,7 +127,7 @@ public class RedisUtil { |
|
|
public static boolean set(String key, Object value, long seconds) { |
|
|
public static boolean set(String key, Object value, long seconds) { |
|
|
try { |
|
|
try { |
|
|
if (seconds > 0) { |
|
|
if (seconds > 0) { |
|
|
getRedisTemplate().opsForValue().set(key, value, seconds, TimeUnit.SECONDS); |
|
|
util.redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS); |
|
|
} else { |
|
|
} else { |
|
|
set(key, value); |
|
|
set(key, value); |
|
|
} |
|
|
} |
|
|
@ -139,7 +149,7 @@ public class RedisUtil { |
|
|
if (delta < 0) { |
|
|
if (delta < 0) { |
|
|
throw new RuntimeException("递增因子必须大于0"); |
|
|
throw new RuntimeException("递增因子必须大于0"); |
|
|
} |
|
|
} |
|
|
return getRedisTemplate().opsForValue().increment(key, delta); |
|
|
return util.redisTemplate.opsForValue().increment(key, delta); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -153,7 +163,7 @@ public class RedisUtil { |
|
|
if (delta < 0) { |
|
|
if (delta < 0) { |
|
|
throw new RuntimeException("递减因子必须大于0"); |
|
|
throw new RuntimeException("递减因子必须大于0"); |
|
|
} |
|
|
} |
|
|
return getRedisTemplate().opsForValue().increment(key, -delta); |
|
|
return util.redisTemplate.opsForValue().increment(key, -delta); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//================================Map=================================
|
|
|
//================================Map=================================
|
|
|
@ -165,7 +175,7 @@ public class RedisUtil { |
|
|
* @return 值 |
|
|
* @return 值 |
|
|
*/ |
|
|
*/ |
|
|
public static Object hget(String key, String item) { |
|
|
public static Object hget(String key, String item) { |
|
|
return getRedisTemplate().opsForHash().get(key, item); |
|
|
return util.redisTemplate.opsForHash().get(key, item); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -175,7 +185,7 @@ public class RedisUtil { |
|
|
* @return 对应的多个键值 |
|
|
* @return 对应的多个键值 |
|
|
*/ |
|
|
*/ |
|
|
public static Map<Object, Object> hmget(String key) { |
|
|
public static Map<Object, Object> hmget(String key) { |
|
|
return getRedisTemplate().opsForHash().entries(key); |
|
|
return util.redisTemplate.opsForHash().entries(key); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -187,7 +197,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean hmset(String key, Map<String, Object> map) { |
|
|
public static boolean hmset(String key, Map<String, Object> map) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForHash().putAll(key, map); |
|
|
util.redisTemplate.opsForHash().putAll(key, map); |
|
|
return true; |
|
|
return true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -205,7 +215,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean hmset(String key, Map<String, Object> map, long time) { |
|
|
public static boolean hmset(String key, Map<String, Object> map, long time) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForHash().putAll(key, map); |
|
|
util.redisTemplate.opsForHash().putAll(key, map); |
|
|
if (time > 0) { |
|
|
if (time > 0) { |
|
|
expire(key, time); |
|
|
expire(key, time); |
|
|
} |
|
|
} |
|
|
@ -226,7 +236,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean hset(String key, String item, Object value) { |
|
|
public static boolean hset(String key, String item, Object value) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForHash().put(key, item, value); |
|
|
util.redisTemplate.opsForHash().put(key, item, value); |
|
|
return true; |
|
|
return true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -245,7 +255,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean hset(String key, String item, Object value, long time) { |
|
|
public static boolean hset(String key, String item, Object value, long time) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForHash().put(key, item, value); |
|
|
util.redisTemplate.opsForHash().put(key, item, value); |
|
|
if (time > 0) { |
|
|
if (time > 0) { |
|
|
expire(key, time); |
|
|
expire(key, time); |
|
|
} |
|
|
} |
|
|
@ -263,7 +273,7 @@ public class RedisUtil { |
|
|
* @param item 项 可以使多个 不能为null |
|
|
* @param item 项 可以使多个 不能为null |
|
|
*/ |
|
|
*/ |
|
|
public static void hdel(String key, Object... item) { |
|
|
public static void hdel(String key, Object... item) { |
|
|
getRedisTemplate().opsForHash().delete(key, item); |
|
|
util.redisTemplate.opsForHash().delete(key, item); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -274,7 +284,7 @@ public class RedisUtil { |
|
|
* @return true 存在 false不存在 |
|
|
* @return true 存在 false不存在 |
|
|
*/ |
|
|
*/ |
|
|
public static boolean hHasKey(String key, String item) { |
|
|
public static boolean hHasKey(String key, String item) { |
|
|
return getRedisTemplate().opsForHash().hasKey(key, item); |
|
|
return util.redisTemplate.opsForHash().hasKey(key, item); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -286,7 +296,7 @@ public class RedisUtil { |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
public static double hincr(String key, String item, double by) { |
|
|
public static double hincr(String key, String item, double by) { |
|
|
return getRedisTemplate().opsForHash().increment(key, item, by); |
|
|
return util.redisTemplate.opsForHash().increment(key, item, by); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -298,7 +308,7 @@ public class RedisUtil { |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
public static double hdecr(String key, String item, double by) { |
|
|
public static double hdecr(String key, String item, double by) { |
|
|
return getRedisTemplate().opsForHash().increment(key, item, -by); |
|
|
return util.redisTemplate.opsForHash().increment(key, item, -by); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//============================set=============================
|
|
|
//============================set=============================
|
|
|
@ -311,7 +321,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean sHas(String key, Object value) { |
|
|
public static boolean sHas(String key, Object value) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForSet().isMember(key, value); |
|
|
return util.redisTemplate.opsForSet().isMember(key, value); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return false; |
|
|
return false; |
|
|
@ -326,7 +336,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static Set<Object> sGet(String key) { |
|
|
public static Set<Object> sGet(String key) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForSet().members(key); |
|
|
return util.redisTemplate.opsForSet().members(key); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return null; |
|
|
return null; |
|
|
@ -341,7 +351,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static long sSet(String key, Object... values) { |
|
|
public static long sSet(String key, Object... values) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForSet().add(key, values); |
|
|
return util.redisTemplate.opsForSet().add(key, values); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return 0; |
|
|
return 0; |
|
|
@ -358,7 +368,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static long sSetAndTime(String key, long time, Object... values) { |
|
|
public static long sSetAndTime(String key, long time, Object... values) { |
|
|
try { |
|
|
try { |
|
|
Long count = getRedisTemplate().opsForSet().add(key, values); |
|
|
Long count = util.redisTemplate.opsForSet().add(key, values); |
|
|
if (time > 0) {expire(key, time);} |
|
|
if (time > 0) {expire(key, time);} |
|
|
return count; |
|
|
return count; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
@ -375,7 +385,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static long sGetSetSize(String key) { |
|
|
public static long sGetSetSize(String key) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForSet().size(key); |
|
|
return util.redisTemplate.opsForSet().size(key); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return 0; |
|
|
return 0; |
|
|
@ -391,7 +401,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static long setRemove(String key, Object... values) { |
|
|
public static long setRemove(String key, Object... values) { |
|
|
try { |
|
|
try { |
|
|
Long count = getRedisTemplate().opsForSet().remove(key, values); |
|
|
Long count = util.redisTemplate.opsForSet().remove(key, values); |
|
|
return count; |
|
|
return count; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -407,7 +417,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static Object sPop(String key) { |
|
|
public static Object sPop(String key) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForSet().pop(key); |
|
|
return util.redisTemplate.opsForSet().pop(key); |
|
|
}catch (Exception e){ |
|
|
}catch (Exception e){ |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return null; |
|
|
return null; |
|
|
@ -425,7 +435,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static List<Object> lGet(String key, long start, long end) { |
|
|
public static List<Object> lGet(String key, long start, long end) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForList().range(key, start, end); |
|
|
return util.redisTemplate.opsForList().range(key, start, end); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return null; |
|
|
return null; |
|
|
@ -440,7 +450,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static long lGetListSize(String key) { |
|
|
public static long lGetListSize(String key) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForList().size(key); |
|
|
return util.redisTemplate.opsForList().size(key); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return 0; |
|
|
return 0; |
|
|
@ -456,7 +466,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static Object lGetIndex(String key, long index) { |
|
|
public static Object lGetIndex(String key, long index) { |
|
|
try { |
|
|
try { |
|
|
return getRedisTemplate().opsForList().index(key, index); |
|
|
return util.redisTemplate.opsForList().index(key, index); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
return null; |
|
|
return null; |
|
|
@ -472,7 +482,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean lSet(String key, Object value) { |
|
|
public static boolean lSet(String key, Object value) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForList().rightPush(key, value); |
|
|
util.redisTemplate.opsForList().rightPush(key, value); |
|
|
return true; |
|
|
return true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -490,7 +500,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean lSet(String key, Object value, long seconds) { |
|
|
public static boolean lSet(String key, Object value, long seconds) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForList().rightPush(key, value); |
|
|
util.redisTemplate.opsForList().rightPush(key, value); |
|
|
if (seconds > 0) { |
|
|
if (seconds > 0) { |
|
|
expire(key, seconds); |
|
|
expire(key, seconds); |
|
|
} |
|
|
} |
|
|
@ -510,7 +520,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean lSet(String key, List<Object> value) { |
|
|
public static boolean lSet(String key, List<Object> value) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForList().rightPushAll(key, value); |
|
|
util.redisTemplate.opsForList().rightPushAll(key, value); |
|
|
return true; |
|
|
return true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -528,7 +538,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean lSet(String key, List<Object> value, long time) { |
|
|
public static boolean lSet(String key, List<Object> value, long time) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForList().rightPushAll(key, value); |
|
|
util.redisTemplate.opsForList().rightPushAll(key, value); |
|
|
if (time > 0) { |
|
|
if (time > 0) { |
|
|
expire(key, time); |
|
|
expire(key, time); |
|
|
} |
|
|
} |
|
|
@ -549,7 +559,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static boolean lUpdateIndex(String key, long index, Object value) { |
|
|
public static boolean lUpdateIndex(String key, long index, Object value) { |
|
|
try { |
|
|
try { |
|
|
getRedisTemplate().opsForList().set(key, index, value); |
|
|
util.redisTemplate.opsForList().set(key, index, value); |
|
|
return true; |
|
|
return true; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -567,7 +577,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static long lRemove(String key, long count, Object value) { |
|
|
public static long lRemove(String key, long count, Object value) { |
|
|
try { |
|
|
try { |
|
|
Long remove = getRedisTemplate().opsForList().remove(key, count, value); |
|
|
Long remove = util.redisTemplate.opsForList().remove(key, count, value); |
|
|
return remove; |
|
|
return remove; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
@ -582,7 +592,7 @@ public class RedisUtil { |
|
|
*/ |
|
|
*/ |
|
|
public static Object lPop(String key) { |
|
|
public static Object lPop(String key) { |
|
|
try { |
|
|
try { |
|
|
Object value = getRedisTemplate().opsForList().leftPop(key); |
|
|
Object value = util.redisTemplate.opsForList().leftPop(key); |
|
|
return value; |
|
|
return value; |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
|