From e6df4123693fca526040115475701bcd3e373904 Mon Sep 17 00:00:00 2001 From: vincentlu <t1341870251@gmail.com> Date: 星期四, 13 二月 2025 13:36:59 +0800 Subject: [PATCH] # --- rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java | 337 +++++++++++++++++++++++++------------------------------- 1 files changed, 150 insertions(+), 187 deletions(-) diff --git a/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java b/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java index aa90caf..6886d97 100644 --- a/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java +++ b/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")); // 鍦╞orrow涓�涓猨edis瀹炰緥鏃讹紝鏄惁鎻愬墠杩涜validate鎿嶄綔锛涘鏋滀负true锛屽垯寰楀埌鐨刯edis瀹炰緥鍧囨槸鍙敤鐨勶紱 config.setTestOnBorrow(false); // 褰撳墠jedis杩炴帴鐨勫簱鍙� - index = ConfigHelper.getInteger(name+".index"); + index = ConfigHelper.getInteger(name + ".index"); // 瀹炰緥鍖杍edis姹� - 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; + } + + // 涓哄凡瀛樺湪鐨刱ey璁剧疆杩囨湡鏃堕棿 - 绉� + 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); + } + } + + // 涓哄凡瀛樺湪鐨刱ey璁剧疆杩囨湡鏃堕棿 - 鍏蜂綋鍒版椂闂存埑 锛堢锛� + 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; } } -- Gitblit v1.9.1