|
|
@@ -1,9 +1,12 @@
|
|
|
package com.ctsi.utils;
|
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.data.redis.core.*;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.Serializable;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
@@ -18,18 +21,19 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Component("redisUtil")
|
|
|
public class RedisUtil {
|
|
|
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private RedisTemplate redisTemplate;
|
|
|
|
|
|
- private static double size = Math.pow(2, 32);
|
|
|
+ private static final double size = Math.pow(2, 32);
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 写入缓存
|
|
|
*
|
|
|
- * @param key
|
|
|
* @param offset 位 8Bit=1Byte
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public boolean setBit(String key, long offset, boolean isShow) {
|
|
|
boolean result = false;
|
|
|
@@ -38,7 +42,7 @@ public class RedisUtil {
|
|
|
operations.setBit(key, offset, isShow);
|
|
|
result = true;
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
@@ -46,9 +50,8 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 写入缓存
|
|
|
*
|
|
|
- * @param key
|
|
|
* @param offset
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public boolean getBit(String key, long offset) {
|
|
|
boolean result = false;
|
|
|
@@ -56,7 +59,7 @@ public class RedisUtil {
|
|
|
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
|
|
result = operations.getBit(key, offset);
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
@@ -65,9 +68,8 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 写入缓存
|
|
|
*
|
|
|
- * @param key
|
|
|
* @param value
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public boolean set(final String key, Object value) {
|
|
|
boolean result = false;
|
|
|
@@ -76,7 +78,7 @@ public class RedisUtil {
|
|
|
operations.set(key, value);
|
|
|
result = true;
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
@@ -86,7 +88,7 @@ public class RedisUtil {
|
|
|
*
|
|
|
* @param key
|
|
|
* 键
|
|
|
- * @return 失效时间(秒)
|
|
|
+ * @ return 失效时间(秒)
|
|
|
*/
|
|
|
public long getExpire(String key) {
|
|
|
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
|
|
@@ -97,7 +99,7 @@ public class RedisUtil {
|
|
|
*
|
|
|
* @param key
|
|
|
* @param value
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public boolean set(final String key, Object value, Long expireTime) {
|
|
|
boolean result = false;
|
|
|
@@ -107,7 +109,7 @@ public class RedisUtil {
|
|
|
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
|
|
|
result = true;
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
@@ -139,7 +141,7 @@ public class RedisUtil {
|
|
|
* 判断缓存中是否有对应的value
|
|
|
*
|
|
|
* @param key
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public boolean exists(final String key) {
|
|
|
return redisTemplate.hasKey(key);
|
|
|
@@ -149,7 +151,7 @@ public class RedisUtil {
|
|
|
* 读取缓存
|
|
|
*
|
|
|
* @param key
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public Object get(final String key) {
|
|
|
Object result = null;
|
|
|
@@ -161,9 +163,6 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 哈希 添加
|
|
|
*
|
|
|
- * @param key
|
|
|
- * @param hashKey
|
|
|
- * @param value
|
|
|
*/
|
|
|
public void hmSet(String key, Object hashKey, Object value) {
|
|
|
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
|
|
|
@@ -173,9 +172,8 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 哈希获取数据
|
|
|
*
|
|
|
- * @param key
|
|
|
* @param hashKey
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public Object hmGet(String key, Object hashKey) {
|
|
|
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
|
|
|
@@ -199,7 +197,7 @@ public class RedisUtil {
|
|
|
* @param k
|
|
|
* @param l
|
|
|
* @param l1
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public List<Object> lRange(String k, long l, long l1) {
|
|
|
ListOperations<String, Object> list = redisTemplate.opsForList();
|
|
|
@@ -221,7 +219,7 @@ public class RedisUtil {
|
|
|
* 集合获取
|
|
|
*
|
|
|
* @param key
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public Set<Object> setMembers(String key) {
|
|
|
SetOperations<String, Object> set = redisTemplate.opsForSet();
|
|
|
@@ -231,9 +229,6 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 有序集合添加
|
|
|
*
|
|
|
- * @param key
|
|
|
- * @param value
|
|
|
- * @param scoure
|
|
|
*/
|
|
|
public void zAdd(String key, Object value, double scoure) {
|
|
|
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
|
|
@@ -243,10 +238,8 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 有序集合获取
|
|
|
*
|
|
|
- * @param key
|
|
|
- * @param scoure
|
|
|
* @param scoure1
|
|
|
- * @return
|
|
|
+ * @ return
|
|
|
*/
|
|
|
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
|
|
|
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
|
|
@@ -294,8 +287,6 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 有序集合添加
|
|
|
*
|
|
|
- * @param key
|
|
|
- * @param value
|
|
|
*/
|
|
|
public Double zSetScore(String key, Object value) {
|
|
|
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
|
|
@@ -306,9 +297,6 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 有序集合添加分数
|
|
|
*
|
|
|
- * @param key
|
|
|
- * @param value
|
|
|
- * @param scoure
|
|
|
*/
|
|
|
public void incrementScore(String key, Object value, double scoure) {
|
|
|
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
|
|
@@ -319,7 +307,6 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 有序集合获取排名
|
|
|
*
|
|
|
- * @param key
|
|
|
*/
|
|
|
public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithScore(String key, long start, long end) {
|
|
|
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
|
|
@@ -330,7 +317,6 @@ public class RedisUtil {
|
|
|
/**
|
|
|
* 有序集合获取排名
|
|
|
*
|
|
|
- * @param key
|
|
|
*/
|
|
|
public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithRank(String key, long start, long end) {
|
|
|
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|