|
@@ -11,12 +11,11 @@ import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
-@SuppressWarnings("all")
|
|
|
|
-public class RedisService {
|
|
|
|
- private Logger log = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
+public class RedisService<T> {
|
|
|
|
+ private final Logger log = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
+ private RedisTemplate<String, T> redisTemplate;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 发布消息
|
|
* 发布消息
|
|
@@ -24,7 +23,7 @@ public class RedisService {
|
|
* @param queueName
|
|
* @param queueName
|
|
* @param message
|
|
* @param message
|
|
*/
|
|
*/
|
|
- public void sendMessage(String queueName, Object message) {
|
|
|
|
|
|
+ public void sendMessage(String queueName, T message) {
|
|
redisTemplate.opsForList().leftPush(queueName, message);
|
|
redisTemplate.opsForList().leftPush(queueName, message);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -34,7 +33,7 @@ public class RedisService {
|
|
* @param queueName
|
|
* @param queueName
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public Object receiveMessage(String queueName) {
|
|
|
|
|
|
+ public T receiveMessage(String queueName) {
|
|
return redisTemplate.opsForList().rightPop(queueName);
|
|
return redisTemplate.opsForList().rightPop(queueName);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -104,7 +103,7 @@ public class RedisService {
|
|
* @param key 键
|
|
* @param key 键
|
|
* @return 值
|
|
* @return 值
|
|
*/
|
|
*/
|
|
- public Object get(String key) {
|
|
|
|
|
|
+ public T get(String key) {
|
|
return key == null ? null : redisTemplate.opsForValue().get(key);
|
|
return key == null ? null : redisTemplate.opsForValue().get(key);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -115,9 +114,9 @@ public class RedisService {
|
|
* @param value 值
|
|
* @param value 值
|
|
* @return true成功 false失败
|
|
* @return true成功 false失败
|
|
*/
|
|
*/
|
|
- public Boolean set(String key, Object value) {
|
|
|
|
|
|
+ public Boolean set(String key, T value) {
|
|
try {
|
|
try {
|
|
- redisTemplate.opsForValue().set(key, value);
|
|
|
|
|
|
+ redisTemplate.opsForValue().set(key, value, null);
|
|
return true;
|
|
return true;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
@@ -133,7 +132,7 @@ public class RedisService {
|
|
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
|
|
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
|
|
* @return true成功 false 失败
|
|
* @return true成功 false 失败
|
|
*/
|
|
*/
|
|
- public Boolean set(String key, Object value, Long time) {
|
|
|
|
|
|
+ public Boolean set(String key, T value, Long time) {
|
|
try {
|
|
try {
|
|
if (time > 0) {
|
|
if (time > 0) {
|
|
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
|
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
|
@@ -212,7 +211,7 @@ public class RedisService {
|
|
* @param map 对应多个键值
|
|
* @param map 对应多个键值
|
|
* @return true 成功 false 失败
|
|
* @return true 成功 false 失败
|
|
*/
|
|
*/
|
|
- public Boolean hmset(String key, Map<String, Object> map) {
|
|
|
|
|
|
+ public Boolean hmset(String key, Map<String, T> map) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForHash().putAll(key, map);
|
|
redisTemplate.opsForHash().putAll(key, map);
|
|
return true;
|
|
return true;
|
|
@@ -230,7 +229,7 @@ public class RedisService {
|
|
* @param time 时间(秒)
|
|
* @param time 时间(秒)
|
|
* @return true成功 false失败
|
|
* @return true成功 false失败
|
|
*/
|
|
*/
|
|
- public Boolean hmset(String key, Map<String, Object> map, Long time) {
|
|
|
|
|
|
+ public Boolean hmset(String key, Map<String, T> map, Long time) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForHash().putAll(key, map);
|
|
redisTemplate.opsForHash().putAll(key, map);
|
|
if (time > 0) {
|
|
if (time > 0) {
|
|
@@ -252,7 +251,7 @@ public class RedisService {
|
|
* @param value 值
|
|
* @param value 值
|
|
* @return true 成功 false失败
|
|
* @return true 成功 false失败
|
|
*/
|
|
*/
|
|
- public Boolean hset(String key, String item, Object value) {
|
|
|
|
|
|
+ public Boolean hset(String key, String item, T value) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForHash().put(key, item, value);
|
|
redisTemplate.opsForHash().put(key, item, value);
|
|
return true;
|
|
return true;
|
|
@@ -271,7 +270,7 @@ public class RedisService {
|
|
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
|
|
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
|
|
* @return true 成功 false失败
|
|
* @return true 成功 false失败
|
|
*/
|
|
*/
|
|
- public Boolean hset(String key, String item, Object value, Long time) {
|
|
|
|
|
|
+ public Boolean hset(String key, String item, T value, Long time) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForHash().put(key, item, value);
|
|
redisTemplate.opsForHash().put(key, item, value);
|
|
if (time > 0) {
|
|
if (time > 0) {
|
|
@@ -290,7 +289,7 @@ public class RedisService {
|
|
* @param key 键 不能为 null
|
|
* @param key 键 不能为 null
|
|
* @param item 项 可以使多个不能为 null
|
|
* @param item 项 可以使多个不能为 null
|
|
*/
|
|
*/
|
|
- public void hdel(String key, Object... item) {
|
|
|
|
|
|
+ public void hdel(String key, T... item) {
|
|
redisTemplate.opsForHash().delete(key, item);
|
|
redisTemplate.opsForHash().delete(key, item);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -336,7 +335,7 @@ public class RedisService {
|
|
* @param key 键
|
|
* @param key 键
|
|
* @return Set
|
|
* @return Set
|
|
*/
|
|
*/
|
|
- public Set<Object> sGet(String key) {
|
|
|
|
|
|
+ public Set<T> sGet(String key) {
|
|
try {
|
|
try {
|
|
return redisTemplate.opsForSet().members(key);
|
|
return redisTemplate.opsForSet().members(key);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -352,7 +351,7 @@ public class RedisService {
|
|
* @param value 值
|
|
* @param value 值
|
|
* @return true 存在 false不存在
|
|
* @return true 存在 false不存在
|
|
*/
|
|
*/
|
|
- public Boolean sHasKey(String key, Object value) {
|
|
|
|
|
|
+ public Boolean sHasKey(String key, T value) {
|
|
try {
|
|
try {
|
|
return redisTemplate.opsForSet().isMember(key, value);
|
|
return redisTemplate.opsForSet().isMember(key, value);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -368,7 +367,7 @@ public class RedisService {
|
|
* @param values 值 可以是多个
|
|
* @param values 值 可以是多个
|
|
* @return 成功个数
|
|
* @return 成功个数
|
|
*/
|
|
*/
|
|
- public Long sSet(String key, Object... values) {
|
|
|
|
|
|
+ public Long sSet(String key, T... values) {
|
|
try {
|
|
try {
|
|
return redisTemplate.opsForSet().add(key, values);
|
|
return redisTemplate.opsForSet().add(key, values);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -386,7 +385,7 @@ public class RedisService {
|
|
* @param values 值 可以是多个
|
|
* @param values 值 可以是多个
|
|
* @return 成功个数
|
|
* @return 成功个数
|
|
*/
|
|
*/
|
|
- public Long sSetAndTime(String key, Long time, Object... values) {
|
|
|
|
|
|
+ public Long sSetAndTime(String key, Long time, T... values) {
|
|
try {
|
|
try {
|
|
Long count = redisTemplate.opsForSet().add(key, values);
|
|
Long count = redisTemplate.opsForSet().add(key, values);
|
|
if (time > 0) {
|
|
if (time > 0) {
|
|
@@ -421,7 +420,7 @@ public class RedisService {
|
|
* @param values 值 可以是多个
|
|
* @param values 值 可以是多个
|
|
* @return 移除的个数
|
|
* @return 移除的个数
|
|
*/
|
|
*/
|
|
- public Long setRemove(String key, Object... values) {
|
|
|
|
|
|
+ public Long setRemove(String key, T... values) {
|
|
try {
|
|
try {
|
|
return redisTemplate.opsForSet().remove(key, values);
|
|
return redisTemplate.opsForSet().remove(key, values);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -438,7 +437,7 @@ public class RedisService {
|
|
* @param end 结束 0 到 -1代表所有值
|
|
* @param end 结束 0 到 -1代表所有值
|
|
* @return List
|
|
* @return List
|
|
*/
|
|
*/
|
|
- public List<Object> lGet(String key, Long start, Long end) {
|
|
|
|
|
|
+ public List<T> lGet(String key, Long start, Long end) {
|
|
try {
|
|
try {
|
|
return redisTemplate.opsForList().range(key, start, end);
|
|
return redisTemplate.opsForList().range(key, start, end);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -470,7 +469,7 @@ public class RedisService {
|
|
* index<0时,-1,表尾,-2倒数第二个元素,依次类推
|
|
* index<0时,-1,表尾,-2倒数第二个元素,依次类推
|
|
* @return Object
|
|
* @return Object
|
|
*/
|
|
*/
|
|
- public Object lGetIndex(String key, Long index) {
|
|
|
|
|
|
+ public T lGetIndex(String key, Long index) {
|
|
try {
|
|
try {
|
|
return redisTemplate.opsForList().index(key, index);
|
|
return redisTemplate.opsForList().index(key, index);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -486,7 +485,7 @@ public class RedisService {
|
|
* @param value 值
|
|
* @param value 值
|
|
* @return Boolean
|
|
* @return Boolean
|
|
*/
|
|
*/
|
|
- public Boolean lSet(String key, Object value) {
|
|
|
|
|
|
+ public Boolean lSet(String key, T value) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForList().rightPush(key, value);
|
|
redisTemplate.opsForList().rightPush(key, value);
|
|
return true;
|
|
return true;
|
|
@@ -504,7 +503,7 @@ public class RedisService {
|
|
* @param time 时间(秒)
|
|
* @param time 时间(秒)
|
|
* @return Boolean
|
|
* @return Boolean
|
|
*/
|
|
*/
|
|
- public Boolean lSet(String key, Object value, Long time) {
|
|
|
|
|
|
+ public Boolean lSet(String key, T value, Long time) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForList().rightPush(key, value);
|
|
redisTemplate.opsForList().rightPush(key, value);
|
|
if (time > 0) {
|
|
if (time > 0) {
|
|
@@ -524,7 +523,7 @@ public class RedisService {
|
|
* @param value 值
|
|
* @param value 值
|
|
* @return Boolean
|
|
* @return Boolean
|
|
*/
|
|
*/
|
|
- public Boolean lSet(String key, List<Object> value) {
|
|
|
|
|
|
+ public Boolean lSet(String key, List<T> value) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForList().rightPushAll(key, value);
|
|
redisTemplate.opsForList().rightPushAll(key, value);
|
|
return true;
|
|
return true;
|
|
@@ -542,7 +541,7 @@ public class RedisService {
|
|
* @param time 时间(秒)
|
|
* @param time 时间(秒)
|
|
* @return Boolean
|
|
* @return Boolean
|
|
*/
|
|
*/
|
|
- public Boolean lSet(String key, List<Object> value, Long time) {
|
|
|
|
|
|
+ public Boolean lSet(String key, List<T> value, Long time) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForList().rightPushAll(key, value);
|
|
redisTemplate.opsForList().rightPushAll(key, value);
|
|
if (time > 0) {
|
|
if (time > 0) {
|
|
@@ -563,7 +562,7 @@ public class RedisService {
|
|
* @param value 值
|
|
* @param value 值
|
|
* @return Boolean
|
|
* @return Boolean
|
|
*/
|
|
*/
|
|
- public Boolean lUpdateIndex(String key, Long index, Object value) {
|
|
|
|
|
|
+ public Boolean lUpdateIndex(String key, Long index, T value) {
|
|
try {
|
|
try {
|
|
redisTemplate.opsForList().set(key, index, value);
|
|
redisTemplate.opsForList().set(key, index, value);
|
|
return true;
|
|
return true;
|
|
@@ -581,7 +580,7 @@ public class RedisService {
|
|
* @param value 值
|
|
* @param value 值
|
|
* @return 移除的个数
|
|
* @return 移除的个数
|
|
*/
|
|
*/
|
|
- public Long lRemove(String key, Long count, Object value) {
|
|
|
|
|
|
+ public Long lRemove(String key, Long count, T value) {
|
|
try {
|
|
try {
|
|
return redisTemplate.opsForList().remove(key, count, value);
|
|
return redisTemplate.opsForList().remove(key, count, value);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|