|
@@ -23,127 +23,146 @@ import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
|
import org.springframework.data.redis.core.script.RedisScript;
|
|
|
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
@Component
|
|
|
public class RedisUtils {
|
|
|
|
|
|
- @Autowired(required = false)
|
|
|
- @Qualifier("initRedisTemplate")
|
|
|
- private RedisTemplate<String, Object> redisTemplate;
|
|
|
-
|
|
|
- @Value("${spring.redis.isEnable:false}")
|
|
|
- private boolean isRedisEnable;
|
|
|
-
|
|
|
- private final String script = "if redis.call('setnx', KEYS[1], ARGV[1]) == 1 then return redis.call('expire', KEYS[1], ARGV[2]) else return 0 end";
|
|
|
-
|
|
|
- public boolean isRedisEnable() {
|
|
|
- return isRedisEnable;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean set(String key, Object value) {
|
|
|
- if (!isRedisEnable) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
- valueOperations.set(key, value);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean set(String key, Object value, Long l, TimeUnit timeUnit) {
|
|
|
- if (!isRedisEnable) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
- valueOperations.set(key, value, l, timeUnit);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- public Object get(String key) {
|
|
|
- if (!isRedisEnable) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
- return valueOperations.get(key);
|
|
|
- }
|
|
|
-
|
|
|
- public boolean delete(String key) {
|
|
|
- return isRedisEnable && redisTemplate.delete(key);
|
|
|
- }
|
|
|
-
|
|
|
- public void convertAndSend(String channel, Object message) {
|
|
|
-
|
|
|
- if (!isRedisEnable) {
|
|
|
- throw new RuntimeException("Redis is disabled");
|
|
|
- }
|
|
|
-
|
|
|
- redisTemplate.convertAndSend(channel, message);
|
|
|
- }
|
|
|
-
|
|
|
- public Long incrementDelta(String key, int delta) {
|
|
|
- if (!isRedisEnable) {
|
|
|
- throw new RuntimeException("Redis is disabled");
|
|
|
- }
|
|
|
- ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
- return valueOperations.increment(key, delta);
|
|
|
- }
|
|
|
-
|
|
|
- public synchronized Long incr(String key, int timeout) {
|
|
|
- if (!isRedisEnable) {
|
|
|
- return -1L;
|
|
|
- }
|
|
|
-
|
|
|
- if (setIfAbsent(key, 1, timeout) && timeout > 0) {
|
|
|
- return 1L;
|
|
|
- } else {
|
|
|
- ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
- return valueOperations.increment(key, 1);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public synchronized Long decr(String key, int timeout) {
|
|
|
- if (!isRedisEnable) {
|
|
|
- return -1L;
|
|
|
- }
|
|
|
-
|
|
|
- if (setIfAbsent(key, -1, timeout) && timeout > 0) {
|
|
|
- return -1L;
|
|
|
- } else {
|
|
|
- ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
- return valueOperations.increment(key, -1);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public boolean setIfAbsent(String key, Object value, int timeout) {
|
|
|
-
|
|
|
- if (!isRedisEnable) {
|
|
|
- throw new RuntimeException("Redis is disabled");
|
|
|
- }
|
|
|
-
|
|
|
- List<String> keys = new ArrayList<>();
|
|
|
- keys.add(key);
|
|
|
-
|
|
|
- Object[] values = new Object[] { value, timeout };
|
|
|
-
|
|
|
- return 1L == (Long) redisTemplate.execute(RedisScript.of(script, Long.class), keys, values);
|
|
|
- }
|
|
|
-
|
|
|
- public boolean setIfAbsent(String key, Object value) {
|
|
|
-
|
|
|
- if (!isRedisEnable) {
|
|
|
- throw new RuntimeException("Redis is disabled");
|
|
|
- }
|
|
|
-
|
|
|
- ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
- return valueOperations.setIfAbsent(key, value);
|
|
|
- }
|
|
|
+ @Autowired(required = false)
|
|
|
+ @Qualifier("initRedisTemplate")
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
+ @Value("${spring.redis.isEnable:false}")
|
|
|
+ private boolean isRedisEnable;
|
|
|
+
|
|
|
+ private final String script = "if redis.call('setnx', KEYS[1], ARGV[1]) == 1 then return redis.call('expire', KEYS[1], ARGV[2]) else return 0 end";
|
|
|
+
|
|
|
+ public boolean isRedisEnable() {
|
|
|
+ return isRedisEnable;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean set(String key, Object value) {
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ valueOperations.set(key, value);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean set(String key, Object value, Long l, TimeUnit timeUnit) {
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ valueOperations.set(key, value, l, timeUnit);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object get(String key) {
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ return valueOperations.get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object getSpac(String key) {
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());
|
|
|
+ redisTemplate.afterPropertiesSet();
|
|
|
+
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ Object value = valueOperations.get(key);
|
|
|
+
|
|
|
+ redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
|
|
+ redisTemplate.afterPropertiesSet();
|
|
|
+
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean delete(String key) {
|
|
|
+ return isRedisEnable && redisTemplate.delete(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void convertAndSend(String channel, Object message) {
|
|
|
+
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ throw new RuntimeException("Redis is disabled");
|
|
|
+ }
|
|
|
+
|
|
|
+ redisTemplate.convertAndSend(channel, message);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Long incrementDelta(String key, int delta) {
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ throw new RuntimeException("Redis is disabled");
|
|
|
+ }
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ return valueOperations.increment(key, delta);
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized Long incr(String key, int timeout) {
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ return -1L;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (setIfAbsent(key, 1, timeout) && timeout > 0) {
|
|
|
+ return 1L;
|
|
|
+ } else {
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ return valueOperations.increment(key, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public synchronized Long decr(String key, int timeout) {
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ return -1L;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (setIfAbsent(key, -1, timeout) && timeout > 0) {
|
|
|
+ return -1L;
|
|
|
+ } else {
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ return valueOperations.increment(key, -1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public boolean setIfAbsent(String key, Object value, int timeout) {
|
|
|
+
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ throw new RuntimeException("Redis is disabled");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> keys = new ArrayList<>();
|
|
|
+ keys.add(key);
|
|
|
+
|
|
|
+ Object[] values = new Object[]{value, timeout};
|
|
|
+
|
|
|
+ return 1L == (Long) redisTemplate.execute(RedisScript.of(script, Long.class), keys, values);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean setIfAbsent(String key, Object value) {
|
|
|
+
|
|
|
+ if (!isRedisEnable) {
|
|
|
+ throw new RuntimeException("Redis is disabled");
|
|
|
+ }
|
|
|
+
|
|
|
+ ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
|
|
|
+ return valueOperations.setIfAbsent(key, value);
|
|
|
+ }
|
|
|
|
|
|
}
|