cl
6 天以前 2a34b52125d5fc356d65ee1e8912845dd601d4e3
rsf-server/src/main/java/com/vincent/rsf/server/common/service/RedisService.java
@@ -3,6 +3,7 @@
import com.vincent.rsf.common.utils.Serialize;
import com.vincent.rsf.server.common.config.RedisProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
@@ -36,16 +37,19 @@
         JedisPoolConfig config = new JedisPoolConfig();
         config.setTestOnBorrow(false);
         this.index = redisProperties.getIndex();
         // 空白密码传 null,不向未开认证的 Redis 发 AUTH
         String pwd = StringUtils.trimToNull(redisProperties.getPassword());
         this.pool = new JedisPool(config
               , redisProperties.getHost()
               , redisProperties.getPort()
               , redisProperties.getTimeout()
               , redisProperties.getPassword()
               , pwd
         );
      }
      return this.pool;
   }
   /** 借连接失败时返回 null */
   public Jedis getJedis(){
      try{
         Jedis jedis = this.getPool().getResource();
@@ -71,6 +75,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.set((flag + LINK + key).getBytes(), Serialize.serialize(value));
      } catch (Exception e) {
@@ -88,6 +95,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.setex((flag + LINK + key).getBytes(), seconds, Serialize.serialize(value));
      } catch (Exception e) {
@@ -101,6 +111,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         byte[] bytes = jedis.get((flag + LINK + key).getBytes());
         if(bytes == null || bytes.length == 0 ) {
@@ -118,6 +131,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.del((flag + LINK + key).getBytes());
      } catch (Exception e) {
@@ -131,6 +147,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      this.setValue(flag, "CLEARING", "true");
      try{
         Object returnValue = jedis.eval("local keys = redis.call('keys', ARGV[1]) for i=1,#keys,1000 do redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) end return #keys",0,flag + LINK + "*");
@@ -147,6 +166,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expire((flag + LINK + key).getBytes(), seconds);
      } catch (Exception e) {
@@ -160,6 +182,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expireAt((flag + LINK + key).getBytes(), toTime.getTime()/1000);
      } catch (Exception e) {
@@ -173,6 +198,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.ttl((flag + LINK + key).getBytes());
      } catch (Exception e) {
@@ -189,6 +217,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.set(flag + LINK + key, value);
      } catch (Exception e) {
@@ -202,6 +233,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.setex(flag + LINK + key, seconds , value);
      } catch (Exception e) {
@@ -215,6 +249,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.get(flag + LINK + key);
      } catch (Exception e) {
@@ -229,6 +266,9 @@
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         String[] keys = new String[key.length];
         for(int i=0;i<key.length;i++){
@@ -248,6 +288,9 @@
      this.setValue(flag, "CLEARING", "true");
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         Object returnValue = jedis.eval("return redis.call('del', unpack(redis.call('keys', ARGV[1])))",0,flag + LINK + "*");
@@ -263,6 +306,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expire((flag + LINK + key).getBytes(), seconds);
      } catch (Exception e) {
@@ -275,6 +321,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expireAt((flag + LINK + key).getBytes(), atTime.getTime()/1000);
      } catch (Exception e) {
@@ -294,6 +343,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try {
         return jedis.hset(name.getBytes(), key.getBytes(), Serialize.serialize(value));
      } catch (Exception e) {
@@ -307,6 +359,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         byte[] bytes = jedis.hget(name.getBytes(), key.getBytes());
         if (bytes == null || bytes.length == 0) {
@@ -324,6 +379,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
            return jedis.hkeys(name);
      } catch (Exception e) {
@@ -337,6 +395,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         String[] keys = new String[key.length];
            System.arraycopy(key, 0, keys, 0, key.length);
@@ -352,6 +413,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.del(name);
      } catch (Exception e) {
@@ -365,6 +429,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expire(name.getBytes(), seconds);
      } catch (Exception e) {
@@ -377,6 +444,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expireAt(name.getBytes(), atTime.getTime()/1000);
      } catch (Exception e) {
@@ -396,6 +466,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.rpush(name.getBytes(), Serialize.serialize(value));
      } catch (Exception e) {
@@ -410,6 +483,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         byte[] bytes = jedis.lpop(name.getBytes());
         if(bytes == null || bytes.length == 0) {
@@ -428,6 +504,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
         return jedis.del(name);
      } catch (Exception e) {
@@ -441,6 +520,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expire(name.getBytes(), seconds);
      } catch (Exception e) {
@@ -453,6 +535,9 @@
         return;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return;
      }
      try{
         jedis.expireAt(name.getBytes(), atTime.getTime()/1000);
      } catch (Exception e) {
@@ -467,6 +552,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
            return jedis.incr("COUNT." + key);
      } catch (Exception e) {
@@ -480,6 +568,9 @@
         return null;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return null;
      }
      try{
            return jedis.decr("COUNT." + key);
      } catch (Exception e) {
@@ -488,4 +579,50 @@
      return null;
   }
   /** SET NX EX;键已存在返回 false;未初始化或借连接失败返回 true,由调用方降级 */
   public boolean trySetStringNxEx(String flag, String key, String value, int expireSeconds) {
      if (!this.initialize) {
         return true;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return true;
      }
      try {
         String fullKey = flag + LINK + key;
         String r = jedis.set(fullKey, value, "NX", "EX", expireSeconds);
         return "OK".equals(r);
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
         return true;
      }
   }
   /** SETNX + EXPIRE;未抢到锁返回 false;jedis 异常时降级为 true */
   public boolean tryLockValue(String flag, String key, int expireSeconds) {
      if(!this.initialize) {
         return true;
      }
      Jedis jedis = this.getJedis();
      if (jedis == null) {
         return true;
      }
      try {
         String lockKey = flag + LINK + key;
         Long r = jedis.setnx(lockKey, "1");
         if (r != null && r == 1L) {
            jedis.expire(lockKey, expireSeconds);
            return true;
         }
         return false;
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
         return true;
      }
   }
   public void unlockValue(String flag, String key) {
      deleteValue(flag, key);
   }
}