|
@@ -0,0 +1,233 @@
|
|
|
+package com.abi.qms.platform.infrastructure.util;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+import redis.clients.jedis.Jedis;
|
|
|
+import redis.clients.jedis.JedisPool;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class RedisUtils {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JedisPool jedisPool;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加锁
|
|
|
+ */
|
|
|
+ public boolean getLock(String lockKey, String lockId, long millisecond) {
|
|
|
+ Boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey, lockId,
|
|
|
+ millisecond, TimeUnit.MILLISECONDS);
|
|
|
+ return success != null && success;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*public boolean getLock(String lockKey){
|
|
|
+ return redisTemplate.opsForValue().setIfAbsent();
|
|
|
+ }*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 释放锁
|
|
|
+ */
|
|
|
+ // 1: 2021/2/26 1.先判断锁是否是自己的,身份验证
|
|
|
+ public void releaseLock(String lockKey, String lockId) {
|
|
|
+
|
|
|
+ if (redisTemplate.opsForValue().get(lockKey).equals(lockId))
|
|
|
+ redisTemplate.delete(lockKey);
|
|
|
+ else
|
|
|
+ throw new RuntimeException(lockId + " 释放锁失败,锁不存在或已失效!...");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public RedisUtils() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean expire(String key, long time) {
|
|
|
+ try {
|
|
|
+ if (time > 0L) {
|
|
|
+ this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (Exception var5) {
|
|
|
+ // log.error("RedisUtil expire error:", var5);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean expireAt(String key, Date date) {
|
|
|
+ try {
|
|
|
+ if (!ObjectUtils.isEmpty(date)) {
|
|
|
+ this.redisTemplate.expireAt(key, date);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (Exception var4) {
|
|
|
+ //log.error("RedisUtil expireAt error:", var4);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getExpire(final String key) {
|
|
|
+ return this.redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public long getExpire(final String key, TimeUnit unit) {
|
|
|
+ return this.redisTemplate.getExpire(key, unit);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean exists(String key) {
|
|
|
+ return key != null && !"".equals(key) ? this.redisTemplate.hasKey(key) : false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object getWithWatch(final String key) {
|
|
|
+ Object result = null;
|
|
|
+ this.redisTemplate.watch(key);
|
|
|
+ ValueOperations<String, Object> operations = this.redisTemplate.opsForValue();
|
|
|
+ result = operations.get(key);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean setIfAbsent(String key, Object value) {
|
|
|
+ return this.redisTemplate.opsForValue().setIfAbsent(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object getAndSet(String key, Object value) {
|
|
|
+ return this.redisTemplate.opsForValue().getAndSet(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setHash(String key, Map map) {
|
|
|
+ this.redisTemplate.opsForHash().putAll(key, map);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setHash(String key, Object key1, Object value1) {
|
|
|
+ this.redisTemplate.opsForHash().put(key, key1, value1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object getHash(String key, Object key1) {
|
|
|
+ return this.redisTemplate.opsForHash().get(key, key1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deleteHash(String key, Object... key1) {
|
|
|
+ this.redisTemplate.opsForHash().delete(key, key1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void pushAllList(String key, List<Object> values) {
|
|
|
+ this.redisTemplate.opsForList().leftPushAll(key, values);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void pushList(String key, Object value) {
|
|
|
+ if (this.redisTemplate.hasKey(key)) {
|
|
|
+ this.redisTemplate.opsForList().leftPush(key, value);
|
|
|
+ } else {
|
|
|
+ this.pushAllList(key, Arrays.asList(value));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void del(String... key) {
|
|
|
+ if (key != null && key.length > 0) {
|
|
|
+ if (key.length == 1) {
|
|
|
+ this.redisTemplate.delete(key[0]);
|
|
|
+ } else {
|
|
|
+ this.redisTemplate.delete(CollectionUtils.arrayToList(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void remove(final String key) {
|
|
|
+ if (this.exists(key)) {
|
|
|
+ this.redisTemplate.delete(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void remove(final String... keys) {
|
|
|
+ String[] var2 = keys;
|
|
|
+ int var3 = keys.length;
|
|
|
+
|
|
|
+ for (int var4 = 0; var4 < var3; ++var4) {
|
|
|
+ String key = var2[var4];
|
|
|
+ this.remove(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removePattern(final String pattern) {
|
|
|
+ Set<String> keys = this.redisTemplate.keys(pattern);
|
|
|
+ if (keys.size() > 0) {
|
|
|
+ this.redisTemplate.delete(keys);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object get(final String key) {
|
|
|
+ return key == null ? null : this.redisTemplate.opsForValue().get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean set(final String key, Object value) {
|
|
|
+ try {
|
|
|
+ this.redisTemplate.opsForValue().set(key, value);
|
|
|
+ return true;
|
|
|
+ } catch (Exception var4) {
|
|
|
+ //log.error("RedisUtil set error:", var4);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean set(final String key, Object value, long time) {
|
|
|
+ try {
|
|
|
+ if (time > 0L) {
|
|
|
+ this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.MILLISECONDS);
|
|
|
+ } else {
|
|
|
+ this.set(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (Exception var6) {
|
|
|
+ //log.error("RedisUtil set2 error:", var6);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean set(final String key, Object value, long time, TimeUnit unit) {
|
|
|
+ try {
|
|
|
+ if (time > 0L) {
|
|
|
+ this.redisTemplate.opsForValue().set(key, value, time, unit);
|
|
|
+ } else {
|
|
|
+ this.set(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (Exception var7) {
|
|
|
+ // log.error("RedisUtil set2 error:", var7);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean setNx(final String key, Object value, long time) {
|
|
|
+ try {
|
|
|
+ return time > 0L ? this.redisTemplate.opsForValue().setIfAbsent(key, value, time, TimeUnit.MILLISECONDS) : this.redisTemplate.opsForValue().setIfAbsent(key, value);
|
|
|
+ } catch (Exception var6) {
|
|
|
+ //log.error("RedisUtil setNX error:", var6);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean unlock(String key, String value) {
|
|
|
+ Long RELEASE_SUCCESS = 1L;
|
|
|
+ Jedis jedis = this.jedisPool.getResource();
|
|
|
+ String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
|
|
|
+ Object result = jedis.eval(script, Collections.singletonList(key), Collections.singletonList(value));
|
|
|
+ return RELEASE_SUCCESS.equals(result) ? true : false;
|
|
|
+ }
|
|
|
+}
|