#
vincentlu
2025-02-13 e6df4123693fca526040115475701bcd3e373904
rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java
@@ -45,7 +45,7 @@
   private RedisSupport(String name) {
      try {
         boolean bAll = true;
         String[] configKeys={"host","port","max","min","timeout","index"};
         String[] configKeys = {"host", "port", "max", "min", "timeout", "index"};
         for(String key : configKeys){
            if(!ConfigHelper.contains(name + "." + key)){
               bAll = false;
@@ -53,19 +53,19 @@
         }
         if (bAll) {
            config = new JedisPoolConfig();
            config.setMaxIdle(ConfigHelper.getInteger(name+".min"));
              config.setMaxWaitMillis(ConfigHelper.getLong(name+".timeout"));
            config.setMaxIdle(ConfigHelper.getInteger(name + ".min"));
              config.setMaxWaitMillis(ConfigHelper.getLong(name + ".timeout"));
              // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
              config.setTestOnBorrow(false);
              // 当前jedis连接的库号
            index = ConfigHelper.getInteger(name+".index");
            index = ConfigHelper.getInteger(name + ".index");
            // 实例化jedis池
              String host = ConfigHelper.getString(name+".host");
              int port = ConfigHelper.getInteger(name+".port");
              int timeout = ConfigHelper.getInteger(name+".timeout");
              String password = ConfigHelper.getString(name+".password");
              String host = ConfigHelper.getString(name + ".host");
              int port = ConfigHelper.getInteger(name + ".port");
              int timeout = ConfigHelper.getInteger(name + ".timeout");
              String password = ConfigHelper.getString(name + ".password");
            pool = new JedisPool(config,host , port,timeout,password);
            pool = new JedisPool(config, host , port, timeout, password);
            Jedis jedis = this.getJedis();
            this.pool.returnResource(jedis);
            initialize = true;
@@ -92,7 +92,129 @@
      return null;
   }
   // key - value ----------------------------------------------------------------------------------------------------------
   // key - object ----------------------------------------------------------------------------------------------------------
   public String set(String flag, String key, Object value) {
      if(!this.initialize) {
         return null;
      }
      if(null == value) {
         this.delete(flag, key);
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         return jedis.set((flag + LINK + key).getBytes(), Serialize.serialize(value));
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   public String set(String flag, String key, Object value, Integer seconds) {
      if(!this.initialize) {
         return null;
      }
      if (null == value) {
         this.delete(flag, key);
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         return jedis.setex((flag + LINK + key).getBytes(), seconds, Serialize.serialize(value));
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   public <T> T get(String flag, String key) {
      if(!this.initialize) {
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         byte[] bytes = jedis.get((flag + LINK + key).getBytes());
         if(bytes == null || bytes.length == 0 ) {
            return null;
         }
         return (T) Serialize.unSerialize(bytes);
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   public Long delete(String flag, String key) {
      if(!this.initialize) {
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         return jedis.del((flag + LINK + key).getBytes());
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   public Long clear(String flag) {
      if(!this.initialize) {
         return null;
      }
      Jedis jedis = this.getJedis();
      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 + "*");
         return Long.parseLong(String.valueOf(returnValue));
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   // 为已存在的key设置过期时间 - 秒
   public void setExpire(String flag, String key, int seconds){
      if(!this.initialize) {
         return;
      }
      Jedis jedis = this.getJedis();
      try{
         jedis.expire((flag + LINK + key).getBytes(), seconds);
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
   }
   // 为已存在的key设置过期时间 - 具体到时间戳 (秒)
   public void setExpireAt(String flag, String key, Date toTime){
      if(!this.initialize) {
         return;
      }
      Jedis jedis = this.getJedis();
      try{
         jedis.expireAt((flag + LINK + key).getBytes(), toTime.getTime()/1000);
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
   }
   // 获取过期剩余时间(秒) ttl == -1 没有设置过期时间; ttl == -2 key不存在
   public Long getExpire(String flag, String key) {
      if(!this.initialize) {
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         return jedis.ttl((flag + LINK + key).getBytes());
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   // key - string ----------------------------------------------------------------------------------------------------------
   public String setValue(String flag, String key, String value) {
      if(!this.initialize) {
@@ -107,7 +229,7 @@
      return null;
   }
   public String setValue(String flag, String key, String value,Integer seconds) {
   public String setValue(String flag, String key, String value, Integer seconds) {
      if(!this.initialize) {
         return null;
      }
@@ -180,7 +302,7 @@
      }
   }
   public void setValueExpireTime(String flag, String key,Date atTime){
   public void setValueExpireAt(String flag, String key,Date atTime){
      if(!this.initialize) {
         return;
      }
@@ -282,7 +404,7 @@
      }
   }
   public void setMapExpireTime(String name,Date atTime){
   public void setMapExpireAt(String name,Date atTime){
      if(!this.initialize) {
         return;
      }
@@ -358,7 +480,7 @@
      }
   }
   public void setListExpireTime(String name, Date atTime){
   public void setListExpireAt(String name, Date atTime){
      if(!this.initialize) {
         return;
      }
@@ -370,195 +492,36 @@
      }
   }
   public Object setObject(String flag,String key, Object value) {
      if(!this.initialize)return null;
      if(value==null){
         this.deleteObject(flag, key);
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         String result = jedis.set(("OBJ."+flag+"."+key).getBytes(), Serialize.serialize(value));
         return value;
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
      return value;
   }
   public Object setObject(String flag,String key, Object value,Integer seconds) {
      if(!this.initialize)return null;
      if(value==null){
         this.deleteObject(flag, key);
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         String result = jedis.setex(("OBJ."+flag+"."+key).getBytes(),seconds,Serialize.serialize(value));
         return value;
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
      return value;
   }
   public <T> T getObject(String flag,String key) {
      if(!this.initialize)return null;
      Jedis jedis = this.getJedis();
      try{
         byte[] bytes = jedis.get(("OBJ."+flag+"."+key).getBytes());
         if(bytes==null||bytes.length==0)
            return null;
         T returnValue = (T) Serialize.unSerialize(bytes);
         return (T) returnValue;
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
      return null;
   }
   public <T> Long deleteObject(String flag, String key) {
      if(!this.initialize)return null;
      Jedis jedis = this.getJedis();
      try{
         Long returnValue = jedis.del(("OBJ."+flag+"."+key).getBytes());
         return returnValue;
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
      return null;
   }
   public <T> Long clearObject(String flag) {
      if(!this.initialize)return null;
      this.setObject(flag, "CLEARING", "true");
      Jedis jedis = this.getJedis();
      try{
         //EVAL "return redis.call('del', unpack(redis.call('keys', 'OBJ.*')))" 0
         //EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 OBJ.*
         //Object returnValue = jedis.eval("return redis.call('del', unpack(redis.call('keys', ARGV[1])))",0,"OBJ."+flag+"."+"*");
         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,"OBJ."+flag+"."+"*");
         return Long.parseLong(String.valueOf(returnValue));
      }catch(Exception ex){
         ////Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
      return null;
   }
   public void setObjectExpire(String flag, String key,int seconds){
      if(!this.initialize)return;
      Jedis jedis = this.getJedis();
      try{
         jedis.expire(("OBJ."+flag+"."+key).getBytes(), seconds);
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
   }
   public void setObjectExpireTime(String flag, String key,Date toTime){
      if(!this.initialize)return;
      Jedis jedis = this.getJedis();
      try{
         jedis.expireAt(("OBJ."+flag+"."+key).getBytes(), toTime.getTime()/1000);
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
   }
   public String[] getKeys(String pattern) {
      if(!this.initialize)return null;
      Jedis jedis = this.getJedis();
      try{
         return jedis.keys(pattern).toArray(new String[]{});
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
      }
      return null;
   }
   // count ----------------------------------------------------------------------------------------------------------
   public Long incr(String key) {
      if(!this.initialize)return null;
      if(!this.initialize) {
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         Long returnValue = jedis.incr("DI."+key);
         return returnValue;
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
            return jedis.incr("COUNT." + key);
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   public Long decr(String key) {
      if(!this.initialize)return null;
      if(!this.initialize) {
         return null;
      }
      Jedis jedis = this.getJedis();
      try{
         Long returnValue = jedis.decr("DI."+key);
         return returnValue;
      }catch(Exception ex){
         //Utils.trace(ex.getMessage());
      }finally{
         if(jedis!=null)
            this.pool.returnResource(jedis);
            return jedis.decr("COUNT." + key);
      } catch (Exception e) {
         log.error(this.getClass().getSimpleName(), e);
      }
      return null;
   }
   public static void main(String...strings){
      Date start = new Date();
      RedisSupport r = RedisSupport.getRedis("lazy.redis.0");
      r.clearObject("aaa");
      RedisSupport redis = RedisSupport.defaultRedisSupport;
   }
}