From 41634e2054095a666ace14543c4d8e0ea42b9bc0 Mon Sep 17 00:00:00 2001 From: xsx Date: Wed, 12 Jun 2019 21:06:44 +0800 Subject: [PATCH 01/13] update dependency version --- README.en.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.en.md b/README.en.md index 4c7b5f6..60a48ca 100644 --- a/README.en.md +++ b/README.en.md @@ -59,7 +59,7 @@ d org.redisson redisson-spring-data-21 - 3.10.7 + 3.11.0 ``` ##### redisson config: diff --git a/README.md b/README.md index f5a3385..a3ac4ee 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ https://apidoc.gitee.com/xsxgit/spring-boot-starter-fast-redis org.redisson redisson-spring-data-21 - 3.10.7 + 3.11.0 直接调用API: -- Gitee From abaaef7cce68b3366784f372153aa828a2d3db91 Mon Sep 17 00:00:00 2001 From: "xsx344646090@vip.qq.com" Date: Thu, 13 Jun 2019 17:30:06 +0800 Subject: [PATCH 02/13] optimizing code --- .../wiki/xsx/core/handler/HashHandler.java | 12 + .../wiki/xsx/core/handler/NumberHandler.java | 255 +++++++++++++++++- .../wiki/xsx/core/handler/SetHandler.java | 2 +- .../wiki/xsx/core/handler/ZsetHandler.java | 2 +- 4 files changed, 267 insertions(+), 4 deletions(-) diff --git a/src/main/java/wiki/xsx/core/handler/HashHandler.java b/src/main/java/wiki/xsx/core/handler/HashHandler.java index 53adbc5..d1d0320 100644 --- a/src/main/java/wiki/xsx/core/handler/HashHandler.java +++ b/src/main/java/wiki/xsx/core/handler/HashHandler.java @@ -331,6 +331,7 @@ public final class HashHandler implements RedisHandler { /** * 自增 + * 已过期,请使用 {@link NumberHandler#addDouble(String, String, double)} 替换 * @see Redis Documentation: HINCRBYFLOAT * @since redis 2.6.0 * @param key 键 @@ -338,12 +339,14 @@ public final class HashHandler implements RedisHandler { * @param data 步长 * @return 返回自增后的值 */ + @Deprecated public Double increment(String key, String hashKey, Double data) { return this.stringHashOperations.increment(key, hashKey, data); } /** * 自增 + * 已过期,请使用 {@link NumberHandler#addLong(String, String, long)} 替换 * @see Redis Documentation: HINCRBY * @since redis 2.0.0 * @param key 键 @@ -351,24 +354,28 @@ public final class HashHandler implements RedisHandler { * @param data 步长 * @return 返回自增后的值 */ + @Deprecated public Long increment(String key, String hashKey, Long data) { return this.stringHashOperations.increment(key, hashKey, data); } /** * 自增 + * 已过期,请使用 {@link NumberHandler#incrementLong(String, String)} 替换 * @see Redis Documentation: HINCRBY * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 * @return 返回自增后的值 */ + @Deprecated public Long increment(String key, String hashKey) { return this.stringHashOperations.increment(key, hashKey, 1L); } /** * 递减 + * 已过期,请使用 {@link NumberHandler#subtractDouble(String, String, double)} 替换 * @see Redis Documentation: HINCRBYFLOAT * @since redis 2.6.0 * @param key 键 @@ -376,12 +383,14 @@ public final class HashHandler implements RedisHandler { * @param data 步长 * @return 返回递减后的值 */ + @Deprecated public Double decrement(String key, String hashKey, Double data) { return this.stringHashOperations.increment(key, hashKey, -data); } /** * 递减 + * 已过期,请使用 {@link NumberHandler#subtractLong(String, String, long)} 替换 * @see Redis Documentation: HINCRBY * @since redis 2.0.0 * @param key 键 @@ -389,18 +398,21 @@ public final class HashHandler implements RedisHandler { * @param data 步长 * @return 返回递减后的值 */ + @Deprecated public Long decrement(String key, String hashKey, Long data) { return this.stringHashOperations.increment(key, hashKey, -data); } /** * 递减 + * 已过期,请使用 {@link NumberHandler#decrementLong(String, String)} 替换 * @see Redis Documentation: HINCRBY * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 * @return 返回递减后的值 */ + @Deprecated public Long decrement(String key, String hashKey) { return this.stringHashOperations.increment(key, hashKey, -1L); } diff --git a/src/main/java/wiki/xsx/core/handler/NumberHandler.java b/src/main/java/wiki/xsx/core/handler/NumberHandler.java index bf57060..772f031 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -1,5 +1,6 @@ package wiki.xsx.core.handler; +import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.support.atomic.RedisAtomicDouble; @@ -24,6 +25,10 @@ public final class NumberHandler implements RedisHandler { * 字符串模板 */ private ValueOperations stringOperations; + /** + * 字符串哈希模板 + */ + private HashOperations stringHashOperations; /** * 数据库索引 */ @@ -78,6 +83,19 @@ public final class NumberHandler implements RedisHandler { return this.stringOperations.increment(key, data); } + /** + * 增加浮点数 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回增加后的值 + */ + public Double addDouble(String key, String hashKey, double data) { + return this.stringHashOperations.increment(key, hashKey, data); + } + /** * 获取并增加浮点数 * @see Redis Documentation: INCRBYFLOAT @@ -90,6 +108,19 @@ public final class NumberHandler implements RedisHandler { return this.addDouble(key, data) - data; } + /** + * 获取并增加浮点数 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 + */ + public Double getAndAddDouble(String key, String hashKey, double data) { + return this.addDouble(key, hashKey, data) - data; + } + /** * 增加长整数 * @see Redis Documentation: INCRBY @@ -102,6 +133,19 @@ public final class NumberHandler implements RedisHandler { return this.stringOperations.increment(key, data); } + /** + * 增加长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回增加后的值 + */ + public Long addLong(String key, String hashKey, long data) { + return this.stringHashOperations.increment(key, hashKey, data); + } + /** * 获取并增加长整数 * @see Redis Documentation: INCRBY @@ -114,6 +158,19 @@ public final class NumberHandler implements RedisHandler { return this.addLong(key, data) - data; } + /** + * 获取并增加长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 + */ + public Long getAndAddLong(String key, String hashKey, long data) { + return this.addLong(key, hashKey, data) - data; + } + /** * 长整数自增 * @see Redis Documentation: INCR @@ -125,6 +182,18 @@ public final class NumberHandler implements RedisHandler { return this.stringOperations.increment(key); } + /** + * 长整数自增 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回自增后的值 + */ + public Long incrementLong(String key, String hashKey) { + return this.addLong(key, hashKey, 1L); + } + /** * 获取并自增长整数 * @see Redis Documentation: INCR @@ -136,6 +205,18 @@ public final class NumberHandler implements RedisHandler { return this.incrementLong(key) - 1L; } + /** + * 获取并自增长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回原值 + */ + public Long getAndIncrementLong(String key, String hashKey) { + return this.incrementLong(key, hashKey) - 1L; + } + /** * 浮点数减小 * @see Redis Documentation: INCRBYFLOAT @@ -148,6 +229,19 @@ public final class NumberHandler implements RedisHandler { return this.addDouble(key, -data); } + /** + * 浮点数减小 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回相减后的值 + */ + public Double subtractDouble(String key, String hashKey, double data) { + return this.addDouble(key, hashKey, -data); + } + /** * 获取并减小浮点数 * @see Redis Documentation: INCRBYFLOAT @@ -160,6 +254,19 @@ public final class NumberHandler implements RedisHandler { return this.addDouble(key, -data) + data; } + /** + * 获取并减小浮点数 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 + */ + public Double getAndSubtractDouble(String key, String hashKey, double data) { + return this.subtractDouble(key, hashKey, data) + data; + } + /** * 长整数减小 * @see Redis Documentation: DECRBY @@ -172,18 +279,44 @@ public final class NumberHandler implements RedisHandler { return this.stringOperations.decrement(key, data); } + /** + * 长整数减小 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回减小后的值 + */ + public Long subtractLong(String key, String hashKey, long data) { + return this.stringHashOperations.increment(key, hashKey, -data); + } + /** * 获取并减小长整数 * @see Redis Documentation: DECRBY * @since redis 1.0.0 * @param key 键 * @param data 步长 - * @return 返回减小后的值 + * @return 返回原值 */ public Long getAndSubtractLong(String key, long data) { return this.subtractLong(key, data) + data; } + /** + * 获取并减小长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 + */ + public Long getAndSubtractLong(String key, String hashKey, long data) { + return this.subtractLong(key, hashKey, data) + data; + } + /** * 长整数递减 * @see Redis Documentation: DECR @@ -195,6 +328,18 @@ public final class NumberHandler implements RedisHandler { return this.stringOperations.decrement(key); } + /** + * 长整数递减 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回递减后的值 + */ + public Long decrementLong(String key, String hashKey) { + return this.subtractLong(key, hashKey, 1L); + } + /** * 获取并递减长整数 * @see Redis Documentation: DECR @@ -206,6 +351,18 @@ public final class NumberHandler implements RedisHandler { return this.decrementLong(key) + 1L; } + /** + * 获取并递减长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回原值 + */ + public Long getAndDecrementLong(String key, String hashKey) { + return this.decrementLong(key, hashKey) + 1L; + } + /** * 设置浮点数 * @see Redis Documentation: SET @@ -217,6 +374,18 @@ public final class NumberHandler implements RedisHandler { this.stringOperations.set(key, String.valueOf(value)); } + /** + * 设置浮点数 + * @see Redis Documentation: HSET + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param value 值 + */ + public void setDouble(String key, String hashKey, double value) { + this.stringHashOperations.put(key, hashKey, String.valueOf(value)); + } + /** * 设置浮点数(若存在则更新过期时间) * @see Redis Documentation: SETEX @@ -242,6 +411,19 @@ public final class NumberHandler implements RedisHandler { return this.stringOperations.setIfAbsent(key, String.valueOf(value)); } + /** + * 设置浮点数如果不存在 + * @see Redis Documentation: HSETNX + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param value 浮点数 + * @return 返回布尔值,成功true,失败false + */ + public Boolean setDoubleIfAbsent(String key, String hashKey, double value) { + return this.stringHashOperations.putIfAbsent(key, hashKey, String.valueOf(value)); + } + /** * 设置浮点数并设置过期时间如果不存在 * @see Redis Documentation: SETNX @@ -271,6 +453,22 @@ public final class NumberHandler implements RedisHandler { return null; } + /** + * 获取浮点数 + * @see Redis Documentation: HGET + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回浮点数 + */ + public Double getDouble(String key, String hashKey) { + String value = this.stringHashOperations.get(key, hashKey); + if (value!=null) { + return Double.valueOf(value); + } + return null; + } + /** * 设置长整数 * @see Redis Documentation: SET @@ -282,6 +480,18 @@ public final class NumberHandler implements RedisHandler { this.stringOperations.set(key, String.valueOf(value)); } + /** + * 设置长整数 + * @see Redis Documentation: HSET + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param value 值 + */ + public void setLong(String key, String hashKey, long value) { + this.stringHashOperations.put(key, hashKey, String.valueOf(value)); + } + /** * 设置长整数(若存在则更新过期时间) * @see Redis Documentation: SETEX @@ -307,6 +517,19 @@ public final class NumberHandler implements RedisHandler { return this.stringOperations.setIfAbsent(key, String.valueOf(value)); } + /** + * 设置长整数如果不存在 + * @see Redis Documentation: HSETNX + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param value 长整数 + * @return 返回布尔值,成功true,失败false + */ + public Boolean setLongIfAbsent(String key, String hashKey, long value) { + return this.stringHashOperations.putIfAbsent(key, hashKey, String.valueOf(value)); + } + /** * 设置长整数并设置过期时间如果不存在 * @see Redis Documentation: SETNX @@ -336,6 +559,22 @@ public final class NumberHandler implements RedisHandler { return null; } + /** + * 获取长整数 + * @see Redis Documentation: HGET + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回长整数 + */ + public Long getLong(String key, String hashKey) { + String value = this.stringHashOperations.get(key, hashKey); + if (value!=null) { + return Long.valueOf(value); + } + return null; + } + /** * 获取并设置浮点数 * @see Redis Documentation: GETSET @@ -369,10 +608,22 @@ public final class NumberHandler implements RedisHandler { * @param keys 键 * @return 返回移除数量 */ - public Long remove(String ...keys) { + public Long removeForValue(String ...keys) { return this.stringOperations.getOperations().delete(Arrays.asList(keys)); } + /** + * 移除 + * @see Redis Documentation: HDEL + * @since redis 2.0.0 + * @param key 键 + * @param hashKeys hash键 + * @return 返回移除数量 + */ + public Long removeForHash(String key, String ...hashKeys) { + return this.stringHashOperations.delete(key, (Object[]) hashKeys); + } + /** * 获取spring string redis模板 * @return 返回字符串模板 diff --git a/src/main/java/wiki/xsx/core/handler/SetHandler.java b/src/main/java/wiki/xsx/core/handler/SetHandler.java index 6cf1228..d30da0c 100644 --- a/src/main/java/wiki/xsx/core/handler/SetHandler.java +++ b/src/main/java/wiki/xsx/core/handler/SetHandler.java @@ -153,7 +153,7 @@ public final class SetHandler implements RedisHandler { * @return 返回移除字符串数量 */ public Long remove(String key, String ...values) { - return this.stringSetOperations.remove(key, Arrays.asList(values).toArray()); + return this.stringSetOperations.remove(key, (Object[]) values); } /** diff --git a/src/main/java/wiki/xsx/core/handler/ZsetHandler.java b/src/main/java/wiki/xsx/core/handler/ZsetHandler.java index aeb9016..9241601 100644 --- a/src/main/java/wiki/xsx/core/handler/ZsetHandler.java +++ b/src/main/java/wiki/xsx/core/handler/ZsetHandler.java @@ -546,7 +546,7 @@ public final class ZsetHandler implements RedisHandler { * @return 返回字符串移除数量 */ public Long remove(String key, String ...values) { - return this.stringZSetOperations.remove(key, Arrays.asList(values).toArray()); + return this.stringZSetOperations.remove(key, (Object[]) values); } /** -- Gitee From 7c0811422c42a66c192f165ba492e97a93c5e9c1 Mon Sep 17 00:00:00 2001 From: xsx Date: Thu, 13 Jun 2019 20:44:33 +0800 Subject: [PATCH 03/13] fix bug: StringHashOperations uninitialized --- src/main/java/wiki/xsx/core/handler/NumberHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/wiki/xsx/core/handler/NumberHandler.java b/src/main/java/wiki/xsx/core/handler/NumberHandler.java index 772f031..5332527 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -42,6 +42,7 @@ public final class NumberHandler implements RedisHandler { this.dbIndex = dbIndex; this.stringRedisTemplate = HandlerManager.createStringRedisTemplate(dbIndex); this.stringOperations = this.stringRedisTemplate.opsForValue(); + this.stringHashOperations = this.stringRedisTemplate.opsForHash(); } /** -- Gitee From 37d56e8f896a68de9b6114c2e1416180038bea0d Mon Sep 17 00:00:00 2001 From: xsx Date: Thu, 13 Jun 2019 21:53:00 +0800 Subject: [PATCH 04/13] add method --- pom.xml | 2 +- .../wiki/xsx/core/handler/NumberHandler.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 005a2af..e8b3c53 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ wiki.xsx spring-boot-starter-fast-redis - 1.5.1 + 1.5.2 jar spring-boot-starter-fast-redis http://www.xsx.wiki diff --git a/src/main/java/wiki/xsx/core/handler/NumberHandler.java b/src/main/java/wiki/xsx/core/handler/NumberHandler.java index 5332527..e40f928 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -589,6 +589,22 @@ public final class NumberHandler implements RedisHandler { return value!=null?Double.valueOf(value):null; } + /** + * 获取并设置浮点数 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 1.0.0 + * @param key 键 + * @param hashKey hash键 + * @param newValue 新值 + * @return 返回原值 + */ + public Double getAndSetDouble(String key, String hashKey, double newValue) { + Double value = this.getDouble(key, hashKey); + this.setDouble(key, hashKey, newValue); + return value; + } + /** * 获取并设置长整数 * @see Redis Documentation: GETSET @@ -602,6 +618,22 @@ public final class NumberHandler implements RedisHandler { return value!=null?Long.valueOf(value):null; } + /** + * 获取并设置长整数 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 1.0.0 + * @param key 键 + * @param hashKey hash键 + * @param newValue 新值 + * @return 返回原值 + */ + public Long getAndSetLong(String key, String hashKey, long newValue) { + Long value = this.getLong(key, hashKey); + this.setLong(key, hashKey, newValue); + return value; + } + /** * 移除 * @see Redis Documentation: DEL -- Gitee From 2a4d053aeddbdfe6e9bf23580f9fe43e0ef97559 Mon Sep 17 00:00:00 2001 From: "xsx344646090@vip.qq.com" Date: Fri, 14 Jun 2019 10:02:20 +0800 Subject: [PATCH 05/13] add sync method --- .../wiki/xsx/core/handler/NumberHandler.java | 626 +++++++++++------- 1 file changed, 370 insertions(+), 256 deletions(-) diff --git a/src/main/java/wiki/xsx/core/handler/NumberHandler.java b/src/main/java/wiki/xsx/core/handler/NumberHandler.java index e40f928..216d995 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -73,401 +73,352 @@ public final class NumberHandler implements RedisHandler { } /** - * 增加浮点数 - * @see Redis Documentation: INCRBYFLOAT - * @since redis 2.6.0 + * 设置浮点数 + * @see Redis Documentation: SET + * @since redis 2.0.0 * @param key 键 - * @param data 步长 - * @return 返回增加后的值 + * @param value 值 */ - public Double addDouble(String key, double data) { - return this.stringOperations.increment(key, data); + public void setDouble(String key, double value) { + this.stringOperations.set(key, String.valueOf(value)); } /** - * 增加浮点数 - * @see Redis Documentation: HINCRBYFLOAT - * @since redis 2.6.0 + * 设置浮点数 + * @see Redis Documentation: HSET + * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 - * @param data 步长 - * @return 返回增加后的值 - */ - public Double addDouble(String key, String hashKey, double data) { - return this.stringHashOperations.increment(key, hashKey, data); - } - - /** - * 获取并增加浮点数 - * @see Redis Documentation: INCRBYFLOAT - * @since redis 2.6.0 - * @param key 键 - * @param data 步长 - * @return 返回原值 + * @param value 值 */ - public Double getAndAddDouble(String key, double data) { - return this.addDouble(key, data) - data; + public void setDouble(String key, String hashKey, double value) { + this.stringHashOperations.put(key, hashKey, String.valueOf(value)); } /** - * 获取并增加浮点数 - * @see Redis Documentation: HINCRBYFLOAT - * @since redis 2.6.0 + * 设置浮点数(若存在则更新过期时间) + * @see Redis Documentation: SETEX + * @since redis 2.0.0 * @param key 键 - * @param hashKey hash键 - * @param data 步长 - * @return 返回原值 + * @param value 值 + * @param timeout 过期时间 + * @param unit 时间单位 */ - public Double getAndAddDouble(String key, String hashKey, double data) { - return this.addDouble(key, hashKey, data) - data; + public void setDouble(String key, double value, long timeout, TimeUnit unit) { + this.stringOperations.set(key, String.valueOf(value), timeout, unit); } /** - * 增加长整数 - * @see Redis Documentation: INCRBY + * 设置浮点数如果不存在 + * @see Redis Documentation: SETNX * @since redis 1.0.0 * @param key 键 - * @param data 步长 - * @return 返回增加后的值 + * @param value 浮点数 + * @return 返回布尔值,成功true,失败false */ - public Long addLong(String key, long data) { - return this.stringOperations.increment(key, data); + public Boolean setDoubleIfAbsent(String key, double value) { + return this.stringOperations.setIfAbsent(key, String.valueOf(value)); } /** - * 增加长整数 - * @see Redis Documentation: HINCRBY + * 设置浮点数如果不存在 + * @see Redis Documentation: HSETNX * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 - * @param data 步长 - * @return 返回增加后的值 - */ - public Long addLong(String key, String hashKey, long data) { - return this.stringHashOperations.increment(key, hashKey, data); - } - - /** - * 获取并增加长整数 - * @see Redis Documentation: INCRBY - * @since redis 1.0.0 - * @param key 键 - * @param data 步长 - * @return 返回原值 + * @param value 浮点数 + * @return 返回布尔值,成功true,失败false */ - public Long getAndAddLong(String key, long data) { - return this.addLong(key, data) - data; + public Boolean setDoubleIfAbsent(String key, String hashKey, double value) { + return this.stringHashOperations.putIfAbsent(key, hashKey, String.valueOf(value)); } /** - * 获取并增加长整数 - * @see Redis Documentation: HINCRBY - * @since redis 2.0.0 + * 设置浮点数并设置过期时间如果不存在 + * @see Redis Documentation: SETNX + * @since redis 2.6.12 * @param key 键 - * @param hashKey hash键 - * @param data 步长 - * @return 返回原值 + * @param value 浮点数 + * @param timeout 过期时间 + * @param unit 时间单位 + * @return 返回布尔值,成功true,失败false */ - public Long getAndAddLong(String key, String hashKey, long data) { - return this.addLong(key, hashKey, data) - data; + public Boolean setDoubleIfAbsent(String key, double value, long timeout, TimeUnit unit) { + return this.stringOperations.setIfAbsent(key, String.valueOf(value), timeout, unit); } /** - * 长整数自增 - * @see Redis Documentation: INCR + * 获取浮点数 + * @see Redis Documentation: GET * @since redis 1.0.0 * @param key 键 - * @return 返回自增后的值 + * @return 返回浮点数 */ - public Long incrementLong(String key) { - return this.stringOperations.increment(key); + public Double getDouble(String key) { + String value = this.stringOperations.get(key); + if (value!=null) { + return Double.valueOf(value); + } + return null; } /** - * 长整数自增 - * @see Redis Documentation: HINCRBY + * 获取浮点数 + * @see Redis Documentation: HGET * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 - * @return 返回自增后的值 + * @return 返回浮点数 */ - public Long incrementLong(String key, String hashKey) { - return this.addLong(key, hashKey, 1L); + public Double getDouble(String key, String hashKey) { + String value = this.stringHashOperations.get(key, hashKey); + if (value!=null) { + return Double.valueOf(value); + } + return null; } /** - * 获取并自增长整数 - * @see Redis Documentation: INCR + * 获取并设置浮点数 + * @see Redis Documentation: GETSET * @since redis 1.0.0 * @param key 键 + * @param newValue 新值 * @return 返回原值 */ - public Long getAndIncrementLong(String key) { - return this.incrementLong(key) - 1L; + public Double getAndSetDouble(String key, double newValue) { + String value = this.stringOperations.getAndSet(key, String.valueOf(newValue)); + return value!=null?Double.valueOf(value):null; } /** - * 获取并自增长整数 - * @see Redis Documentation: HINCRBY - * @since redis 2.0.0 + * 获取并设置浮点数 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 1.0.0 * @param key 键 * @param hashKey hash键 + * @param newValue 新值 * @return 返回原值 */ - public Long getAndIncrementLong(String key, String hashKey) { - return this.incrementLong(key, hashKey) - 1L; + public Double getAndSetDouble(String key, String hashKey, double newValue) { + Double value = this.getDouble(key, hashKey); + this.setDouble(key, hashKey, newValue); + return value; } /** - * 浮点数减小 + * 增加浮点数 * @see Redis Documentation: INCRBYFLOAT * @since redis 2.6.0 * @param key 键 * @param data 步长 - * @return 返回相减后的值 - */ - public Double subtractDouble(String key, double data) { - return this.addDouble(key, -data); - } - - /** - * 浮点数减小 - * @see Redis Documentation: HINCRBYFLOAT - * @since redis 2.6.0 - * @param key 键 - * @param hashKey hash键 - * @param data 步长 - * @return 返回相减后的值 + * @return 返回增加后的值 */ - public Double subtractDouble(String key, String hashKey, double data) { - return this.addDouble(key, hashKey, -data); + public Double addDouble(String key, double data) { + return this.stringOperations.increment(key, data); } /** - * 获取并减小浮点数 - * @see Redis Documentation: INCRBYFLOAT + * 增加浮点数 + * @see Redis Documentation: GET + * @see Redis Documentation: SET * @since redis 2.6.0 * @param key 键 * @param data 步长 - * @return 返回原值 + * @return 返回增加后的值 */ - public Double getAndSubtractDouble(String key, double data) { - return this.addDouble(key, -data) + data; + public synchronized Double addDoubleBySync(String key, double data) { + Double old = this.getDouble(key); + double value = (old==null?0:old) + data; + this.setDouble(key, value); + return value; } /** - * 获取并减小浮点数 + * 增加浮点数 * @see Redis Documentation: HINCRBYFLOAT * @since redis 2.6.0 * @param key 键 * @param hashKey hash键 * @param data 步长 - * @return 返回原值 + * @return 返回增加后的值 */ - public Double getAndSubtractDouble(String key, String hashKey, double data) { - return this.subtractDouble(key, hashKey, data) + data; + public Double addDouble(String key, String hashKey, double data) { + return this.stringHashOperations.increment(key, hashKey, data); } /** - * 长整数减小 - * @see Redis Documentation: DECRBY - * @since redis 1.0.0 + * 增加浮点数 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 2.6.0 * @param key 键 + * @param hashKey hash键 * @param data 步长 - * @return 返回减小后的值 + * @return 返回增加后的值 */ - public Long subtractLong(String key, long data) { - return this.stringOperations.decrement(key, data); + public synchronized Double addDoubleBySync(String key, String hashKey, double data) { + Double old = this.getDouble(key, hashKey); + double value = (old==null?0:old) + data; + this.setDouble(key, hashKey, value); + return value; } /** - * 长整数减小 - * @see Redis Documentation: HINCRBY - * @since redis 2.0.0 + * 获取并增加浮点数 + * @see Redis Documentation: INCRBYFLOAT + * @since redis 2.6.0 * @param key 键 - * @param hashKey hash键 * @param data 步长 - * @return 返回减小后的值 + * @return 返回原值 */ - public Long subtractLong(String key, String hashKey, long data) { - return this.stringHashOperations.increment(key, hashKey, -data); + public Double getAndAddDouble(String key, double data) { + return this.addDouble(key, data) - data; } /** - * 获取并减小长整数 - * @see Redis Documentation: DECRBY - * @since redis 1.0.0 + * 获取并增加浮点数 + * @see Redis Documentation: GET + * @see Redis Documentation: SET + * @since redis 2.6.0 * @param key 键 * @param data 步长 * @return 返回原值 */ - public Long getAndSubtractLong(String key, long data) { - return this.subtractLong(key, data) + data; + public synchronized Double getAndAddDoubleBySync(String key, double data) { + return this.addDoubleBySync(key, data) - data; } /** - * 获取并减小长整数 - * @see Redis Documentation: HINCRBY - * @since redis 2.0.0 + * 获取并增加浮点数 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 * @param key 键 * @param hashKey hash键 * @param data 步长 * @return 返回原值 */ - public Long getAndSubtractLong(String key, String hashKey, long data) { - return this.subtractLong(key, hashKey, data) + data; - } - - /** - * 长整数递减 - * @see Redis Documentation: DECR - * @since redis 1.0.0 - * @param key 键 - * @return 返回递减后的值 - */ - public Long decrementLong(String key) { - return this.stringOperations.decrement(key); + public Double getAndAddDouble(String key, String hashKey, double data) { + return this.addDouble(key, hashKey, data) - data; } /** - * 长整数递减 - * @see Redis Documentation: HINCRBY - * @since redis 2.0.0 + * 获取并增加浮点数 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 2.6.0 * @param key 键 * @param hashKey hash键 - * @return 返回递减后的值 - */ - public Long decrementLong(String key, String hashKey) { - return this.subtractLong(key, hashKey, 1L); - } - - /** - * 获取并递减长整数 - * @see Redis Documentation: DECR - * @since redis 1.0.0 - * @param key 键 + * @param data 步长 * @return 返回原值 */ - public Long getAndDecrementLong(String key) { - return this.decrementLong(key) + 1L; + public synchronized Double getAndAddDoubleBySync(String key, String hashKey, double data) { + return this.addDoubleBySync(key, hashKey, data) - data; } /** - * 获取并递减长整数 - * @see Redis Documentation: HINCRBY - * @since redis 2.0.0 + * 浮点数减小 + * @see Redis Documentation: INCRBYFLOAT + * @since redis 2.6.0 * @param key 键 - * @param hashKey hash键 - * @return 返回原值 + * @param data 步长 + * @return 返回相减后的值 */ - public Long getAndDecrementLong(String key, String hashKey) { - return this.decrementLong(key, hashKey) + 1L; + public Double subtractDouble(String key, double data) { + return this.addDouble(key, -data); } /** - * 设置浮点数 + * 浮点数减小 + * @see Redis Documentation: GET * @see Redis Documentation: SET - * @since redis 2.0.0 + * @since redis 2.6.0 * @param key 键 - * @param value 值 + * @param data 步长 + * @return 返回相减后的值 */ - public void setDouble(String key, double value) { - this.stringOperations.set(key, String.valueOf(value)); + public synchronized Double subtractDoubleBySync(String key, double data) { + return this.addDoubleBySync(key, -data); } /** - * 设置浮点数 - * @see Redis Documentation: HSET - * @since redis 2.0.0 + * 浮点数减小 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 * @param key 键 * @param hashKey hash键 - * @param value 值 - */ - public void setDouble(String key, String hashKey, double value) { - this.stringHashOperations.put(key, hashKey, String.valueOf(value)); - } - - /** - * 设置浮点数(若存在则更新过期时间) - * @see Redis Documentation: SETEX - * @since redis 2.0.0 - * @param key 键 - * @param value 值 - * @param timeout 过期时间 - * @param unit 时间单位 + * @param data 步长 + * @return 返回相减后的值 */ - public void setDouble(String key, double value, long timeout, TimeUnit unit) { - this.stringOperations.set(key, String.valueOf(value), timeout, unit); + public Double subtractDouble(String key, String hashKey, double data) { + return this.addDouble(key, hashKey, -data); } /** - * 设置浮点数如果不存在 - * @see Redis Documentation: SETNX - * @since redis 1.0.0 + * 浮点数减小 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 2.6.0 * @param key 键 - * @param value 浮点数 - * @return 返回布尔值,成功true,失败false + * @param hashKey hash键 + * @param data 步长 + * @return 返回相减后的值 */ - public Boolean setDoubleIfAbsent(String key, double value) { - return this.stringOperations.setIfAbsent(key, String.valueOf(value)); + public synchronized Double subtractDoubleBySync(String key, String hashKey, double data) { + return this.addDoubleBySync(key, hashKey, -data); } /** - * 设置浮点数如果不存在 - * @see Redis Documentation: HSETNX - * @since redis 2.0.0 + * 获取并减小浮点数 + * @see Redis Documentation: INCRBYFLOAT + * @since redis 2.6.0 * @param key 键 - * @param hashKey hash键 - * @param value 浮点数 - * @return 返回布尔值,成功true,失败false + * @param data 步长 + * @return 返回原值 */ - public Boolean setDoubleIfAbsent(String key, String hashKey, double value) { - return this.stringHashOperations.putIfAbsent(key, hashKey, String.valueOf(value)); + public Double getAndSubtractDouble(String key, double data) { + return this.addDouble(key, -data) + data; } /** - * 设置浮点数并设置过期时间如果不存在 - * @see Redis Documentation: SETNX - * @since redis 2.6.12 + * 获取并减小浮点数 + * @see Redis Documentation: GET + * @see Redis Documentation: SET + * @since redis 2.6.0 * @param key 键 - * @param value 浮点数 - * @param timeout 过期时间 - * @param unit 时间单位 - * @return 返回布尔值,成功true,失败false + * @param data 步长 + * @return 返回原值 */ - public Boolean setDoubleIfAbsent(String key, double value, long timeout, TimeUnit unit) { - return this.stringOperations.setIfAbsent(key, String.valueOf(value), timeout, unit); + public synchronized Double getAndSubtractDoubleBySync(String key, double data) { + return this.addDoubleBySync(key, -data) + data; } /** - * 获取浮点数 - * @see Redis Documentation: GET - * @since redis 1.0.0 + * 获取并减小浮点数 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 * @param key 键 - * @return 返回浮点数 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 */ - public Double getDouble(String key) { - String value = this.stringOperations.get(key); - if (value!=null) { - return Double.valueOf(value); - } - return null; + public Double getAndSubtractDouble(String key, String hashKey, double data) { + return this.subtractDouble(key, hashKey, data) + data; } /** - * 获取浮点数 + * 获取并减小浮点数 * @see Redis Documentation: HGET - * @since redis 2.0.0 + * @see Redis Documentation: HSET + * @since redis 2.6.0 * @param key 键 * @param hashKey hash键 - * @return 返回浮点数 + * @param data 步长 + * @return 返回原值 */ - public Double getDouble(String key, String hashKey) { - String value = this.stringHashOperations.get(key, hashKey); - if (value!=null) { - return Double.valueOf(value); - } - return null; + public synchronized Double getAndSubtractDoubleBySync(String key, String hashKey, double data) { + return this.subtractDoubleBySync(key, hashKey, data) + data; } /** @@ -577,20 +528,20 @@ public final class NumberHandler implements RedisHandler { } /** - * 获取并设置浮点数 + * 获取并设置长整数 * @see Redis Documentation: GETSET * @since redis 1.0.0 * @param key 键 * @param newValue 新值 * @return 返回原值 */ - public Double getAndSetDouble(String key, double newValue) { + public Long getAndSetLong(String key, long newValue) { String value = this.stringOperations.getAndSet(key, String.valueOf(newValue)); - return value!=null?Double.valueOf(value):null; + return value!=null?Long.valueOf(value):null; } /** - * 获取并设置浮点数 + * 获取并设置长整数 * @see Redis Documentation: HGET * @see Redis Documentation: HSET * @since redis 1.0.0 @@ -599,39 +550,202 @@ public final class NumberHandler implements RedisHandler { * @param newValue 新值 * @return 返回原值 */ - public Double getAndSetDouble(String key, String hashKey, double newValue) { - Double value = this.getDouble(key, hashKey); - this.setDouble(key, hashKey, newValue); + public Long getAndSetLong(String key, String hashKey, long newValue) { + Long value = this.getLong(key, hashKey); + this.setLong(key, hashKey, newValue); return value; } /** - * 获取并设置长整数 - * @see Redis Documentation: GETSET + * 增加长整数 + * @see Redis Documentation: INCRBY * @since redis 1.0.0 * @param key 键 - * @param newValue 新值 + * @param data 步长 + * @return 返回增加后的值 + */ + public Long addLong(String key, long data) { + return this.stringOperations.increment(key, data); + } + + /** + * 增加长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回增加后的值 + */ + public Long addLong(String key, String hashKey, long data) { + return this.stringHashOperations.increment(key, hashKey, data); + } + + /** + * 获取并增加长整数 + * @see Redis Documentation: INCRBY + * @since redis 1.0.0 + * @param key 键 + * @param data 步长 * @return 返回原值 */ - public Long getAndSetLong(String key, long newValue) { - String value = this.stringOperations.getAndSet(key, String.valueOf(newValue)); - return value!=null?Long.valueOf(value):null; + public Long getAndAddLong(String key, long data) { + return this.addLong(key, data) - data; } /** - * 获取并设置长整数 - * @see Redis Documentation: HGET - * @see Redis Documentation: HSET + * 获取并增加长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 + */ + public Long getAndAddLong(String key, String hashKey, long data) { + return this.addLong(key, hashKey, data) - data; + } + + /** + * 长整数自增 + * @see Redis Documentation: INCR + * @since redis 1.0.0 + * @param key 键 + * @return 返回自增后的值 + */ + public Long incrementLong(String key) { + return this.stringOperations.increment(key); + } + + /** + * 长整数自增 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回自增后的值 + */ + public Long incrementLong(String key, String hashKey) { + return this.addLong(key, hashKey, 1L); + } + + /** + * 获取并自增长整数 + * @see Redis Documentation: INCR * @since redis 1.0.0 * @param key 键 + * @return 返回原值 + */ + public Long getAndIncrementLong(String key) { + return this.incrementLong(key) - 1L; + } + + /** + * 获取并自增长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 * @param hashKey hash键 - * @param newValue 新值 * @return 返回原值 */ - public Long getAndSetLong(String key, String hashKey, long newValue) { - Long value = this.getLong(key, hashKey); - this.setLong(key, hashKey, newValue); - return value; + public Long getAndIncrementLong(String key, String hashKey) { + return this.incrementLong(key, hashKey) - 1L; + } + + /** + * 长整数减小 + * @see Redis Documentation: DECRBY + * @since redis 1.0.0 + * @param key 键 + * @param data 步长 + * @return 返回减小后的值 + */ + public Long subtractLong(String key, long data) { + return this.stringOperations.decrement(key, data); + } + + /** + * 长整数减小 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回减小后的值 + */ + public Long subtractLong(String key, String hashKey, long data) { + return this.stringHashOperations.increment(key, hashKey, -data); + } + + /** + * 获取并减小长整数 + * @see Redis Documentation: DECRBY + * @since redis 1.0.0 + * @param key 键 + * @param data 步长 + * @return 返回原值 + */ + public Long getAndSubtractLong(String key, long data) { + return this.subtractLong(key, data) + data; + } + + /** + * 获取并减小长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 + */ + public Long getAndSubtractLong(String key, String hashKey, long data) { + return this.subtractLong(key, hashKey, data) + data; + } + + /** + * 长整数递减 + * @see Redis Documentation: DECR + * @since redis 1.0.0 + * @param key 键 + * @return 返回递减后的值 + */ + public Long decrementLong(String key) { + return this.stringOperations.decrement(key); + } + + /** + * 长整数递减 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回递减后的值 + */ + public Long decrementLong(String key, String hashKey) { + return this.subtractLong(key, hashKey, 1L); + } + + /** + * 获取并递减长整数 + * @see Redis Documentation: DECR + * @since redis 1.0.0 + * @param key 键 + * @return 返回原值 + */ + public Long getAndDecrementLong(String key) { + return this.decrementLong(key) + 1L; + } + + /** + * 获取并递减长整数 + * @see Redis Documentation: HINCRBY + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @return 返回原值 + */ + public Long getAndDecrementLong(String key, String hashKey) { + return this.decrementLong(key, hashKey) + 1L; } /** -- Gitee From 90b675a92bd2e4c1289272555be4faf03eb793cb Mon Sep 17 00:00:00 2001 From: "xsx344646090@vip.qq.com" Date: Fri, 14 Jun 2019 11:26:58 +0800 Subject: [PATCH 06/13] fix double precision problem --- .../wiki/xsx/core/handler/NumberHandler.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/wiki/xsx/core/handler/NumberHandler.java b/src/main/java/wiki/xsx/core/handler/NumberHandler.java index 216d995..73123ea 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -7,6 +7,7 @@ import org.springframework.data.redis.support.atomic.RedisAtomicDouble; import org.springframework.data.redis.support.atomic.RedisAtomicInteger; import org.springframework.data.redis.support.atomic.RedisAtomicLong; +import java.math.BigDecimal; import java.util.Arrays; import java.util.concurrent.TimeUnit; @@ -230,7 +231,7 @@ public final class NumberHandler implements RedisHandler { */ public synchronized Double addDoubleBySync(String key, double data) { Double old = this.getDouble(key); - double value = (old==null?0:old) + data; + double value = new BigDecimal(Double.toString((old==null?0: old))).add(new BigDecimal(Double.toString(data))).doubleValue(); this.setDouble(key, value); return value; } @@ -260,7 +261,7 @@ public final class NumberHandler implements RedisHandler { */ public synchronized Double addDoubleBySync(String key, String hashKey, double data) { Double old = this.getDouble(key, hashKey); - double value = (old==null?0:old) + data; + double value = new BigDecimal(Double.toString((old==null?0: old))).add(new BigDecimal(Double.toString(data))).doubleValue(); this.setDouble(key, hashKey, value); return value; } @@ -274,7 +275,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public Double getAndAddDouble(String key, double data) { - return this.addDouble(key, data) - data; + return new BigDecimal(Double.toString(this.addDouble(key, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -287,7 +288,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public synchronized Double getAndAddDoubleBySync(String key, double data) { - return this.addDoubleBySync(key, data) - data; + return new BigDecimal(Double.toString(this.addDoubleBySync(key, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -300,7 +301,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public Double getAndAddDouble(String key, String hashKey, double data) { - return this.addDouble(key, hashKey, data) - data; + return new BigDecimal(Double.toString(this.addDouble(key, hashKey, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -314,7 +315,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public synchronized Double getAndAddDoubleBySync(String key, String hashKey, double data) { - return this.addDoubleBySync(key, hashKey, data) - data; + return new BigDecimal(Double.toString(this.addDoubleBySync(key, hashKey, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -378,7 +379,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public Double getAndSubtractDouble(String key, double data) { - return this.addDouble(key, -data) + data; + return new BigDecimal(Double.toString(this.subtractDouble(key, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -391,7 +392,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public synchronized Double getAndSubtractDoubleBySync(String key, double data) { - return this.addDoubleBySync(key, -data) + data; + return new BigDecimal(Double.toString(this.subtractDoubleBySync(key, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -404,7 +405,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public Double getAndSubtractDouble(String key, String hashKey, double data) { - return this.subtractDouble(key, hashKey, data) + data; + return new BigDecimal(Double.toString(this.subtractDouble(key, hashKey, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -418,7 +419,7 @@ public final class NumberHandler implements RedisHandler { * @return 返回原值 */ public synchronized Double getAndSubtractDoubleBySync(String key, String hashKey, double data) { - return this.subtractDoubleBySync(key, hashKey, data) + data; + return new BigDecimal(Double.toString(this.subtractDoubleBySync(key, hashKey, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); } /** -- Gitee From cec1f960360c86266853833e514002658fbc78b9 Mon Sep 17 00:00:00 2001 From: "xsx344646090@vip.qq.com" Date: Fri, 14 Jun 2019 11:29:49 +0800 Subject: [PATCH 07/13] fix double precision problem --- src/main/java/wiki/xsx/core/handler/NumberHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/wiki/xsx/core/handler/NumberHandler.java b/src/main/java/wiki/xsx/core/handler/NumberHandler.java index 73123ea..7782dc1 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -231,7 +231,7 @@ public final class NumberHandler implements RedisHandler { */ public synchronized Double addDoubleBySync(String key, double data) { Double old = this.getDouble(key); - double value = new BigDecimal(Double.toString((old==null?0: old))).add(new BigDecimal(Double.toString(data))).doubleValue(); + double value = new BigDecimal(old==null?"0":Double.toString(old)).add(new BigDecimal(Double.toString(data))).doubleValue(); this.setDouble(key, value); return value; } @@ -261,7 +261,7 @@ public final class NumberHandler implements RedisHandler { */ public synchronized Double addDoubleBySync(String key, String hashKey, double data) { Double old = this.getDouble(key, hashKey); - double value = new BigDecimal(Double.toString((old==null?0: old))).add(new BigDecimal(Double.toString(data))).doubleValue(); + double value = new BigDecimal(old==null?"0":Double.toString(old)).add(new BigDecimal(Double.toString(data))).doubleValue(); this.setDouble(key, hashKey, value); return value; } -- Gitee From 87839969927e198f8e4a14b0c44dfcefbc2d4e51 Mon Sep 17 00:00:00 2001 From: "xsx344646090@vip.qq.com" Date: Fri, 14 Jun 2019 11:33:34 +0800 Subject: [PATCH 08/13] fix double precision problem --- .../wiki/xsx/core/handler/NumberHandler.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/wiki/xsx/core/handler/NumberHandler.java b/src/main/java/wiki/xsx/core/handler/NumberHandler.java index 7782dc1..4ea8d1c 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -196,7 +196,7 @@ public final class NumberHandler implements RedisHandler { * 获取并设置浮点数 * @see Redis Documentation: HGET * @see Redis Documentation: HSET - * @since redis 1.0.0 + * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 * @param newValue 新值 @@ -224,7 +224,7 @@ public final class NumberHandler implements RedisHandler { * 增加浮点数 * @see Redis Documentation: GET * @see Redis Documentation: SET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param data 步长 * @return 返回增加后的值 @@ -253,7 +253,7 @@ public final class NumberHandler implements RedisHandler { * 增加浮点数 * @see Redis Documentation: HGET * @see Redis Documentation: HSET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 * @param data 步长 @@ -282,7 +282,7 @@ public final class NumberHandler implements RedisHandler { * 获取并增加浮点数 * @see Redis Documentation: GET * @see Redis Documentation: SET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param data 步长 * @return 返回原值 @@ -308,7 +308,7 @@ public final class NumberHandler implements RedisHandler { * 获取并增加浮点数 * @see Redis Documentation: HGET * @see Redis Documentation: HSET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 * @param data 步长 @@ -334,7 +334,7 @@ public final class NumberHandler implements RedisHandler { * 浮点数减小 * @see Redis Documentation: GET * @see Redis Documentation: SET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param data 步长 * @return 返回相减后的值 @@ -360,7 +360,7 @@ public final class NumberHandler implements RedisHandler { * 浮点数减小 * @see Redis Documentation: HGET * @see Redis Documentation: HSET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 * @param data 步长 @@ -386,7 +386,7 @@ public final class NumberHandler implements RedisHandler { * 获取并减小浮点数 * @see Redis Documentation: GET * @see Redis Documentation: SET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param data 步长 * @return 返回原值 @@ -412,7 +412,7 @@ public final class NumberHandler implements RedisHandler { * 获取并减小浮点数 * @see Redis Documentation: HGET * @see Redis Documentation: HSET - * @since redis 2.6.0 + * @since redis 2.0.0 * @param key 键 * @param hashKey hash键 * @param data 步长 -- Gitee From 90300ab83d485879cd55861d408466af93b470e3 Mon Sep 17 00:00:00 2001 From: "xsx344646090@vip.qq.com" Date: Fri, 14 Jun 2019 17:04:28 +0800 Subject: [PATCH 09/13] =?UTF-8?q?add=20method:=20"getRole"=E3=80=81"slaveO?= =?UTF-8?q?f"=20and=20"slaveOfNoOne"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/wiki/xsx/core/handler/DBHandler.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/wiki/xsx/core/handler/DBHandler.java b/src/main/java/wiki/xsx/core/handler/DBHandler.java index c738f0f..3478e39 100644 --- a/src/main/java/wiki/xsx/core/handler/DBHandler.java +++ b/src/main/java/wiki/xsx/core/handler/DBHandler.java @@ -7,6 +7,7 @@ import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; +import wiki.xsx.core.util.RedisUtil; import java.time.Clock; import java.util.Arrays; @@ -436,6 +437,39 @@ public final class DBHandler implements RedisHandler { return this.redisTemplate.getRequiredConnectionFactory().getConnection().lastSave(); } + /** + * 角色的信息 + * @see Redis Documentation: ROLE + * @since redis 2.8.12 + * @return 返回当前是master,slave还是sentinel + */ + @SuppressWarnings("unchecked") + public String getRole() { + CustomCommandHandler commandHandler = RedisUtil.getCustomCommandHandler(this.dbIndex); + List list = (List) commandHandler.executeCommand("ROLE", null); + return commandHandler.deserialize((byte[]) list.get(0)); + } + + /** + * 转为从服务器 + * @see Redis Documentation: SLAVEOF + * @since redis 1.0.0 + * @param ip 主服务器IP + * @param port 主服务器端口 + */ + public void slaveOf(String ip, int port) { + this.redisTemplate.slaveOf(ip, port); + } + + /** + * 转为主服务器 + * @see Redis Documentation: SLAVEOF NO ONE + * @since redis 1.0.0 + */ + public void slaveOfNoOne() { + this.redisTemplate.slaveOfNoOne(); + } + /** * 获取spring redis模板 * @return 返回对象模板 -- Gitee From 5b620ff043a96218fa447489d590cf797295ee83 Mon Sep 17 00:00:00 2001 From: "xsx344646090@vip.qq.com" Date: Fri, 14 Jun 2019 17:44:19 +0800 Subject: [PATCH 10/13] =?UTF-8?q?add=20method:=20"getClientList"=E3=80=81"?= =?UTF-8?q?killClient"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/wiki/xsx/core/handler/DBHandler.java | 209 ++++++++++-------- 1 file changed, 116 insertions(+), 93 deletions(-) diff --git a/src/main/java/wiki/xsx/core/handler/DBHandler.java b/src/main/java/wiki/xsx/core/handler/DBHandler.java index 3478e39..457d156 100644 --- a/src/main/java/wiki/xsx/core/handler/DBHandler.java +++ b/src/main/java/wiki/xsx/core/handler/DBHandler.java @@ -6,6 +6,7 @@ import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.types.RedisClientInfo; import org.springframework.data.redis.serializer.RedisSerializer; import wiki.xsx.core.util.RedisUtil; @@ -130,73 +131,6 @@ public final class DBHandler implements RedisHandler { return this.dbIndex; } - /** - * 检查连接 - * @see Redis Documentation: PING - * @since redis 1.0.0 - * @return 返回延迟时间(ms) - */ - public Long ping() { - RedisConnection connection = this.redisTemplate.getRequiredConnectionFactory().getConnection(); - long begin = CLOCK.millis(); - connection.ping(); - long end = CLOCK.millis(); - return end - begin; - } - - /** - * 服务器时间 - * @see Redis Documentation: TIME - * @since redis 2.6.0 - * @return 返回服务器时间(ms) - */ - public Long time() { - return this.redisTemplate.getRequiredConnectionFactory().getConnection().serverCommands().time(); - } - - /** - * 测试打印 - * @see Redis Documentation: ECHO - * @since redis 1.0.0 - * @param message 信息 - * @return 返回打印信息 - */ - public String echo(String message) { - RedisSerializer serializer = RedisSerializer.string(); - return serializer.deserialize( - this.redisTemplate - .getRequiredConnectionFactory() - .getConnection() - .echo(serializer.serialize(message)) - ); - } - - /** - * 清理当前数据库 - * @see Redis Documentation: FLUSHDB - * @since redis 1.0.0 - */ - public void clearDB() { - try { - this.redisTemplate.getRequiredConnectionFactory().getConnection().serverCommands().flushDb(); - }catch (IllegalStateException e) { - log.error(e.getMessage()); - } - } - - /** - * 清理所有数据库 - * @see Redis Documentation: FLUSHALL - * @since redis 1.0.0 - */ - public void clearDBAll() { - try { - this.redisTemplate.getRequiredConnectionFactory().getConnection().serverCommands().flushAll(); - }catch (IllegalStateException e) { - log.error(e.getMessage()); - } - } - /** * 获取当前数据库信息 * @see Redis Documentation: INFO @@ -234,6 +168,32 @@ public final class DBHandler implements RedisHandler { return serverCommands.info(DBOption.DEFAULT.option); } + /** + * 清理当前数据库 + * @see Redis Documentation: FLUSHDB + * @since redis 1.0.0 + */ + public void clearDB() { + try { + this.redisTemplate.getRequiredConnectionFactory().getConnection().serverCommands().flushDb(); + }catch (IllegalStateException e) { + log.error(e.getMessage()); + } + } + + /** + * 清理所有数据库 + * @see Redis Documentation: FLUSHALL + * @since redis 1.0.0 + */ + public void clearDBAll() { + try { + this.redisTemplate.getRequiredConnectionFactory().getConnection().serverCommands().flushAll(); + }catch (IllegalStateException e) { + log.error(e.getMessage()); + } + } + /** * 设置数据库配置参数 * @see Redis Documentation: CONFIG SET @@ -250,6 +210,7 @@ public final class DBHandler implements RedisHandler { * @see Redis Documentation: CONFIG GET * @since redis 2.0.0 * @param param 参数名 + * @return 返回数据库配置信息 */ public Properties getConfig(String param) { return this.redisTemplate.getRequiredConnectionFactory().getConnection().getConfig(param); @@ -272,9 +233,9 @@ public final class DBHandler implements RedisHandler { */ public void setClientNameAsObj(String name) { this.redisTemplate - .getRequiredConnectionFactory() - .getConnection() - .setClientName(RedisSerializer.string().serialize(name)); + .getRequiredConnectionFactory() + .getConnection() + .setClientName(RedisSerializer.string().serialize(name)); } /** @@ -285,15 +246,16 @@ public final class DBHandler implements RedisHandler { */ public void setClientName(String name) { this.stringRedisTemplate - .getRequiredConnectionFactory() - .getConnection() - .setClientName(RedisSerializer.string().serialize(name)); + .getRequiredConnectionFactory() + .getConnection() + .setClientName(RedisSerializer.string().serialize(name)); } /** * 获取对象客户端连接名称 * @see Redis Documentation: CLIENT GETNAME * @since redis 2.6.9 + * @return 返回客户端连接名称 */ public String getClientNameAsObj() { return this.redisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); @@ -303,11 +265,62 @@ public final class DBHandler implements RedisHandler { * 获取字符串客户端连接名称 * @see Redis Documentation: CLIENT GETNAME * @since redis 2.6.9 + * @return 返回客户端连接名称 */ public String getClientName() { return this.stringRedisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); } + /** + * 获取客户端连接列表 + * @return 返回客户端连接列表 + */ + public List getClientList() { + return this.redisTemplate.getRequiredConnectionFactory().getConnection().getClientList(); + } + + /** + * 关闭客户端 + * @param ip 客户端IP + * @param port 客户端端口 + */ + public void killClient(String ip, int port) { + this.redisTemplate.killClient(ip, port); + } + + /** + * 角色的信息 + * @see Redis Documentation: ROLE + * @since redis 2.8.12 + * @return 返回当前是master,slave还是sentinel + */ + @SuppressWarnings("unchecked") + public String getRole() { + CustomCommandHandler commandHandler = RedisUtil.getCustomCommandHandler(this.dbIndex); + List list = (List) commandHandler.executeCommand("ROLE", null); + return commandHandler.deserialize((byte[]) list.get(0)); + } + + /** + * 转为从服务器 + * @see Redis Documentation: SLAVEOF + * @since redis 1.0.0 + * @param ip 主服务器IP + * @param port 主服务器端口 + */ + public void slaveOf(String ip, int port) { + this.redisTemplate.slaveOf(ip, port); + } + + /** + * 转为主服务器 + * @see Redis Documentation: SLAVEOF NO ONE + * @since redis 1.0.0 + */ + public void slaveOfNoOne() { + this.redisTemplate.slaveOfNoOne(); + } + /** * 标记一个对象事务块的开始 * @since redis 1.2.0 @@ -330,6 +343,7 @@ public final class DBHandler implements RedisHandler { * 提交所有对象事务块内的命令 * @since redis 1.2.0 * @see Redis Documentation: EXEC + * @return 返回执行命令列表 */ public List commitAsObj() { return this.redisTemplate.exec(); @@ -339,6 +353,7 @@ public final class DBHandler implements RedisHandler { * 提交所有字符串事务块内的命令 * @since redis 1.2.0 * @see Redis Documentation: EXEC + * @return 返回执行命令列表 */ public List commit() { return this.stringRedisTemplate.exec(); @@ -438,36 +453,44 @@ public final class DBHandler implements RedisHandler { } /** - * 角色的信息 - * @see Redis Documentation: ROLE - * @since redis 2.8.12 - * @return 返回当前是master,slave还是sentinel + * 检查连接 + * @see Redis Documentation: PING + * @since redis 1.0.0 + * @return 返回延迟时间(ms) */ - @SuppressWarnings("unchecked") - public String getRole() { - CustomCommandHandler commandHandler = RedisUtil.getCustomCommandHandler(this.dbIndex); - List list = (List) commandHandler.executeCommand("ROLE", null); - return commandHandler.deserialize((byte[]) list.get(0)); + public Long ping() { + RedisConnection connection = this.redisTemplate.getRequiredConnectionFactory().getConnection(); + long begin = CLOCK.millis(); + connection.ping(); + long end = CLOCK.millis(); + return end - begin; } /** - * 转为从服务器 - * @see Redis Documentation: SLAVEOF - * @since redis 1.0.0 - * @param ip 主服务器IP - * @param port 主服务器端口 + * 服务器时间 + * @see Redis Documentation: TIME + * @since redis 2.6.0 + * @return 返回服务器时间(ms) */ - public void slaveOf(String ip, int port) { - this.redisTemplate.slaveOf(ip, port); + public Long time() { + return this.redisTemplate.getRequiredConnectionFactory().getConnection().serverCommands().time(); } /** - * 转为主服务器 - * @see Redis Documentation: SLAVEOF NO ONE + * 测试打印 + * @see Redis Documentation: ECHO * @since redis 1.0.0 + * @param message 信息 + * @return 返回打印信息 */ - public void slaveOfNoOne() { - this.redisTemplate.slaveOfNoOne(); + public String echo(String message) { + RedisSerializer serializer = RedisSerializer.string(); + return serializer.deserialize( + this.redisTemplate + .getRequiredConnectionFactory() + .getConnection() + .echo(serializer.serialize(message)) + ); } /** -- Gitee From 35e1b55d71ebd2bc3cb6efb48bd6b285c21dd73e Mon Sep 17 00:00:00 2001 From: xsx Date: Sat, 15 Jun 2019 12:55:12 +0800 Subject: [PATCH 11/13] optimization code --- .../wiki/xsx/core/handler/CommonHandler.java | 314 ------------------ .../java/wiki/xsx/core/handler/DBHandler.java | 115 +------ .../wiki/xsx/core/handler/HandlerManager.java | 123 ++++--- .../xsx/core/handler/HandlerManagerProxy.java | 53 +++ .../wiki/xsx/core/handler/HashHandler.java | 5 +- .../java/wiki/xsx/core/util/RedisUtil.java | 74 ++--- 6 files changed, 193 insertions(+), 491 deletions(-) delete mode 100644 src/main/java/wiki/xsx/core/handler/CommonHandler.java create mode 100644 src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java diff --git a/src/main/java/wiki/xsx/core/handler/CommonHandler.java b/src/main/java/wiki/xsx/core/handler/CommonHandler.java deleted file mode 100644 index f645909..0000000 --- a/src/main/java/wiki/xsx/core/handler/CommonHandler.java +++ /dev/null @@ -1,314 +0,0 @@ -package wiki.xsx.core.handler; - -import org.springframework.boot.autoconfigure.data.redis.RedisProperties; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.StringRedisTemplate; -import wiki.xsx.core.util.ApplicationContextUtil; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.concurrent.ConcurrentMap; - -/** - * 通用助手 - * @author xsx - * @date 2019/4/25 - * @since 1.8 - */ -public class CommonHandler { - /** - * 助手管理实例 - */ - private static final HandlerManager MANAGER = HandlerManager.getManager(); - /** - * 通用助手实例 - */ - private static final CommonHandler HANDLER = new CommonHandler(); - - /** - * 通用助手构造 - */ - private CommonHandler() { - int dbIndex = getInitDBIndex(); - initDBHandler(dbIndex); - initKeyHandler(dbIndex); - initNumberHandler(dbIndex); - initStringHandler(dbIndex); - initListHandler(dbIndex); - initHashHandler(dbIndex); - initSetHandler(dbIndex); - initZsetHandler(dbIndex); - initBitmapHandler(dbIndex); - initGeoHandler(dbIndex); - initHyperLogLogHandler(dbIndex); - initScriptHandler(dbIndex); - initPubSubHandler(dbIndex); - initSentinelHandler(dbIndex); - initCustomCommandHandler(dbIndex); - } - - /** - * 获取助手实例 - * @return 返回助手实例 - */ - public static CommonHandler getInstance() { - return HANDLER; - } - - /** - * 获取默认KEY - * @return 返回默认KEY - */ - public String getDefaultKey() { - return MANAGER.getDefaultKey(); - } - - /** - * 获取助手 - * @param key KEY - * @param type 助手类型 - * @return 返回助手 - */ - public RedisHandler getHandler(String key, HandlerType type) { - // 若是集群助手类型,则直接返回 - if (type==HandlerType.CLUSTER) { - return MANAGER.getClusterHandler(); - } - ConcurrentMap map = MANAGER.getHandlerMap(type); - RedisHandler handler = map.get(key); - if (handler!=null) { - return handler; - } - synchronized (MANAGER) { - handler = map.get(key); - if (handler==null) { - RedisHandler instance = this.getHandlerInstance(key, type); - handler = map.putIfAbsent(key, instance); - if (handler==null) { - handler = instance; - } - } - } - return handler; - } - - /** - * 获取默认对象模板 - * @return 返回对象模板 - */ - public RedisTemplate getDefaultRedisTemplate() { - return HandlerManager.getDefaultRedisTemplate(); - } - - /** - * 获取默认字符串模板 - * @return 返回字符串模板 - */ - public StringRedisTemplate getDefaultStringRedisTemplate() { - return HandlerManager.getDefaultStringRedisTemplate(); - } - - /** - * 获取初始化数据库索引 - * @return 返回数据库索引 - */ - private int getInitDBIndex() { - return ApplicationContextUtil.getContext().getBean(RedisProperties.class).getDatabase(); - } - - /** - * 初始化数据库助手 - * @param dbIndex 数据库索引 - */ - private void initDBHandler(int dbIndex) { - ConcurrentMap dbMap = MANAGER.getHandlerMap(HandlerType.DB); - DBHandler handler = new DBHandler(dbIndex); - dbMap.put(getDefaultKey(), handler); - dbMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化键助手 - * @param dbIndex 数据库索引 - */ - private void initKeyHandler(int dbIndex) { - ConcurrentMap keyMap = MANAGER.getHandlerMap(HandlerType.KEY); - KeyHandler handler = new KeyHandler(dbIndex); - keyMap.put(getDefaultKey(), handler); - keyMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化数字助手 - * @param dbIndex 数据库索引 - */ - private void initNumberHandler(int dbIndex) { - ConcurrentMap numberMap = MANAGER.getHandlerMap(HandlerType.NUMBER); - NumberHandler handler = new NumberHandler(dbIndex); - numberMap.put(getDefaultKey(), handler); - numberMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化字符串助手 - * @param dbIndex 数据库索引 - */ - private void initStringHandler(int dbIndex) { - ConcurrentMap stringMap = MANAGER.getHandlerMap(HandlerType.STRING); - StringHandler handler = new StringHandler(dbIndex); - stringMap.put(getDefaultKey(), handler); - stringMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化列表助手 - * @param dbIndex 数据库索引 - */ - private void initListHandler(int dbIndex) { - ConcurrentMap listMap = MANAGER.getHandlerMap(HandlerType.LIST); - ListHandler handler = new ListHandler(dbIndex); - listMap.put(getDefaultKey(), handler); - listMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化哈希助手 - * @param dbIndex 数据库索引 - */ - private void initHashHandler(int dbIndex) { - ConcurrentMap hashMap = MANAGER.getHandlerMap(HandlerType.HASH); - HashHandler handler = new HashHandler(dbIndex); - hashMap.put(getDefaultKey(), handler); - hashMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化无序集合助手 - * @param dbIndex 数据库索引 - */ - private void initSetHandler(int dbIndex) { - ConcurrentMap setMap = MANAGER.getHandlerMap(HandlerType.SET); - SetHandler handler = new SetHandler(dbIndex); - setMap.put(getDefaultKey(), handler); - setMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化有序集合助手 - * @param dbIndex 数据库索引 - */ - private void initZsetHandler(int dbIndex) { - ConcurrentMap zsetMap = MANAGER.getHandlerMap(HandlerType.ZSET); - ZsetHandler handler = new ZsetHandler(dbIndex); - zsetMap.put(getDefaultKey(), handler); - zsetMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化位图助手 - * @param dbIndex 数据库索引 - */ - private void initBitmapHandler(int dbIndex) { - ConcurrentMap bitmapMap = MANAGER.getHandlerMap(HandlerType.BITMAP); - BitmapHandler handler = new BitmapHandler(dbIndex); - bitmapMap.put(getDefaultKey(), handler); - bitmapMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化地理位置助手 - * @param dbIndex 数据库索引 - */ - private void initGeoHandler(int dbIndex) { - ConcurrentMap geoMap = MANAGER.getHandlerMap(HandlerType.GEO); - GeoHandler handler = new GeoHandler(dbIndex); - geoMap.put(getDefaultKey(), handler); - geoMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化基数助手 - * @param dbIndex 数据库索引 - */ - private void initHyperLogLogHandler(int dbIndex) { - ConcurrentMap hyperLogLogMap = MANAGER.getHandlerMap(HandlerType.HYPERLOGLOG); - HyperLogLogHandler handler = new HyperLogLogHandler(dbIndex); - hyperLogLogMap.put(getDefaultKey(), handler); - hyperLogLogMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化lua脚本助手 - * @param dbIndex 数据库索引 - */ - private void initScriptHandler(int dbIndex) { - ConcurrentMap scriptMap = MANAGER.getHandlerMap(HandlerType.SCRIPT); - ScriptHandler handler = new ScriptHandler(dbIndex); - scriptMap.put(getDefaultKey(), handler); - scriptMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化发布订阅助手 - * @param dbIndex 数据库索引 - */ - private void initPubSubHandler(int dbIndex) { - ConcurrentMap pubSubMap = MANAGER.getHandlerMap(HandlerType.PUBSUB); - PubSubHandler handler = new PubSubHandler(dbIndex); - pubSubMap.put(getDefaultKey(), handler); - pubSubMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化分布式锁助手 - * @param dbIndex 数据库索引 - */ - private void initRedisLockHandler(int dbIndex) { - ConcurrentMap lockMap = MANAGER.getHandlerMap(HandlerType.REDISLOCK); - RedisLockHandler handler = new RedisLockHandler(dbIndex); - lockMap.put(getDefaultKey(), handler); - lockMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化哨兵助手 - * @param dbIndex 数据库索引 - */ - private void initSentinelHandler(int dbIndex) { - ConcurrentMap sentinelMap = MANAGER.getHandlerMap(HandlerType.SENTINEL); - SentinelHandler handler = new SentinelHandler(dbIndex); - sentinelMap.put(getDefaultKey(), handler); - sentinelMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 初始化自定义命令助手 - * @param dbIndex 数据库索引 - */ - private void initCustomCommandHandler(int dbIndex) { - ConcurrentMap customCommandMap = MANAGER.getHandlerMap(HandlerType.CUSTOMCOMMAND); - CustomCommandHandler handler = new CustomCommandHandler(dbIndex); - customCommandMap.put(getDefaultKey(), handler); - customCommandMap.put(String.valueOf(dbIndex), handler); - } - - /** - * 获取助手实例 - * @param key KEY - * @param type 助手类型 - * @return 返回实例 - */ - @SuppressWarnings("unchecked") - private RedisHandler getHandlerInstance(String key, HandlerType type) { - Class clz = type.getTypeClass(); - try { - Constructor constructor = clz.getDeclaredConstructor(Integer.class); - constructor.setAccessible(true); - return (RedisHandler) constructor.newInstance(Integer.valueOf(key)); - } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { - e.printStackTrace(); - } - return null; - } - -} diff --git a/src/main/java/wiki/xsx/core/handler/DBHandler.java b/src/main/java/wiki/xsx/core/handler/DBHandler.java index 457d156..dac333f 100644 --- a/src/main/java/wiki/xsx/core/handler/DBHandler.java +++ b/src/main/java/wiki/xsx/core/handler/DBHandler.java @@ -5,7 +5,6 @@ import org.slf4j.LoggerFactory; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.types.RedisClientInfo; import org.springframework.data.redis.serializer.RedisSerializer; import wiki.xsx.core.util.RedisUtil; @@ -101,10 +100,6 @@ public final class DBHandler implements RedisHandler { * 对象模板 */ private RedisTemplate redisTemplate; - /** - * 字符串模板 - */ - private StringRedisTemplate stringRedisTemplate; /** * 数据库索引 */ @@ -117,9 +112,7 @@ public final class DBHandler implements RedisHandler { @SuppressWarnings("unchecked") DBHandler(Integer dbIndex) { this.dbIndex = dbIndex; - List templateList = HandlerManager.createTemplate(dbIndex); - this.redisTemplate = templateList.get(0); - this.stringRedisTemplate = (StringRedisTemplate) templateList.get(1); + this.redisTemplate = HandlerManager.createRedisTemplate(dbIndex); } /** @@ -226,49 +219,26 @@ public final class DBHandler implements RedisHandler { } /** - * 设置对象客户端连接名称 - * @see Redis Documentation: CLIENT SETNAME - * @since redis 2.6.9 - * @param name 名称 - */ - public void setClientNameAsObj(String name) { - this.redisTemplate - .getRequiredConnectionFactory() - .getConnection() - .setClientName(RedisSerializer.string().serialize(name)); - } - - /** - * 设置字符串客户端连接名称 + * 设置客户端连接名称 * @see Redis Documentation: CLIENT SETNAME * @since redis 2.6.9 * @param name 名称 */ public void setClientName(String name) { - this.stringRedisTemplate + this.redisTemplate .getRequiredConnectionFactory() .getConnection() .setClientName(RedisSerializer.string().serialize(name)); } /** - * 获取对象客户端连接名称 - * @see Redis Documentation: CLIENT GETNAME - * @since redis 2.6.9 - * @return 返回客户端连接名称 - */ - public String getClientNameAsObj() { - return this.redisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); - } - - /** - * 获取字符串客户端连接名称 + * 获取客户端连接名称 * @see Redis Documentation: CLIENT GETNAME * @since redis 2.6.9 * @return 返回客户端连接名称 */ public String getClientName() { - return this.stringRedisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); + return this.redisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); } /** @@ -322,97 +292,50 @@ public final class DBHandler implements RedisHandler { } /** - * 标记一个对象事务块的开始 - * @since redis 1.2.0 - * @see Redis Documentation: MULTI - */ - public void beginTransactionAsObj() { - this.redisTemplate.multi(); - } - - /** - * 标记一个字符串事务块的开始 + * 开始事务 * @since redis 1.2.0 * @see Redis Documentation: MULTI */ public void beginTransaction() { - this.stringRedisTemplate.multi(); - } - - /** - * 提交所有对象事务块内的命令 - * @since redis 1.2.0 - * @see Redis Documentation: EXEC - * @return 返回执行命令列表 - */ - public List commitAsObj() { - return this.redisTemplate.exec(); + this.redisTemplate.multi(); } /** - * 提交所有字符串事务块内的命令 + * 提交事务 * @since redis 1.2.0 * @see Redis Documentation: EXEC * @return 返回执行命令列表 */ public List commit() { - return this.stringRedisTemplate.exec(); - } - - /** - * 取消对象事务 - * @see Redis Documentation: DISCARD - * @since redis 2.0.0 - */ - public void cancelTransactionAsObj() { - this.redisTemplate.discard(); + return this.redisTemplate.exec(); } /** - * 取消字符串事务 + * 取消事务 * @see Redis Documentation: DISCARD * @since redis 2.0.0 */ public void cancelTransaction() { - this.stringRedisTemplate.discard(); - } - - /** - * 监控对象 - * @see Redis Documentation: WATCH - * @since redis 2.2.0 - * @param keys 键 - */ - public void watchAsObj(String ...keys) { - this.redisTemplate.watch(Arrays.asList(keys)); + this.redisTemplate.discard(); } /** - * 监控字符串 + * 监控 * @see Redis Documentation: WATCH * @since redis 2.2.0 * @param keys 键 */ public void watch(String ...keys) { - this.stringRedisTemplate.watch(Arrays.asList(keys)); - } - - /** - * 取消监控对象 - * @see Redis Documentation: UNWATCH - * @since redis 2.2.0 - */ - public void unwatchAsObj() { - this.redisTemplate.unwatch(); + this.redisTemplate.watch(Arrays.asList(keys)); } /** - * 取消监控字符串 + * 取消监控 * @see Redis Documentation: UNWATCH * @since redis 2.2.0 */ public void unwatch() { - this.stringRedisTemplate.unwatch(); + this.redisTemplate.unwatch(); } /** @@ -500,12 +423,4 @@ public final class DBHandler implements RedisHandler { public RedisTemplate getRedisTemplate() { return this.redisTemplate; } - - /** - * 获取spring string redis模板 - * @return 返回字符串模板 - */ - public StringRedisTemplate getStringRedisTemplate() { - return this.stringRedisTemplate; - } } diff --git a/src/main/java/wiki/xsx/core/handler/HandlerManager.java b/src/main/java/wiki/xsx/core/handler/HandlerManager.java index a47d963..5628f1a 100644 --- a/src/main/java/wiki/xsx/core/handler/HandlerManager.java +++ b/src/main/java/wiki/xsx/core/handler/HandlerManager.java @@ -1,5 +1,6 @@ package wiki.xsx.core.handler; +import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; @@ -11,8 +12,11 @@ import wiki.xsx.core.config.redisson.RedissonClientHelper; import wiki.xsx.core.config.redisson.RedissonConnectionFactory; import wiki.xsx.core.util.ApplicationContextUtil; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -22,7 +26,7 @@ import java.util.concurrent.ConcurrentMap; * @date 2019/5/23 * @since 1.8 */ -class HandlerManager { +final class HandlerManager { /** * redis模板(用于对象) */ @@ -32,14 +36,14 @@ class HandlerManager { * redis模板(用于字符串) */ private static final StringRedisTemplate STRING_REDIS_TEMPLATE = ApplicationContextUtil.getContext().getBean("stringRedisTemplate", StringRedisTemplate.class); - /** - * 助手管理实例 - */ - private static final HandlerManager INSTANCE = new HandlerManager(); /** * 默认KEY */ private static final String DEFAULT_KEY = "default"; + /** + * 默认数据库索引 + */ + private static final int DEFAULT_DB_INDEX = ApplicationContextUtil.getContext().getBean(RedisProperties.class).getDatabase(); /** * 助手容器 */ @@ -53,15 +57,7 @@ class HandlerManager { /** * 助手管理构造 */ - private HandlerManager(){} - - /** - * 获取助手管理 - * @return 返回助手管理实例 - */ - static HandlerManager getManager() { - return INSTANCE; - } + HandlerManager(){} /** * 获取默认KEY @@ -72,19 +68,48 @@ class HandlerManager { } /** - * 获取助手字典 - * @return 返回助手字典 + * 获取助手 + * @param key KEY + * @param type 助手类型 + * @return 返回助手 */ - ConcurrentMap getHandlerMap(HandlerType type) { - return this.container.get(type); + RedisHandler getHandler(String key, HandlerType type) { + // 若是集群助手类型,则直接返回 + if (type==HandlerType.CLUSTER) { + return clusterHandler; + } + ConcurrentMap map = this.container.get(type); + RedisHandler handler = map.get(key); + if (handler!=null) { + return handler; + } + synchronized (this.container) { + handler = map.get(key); + if (handler==null) { + RedisHandler instance = this.getHandlerInstance(key, type); + handler = map.putIfAbsent(key, instance); + if (handler==null) { + handler = instance; + } + } + } + return handler; } /** - * 获取集群助手实例 - * @return 返回集群助手实例 + * 获取默认的对象模板 + * @return 返回默认的对象模板 */ - ClusterHandler getClusterHandler() { - return this.clusterHandler; + RedisTemplate getDefaultRedisTemplate() { + return REDIS_TEMPLATE; + } + + /** + * 获取默认的字符串模板 + * @return 返回默认的字符串模板 + */ + StringRedisTemplate getDefaultStringRedisTemplate() { + return STRING_REDIS_TEMPLATE; } /** @@ -184,32 +209,52 @@ class HandlerManager { return redisTemplate; } - /** - * 获取默认的对象模板 - * @return 返回默认的对象模板 - */ - static RedisTemplate getDefaultRedisTemplate() { - return REDIS_TEMPLATE; - } - - /** - * 获取默认的字符串模板 - * @return 返回默认的字符串模板 - */ - static StringRedisTemplate getDefaultStringRedisTemplate() { - return STRING_REDIS_TEMPLATE; - } - /** * 初始化容器 * @return 返回容器 */ private ConcurrentMap> initContainer() { + String dbIndex = String.valueOf(DEFAULT_DB_INDEX); ConcurrentMap> container = new ConcurrentHashMap<>(32); HandlerType[] types = HandlerType.values(); + RedisHandler handler; for (HandlerType type : types) { - container.put(type, new ConcurrentHashMap<>(256)); + ConcurrentHashMap handlerMap = new ConcurrentHashMap<>(256); + container.put(type, handlerMap); + // 初始化跳过redLock + if (type==HandlerType.REDISLOCK) { + continue; + } + handler = this.getHandlerInstance(dbIndex, type); + handlerMap.put(getDefaultKey(), Objects.requireNonNull(handler)); + handlerMap.put(dbIndex, handler); + } return container; } + + /** + * 获取助手实例 + * @param key KEY + * @param type 助手类型 + * @return 返回实例 + */ + @SuppressWarnings("unchecked") + private RedisHandler getHandlerInstance(String key, HandlerType type) { + Class clz = type.getTypeClass(); + try { + Constructor constructor = clz.getDeclaredConstructor(Integer.class); + constructor.setAccessible(true); + RedisHandler handler; + if (type==HandlerType.REDISLOCK&&DEFAULT_KEY.equalsIgnoreCase(key)) { + handler = (RedisHandler) constructor.newInstance(DEFAULT_DB_INDEX); + }else { + handler = (RedisHandler) constructor.newInstance(Integer.valueOf(key)); + } + return handler; + } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java b/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java new file mode 100644 index 0000000..1adc8f7 --- /dev/null +++ b/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java @@ -0,0 +1,53 @@ +package wiki.xsx.core.handler; + +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; + +/** + * 助手管理代理 + * @author xsx + * @date 2019/6/15 + * @since 1.8 + */ +public class HandlerManagerProxy { + + /** + * 助手管理实例 + */ + private final HandlerManager manager = new HandlerManager(); + + /** + * 获取助手 + * @param type 助手类型 + * @return 返回助手 + */ + public RedisHandler getHandler(HandlerType type) { + return this.manager.getHandler(this.manager.getDefaultKey(), type); + } + + /** + * 获取助手 + * @param key KEY + * @param type 助手类型 + * @return 返回助手 + */ + public RedisHandler getHandler(String key, HandlerType type) { + return this.manager.getHandler(key, type); + } + + /** + * 获取默认的对象模板 + * @return 返回默认的对象模板 + */ + public RedisTemplate getDefaultRedisTemplate() { + return this.manager.getDefaultRedisTemplate(); + } + + /** + * 获取默认的字符串模板 + * @return 返回默认的字符串模板 + */ + public StringRedisTemplate getDefaultStringRedisTemplate() { + return this.manager.getDefaultStringRedisTemplate(); + } +} diff --git a/src/main/java/wiki/xsx/core/handler/HashHandler.java b/src/main/java/wiki/xsx/core/handler/HashHandler.java index d1d0320..73fbf04 100644 --- a/src/main/java/wiki/xsx/core/handler/HashHandler.java +++ b/src/main/java/wiki/xsx/core/handler/HashHandler.java @@ -4,7 +4,10 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.springframework.data.redis.core.*; -import java.util.*; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * 哈希助手 diff --git a/src/main/java/wiki/xsx/core/util/RedisUtil.java b/src/main/java/wiki/xsx/core/util/RedisUtil.java index e470940..325d354 100644 --- a/src/main/java/wiki/xsx/core/util/RedisUtil.java +++ b/src/main/java/wiki/xsx/core/util/RedisUtil.java @@ -13,16 +13,16 @@ import wiki.xsx.core.handler.*; public class RedisUtil { /** - * 通用助手实例 + * 助手管理代理实例 */ - private static final CommonHandler COMMON_HANDLER = CommonHandler.getInstance(); + private static final HandlerManagerProxy MANAGER = new HandlerManagerProxy(); /** * 获取数据库助手 * @return 返回数据库助手 */ public static DBHandler getDBHandler() { - return (DBHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.DB); + return (DBHandler) MANAGER.getHandler(HandlerType.DB); } /** @@ -31,7 +31,7 @@ public class RedisUtil { * @return 返回数据库助手 */ public static DBHandler getDBHandler(int dbIndex) { - return (DBHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.DB); + return (DBHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.DB); } /** @@ -39,7 +39,7 @@ public class RedisUtil { * @return 返回键助手 */ public static KeyHandler getKeyHandler() { - return (KeyHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.KEY); + return (KeyHandler) MANAGER.getHandler(HandlerType.KEY); } /** @@ -48,7 +48,7 @@ public class RedisUtil { * @return 返回键助手 */ public static KeyHandler getKeyHandler(int dbIndex) { - return (KeyHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.KEY); + return (KeyHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.KEY); } /** @@ -56,7 +56,7 @@ public class RedisUtil { * @return 返回数字助手 */ public static NumberHandler getNumberHandler() { - return (NumberHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.NUMBER); + return (NumberHandler) MANAGER.getHandler(HandlerType.NUMBER); } /** @@ -65,7 +65,7 @@ public class RedisUtil { * @return 返回数字助手 */ public static NumberHandler getNumberHandler(int dbIndex) { - return (NumberHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.NUMBER); + return (NumberHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.NUMBER); } /** @@ -73,7 +73,7 @@ public class RedisUtil { * @return 返回字符串助手 */ public static StringHandler getStringHandler() { - return (StringHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.STRING); + return (StringHandler) MANAGER.getHandler(HandlerType.STRING); } /** @@ -82,7 +82,7 @@ public class RedisUtil { * @return 返回字符串助手 */ public static StringHandler getStringHandler(int dbIndex) { - return (StringHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.STRING); + return (StringHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.STRING); } /** @@ -90,7 +90,7 @@ public class RedisUtil { * @return 返回哈希助手 */ public static HashHandler getHashHandler() { - return (HashHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.HASH); + return (HashHandler) MANAGER.getHandler(HandlerType.HASH); } /** @@ -99,7 +99,7 @@ public class RedisUtil { * @return 返回哈希助手 */ public static HashHandler getHashHandler(int dbIndex) { - return (HashHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.HASH); + return (HashHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.HASH); } /** @@ -107,7 +107,7 @@ public class RedisUtil { * @return 返回列表助手 */ public static ListHandler getListHandler() { - return (ListHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.LIST); + return (ListHandler) MANAGER.getHandler(HandlerType.LIST); } /** @@ -116,7 +116,7 @@ public class RedisUtil { * @return 返回列表助手 */ public static ListHandler getListHandler(int dbIndex) { - return (ListHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.LIST); + return (ListHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.LIST); } /** @@ -124,7 +124,7 @@ public class RedisUtil { * @return 返回无序集合助手 */ public static SetHandler getSetHandler() { - return (SetHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.SET); + return (SetHandler) MANAGER.getHandler(HandlerType.SET); } /** @@ -133,7 +133,7 @@ public class RedisUtil { * @return 返回无序集合助手 */ public static SetHandler getSetHandler(int dbIndex) { - return (SetHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.SET); + return (SetHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SET); } /** @@ -141,7 +141,7 @@ public class RedisUtil { * @return 返回有序集合助手 */ public static ZsetHandler getZsetHandler() { - return (ZsetHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.ZSET); + return (ZsetHandler) MANAGER.getHandler(HandlerType.ZSET); } /** @@ -150,7 +150,7 @@ public class RedisUtil { * @return 返回有序集合助手 */ public static ZsetHandler getZsetHandler(int dbIndex) { - return (ZsetHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.ZSET); + return (ZsetHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.ZSET); } /** @@ -158,7 +158,7 @@ public class RedisUtil { * @return 返回基数助手 */ public static HyperLogLogHandler getHyperLogLogHandler() { - return (HyperLogLogHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.HYPERLOGLOG); + return (HyperLogLogHandler) MANAGER.getHandler(HandlerType.HYPERLOGLOG); } /** @@ -167,7 +167,7 @@ public class RedisUtil { * @return 返回基数助手 */ public static HyperLogLogHandler getHyperLogLogHandler(int dbIndex) { - return (HyperLogLogHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.HYPERLOGLOG); + return (HyperLogLogHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.HYPERLOGLOG); } /** @@ -175,7 +175,7 @@ public class RedisUtil { * @return 返回位图助手 */ public static BitmapHandler getBitmapHandler() { - return (BitmapHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.BITMAP); + return (BitmapHandler) MANAGER.getHandler(HandlerType.BITMAP); } /** @@ -184,7 +184,7 @@ public class RedisUtil { * @return 返回位图助手 */ public static BitmapHandler getBitmapHandler(int dbIndex) { - return (BitmapHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.BITMAP); + return (BitmapHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.BITMAP); } /** @@ -192,7 +192,7 @@ public class RedisUtil { * @return 返回地理位置助手 */ public static GeoHandler getGeoHandler() { - return (GeoHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.GEO); + return (GeoHandler) MANAGER.getHandler(HandlerType.GEO); } /** @@ -201,7 +201,7 @@ public class RedisUtil { * @return 返回地理位置助手 */ public static GeoHandler getGeoHandler(int dbIndex) { - return (GeoHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.GEO); + return (GeoHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.GEO); } /** @@ -209,7 +209,7 @@ public class RedisUtil { * @return 返回lua脚本助手 */ public static ScriptHandler getScriptHandler() { - return (ScriptHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.SCRIPT); + return (ScriptHandler) MANAGER.getHandler(HandlerType.SCRIPT); } /** @@ -218,7 +218,7 @@ public class RedisUtil { * @return 返回lua脚本助手 */ public static ScriptHandler getScriptHandler(int dbIndex) { - return (ScriptHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.SCRIPT); + return (ScriptHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SCRIPT); } /** @@ -226,7 +226,7 @@ public class RedisUtil { * @return 返回发布订阅助手 */ public static PubSubHandler getPubSubHandler() { - return (PubSubHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.PUBSUB); + return (PubSubHandler) MANAGER.getHandler(HandlerType.PUBSUB); } /** @@ -235,7 +235,7 @@ public class RedisUtil { * @return 返回发布订阅助手 */ public static PubSubHandler getPubSubHandler(int dbIndex) { - return (PubSubHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.PUBSUB); + return (PubSubHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.PUBSUB); } /** @@ -243,7 +243,7 @@ public class RedisUtil { * @return 返回分布式锁助手 */ public static RedisLockHandler getRedisLockHandler() { - return (RedisLockHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.REDISLOCK); + return (RedisLockHandler) MANAGER.getHandler(HandlerType.REDISLOCK); } /** @@ -252,7 +252,7 @@ public class RedisUtil { * @return 返回分布式锁助手 */ public static RedisLockHandler getRedisLockHandler(int dbIndex) { - return (RedisLockHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.REDISLOCK); + return (RedisLockHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.REDISLOCK); } /** @@ -260,7 +260,7 @@ public class RedisUtil { * @return 返回哨兵助手 */ public static SentinelHandler getSentinelHandler() { - return (SentinelHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.SENTINEL); + return (SentinelHandler) MANAGER.getHandler(HandlerType.SENTINEL); } /** @@ -269,7 +269,7 @@ public class RedisUtil { * @return 返回哨兵助手 */ public static SentinelHandler getSentinelHandler(int dbIndex) { - return (SentinelHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.SENTINEL); + return (SentinelHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SENTINEL); } /** @@ -277,7 +277,7 @@ public class RedisUtil { * @return 返回集群助手 */ public static ClusterHandler getClusterHandler() { - return (ClusterHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.CLUSTER); + return (ClusterHandler) MANAGER.getHandler(HandlerType.CLUSTER); } /** @@ -285,7 +285,7 @@ public class RedisUtil { * @return 返回自定义命令助手 */ public static CustomCommandHandler getCustomCommandHandler() { - return (CustomCommandHandler) COMMON_HANDLER.getHandler(COMMON_HANDLER.getDefaultKey(), HandlerType.CUSTOMCOMMAND); + return (CustomCommandHandler) MANAGER.getHandler(HandlerType.CUSTOMCOMMAND); } /** @@ -294,7 +294,7 @@ public class RedisUtil { * @return 返回自定义命令助手 */ public static CustomCommandHandler getCustomCommandHandler(int dbIndex) { - return (CustomCommandHandler) COMMON_HANDLER.getHandler(String.valueOf(dbIndex), HandlerType.CUSTOMCOMMAND); + return (CustomCommandHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.CUSTOMCOMMAND); } /** @@ -302,7 +302,7 @@ public class RedisUtil { * @return 返回对象模板 */ public static RedisTemplate getDefaultRedisTemplate() { - return COMMON_HANDLER.getDefaultRedisTemplate(); + return MANAGER.getDefaultRedisTemplate(); } /** @@ -310,6 +310,6 @@ public class RedisUtil { * @return 返回字符串模板 */ public static StringRedisTemplate getDefaultStringRedisTemplate() { - return COMMON_HANDLER.getDefaultStringRedisTemplate(); + return MANAGER.getDefaultStringRedisTemplate(); } } -- Gitee From ed0469cce1f41ef130d009967b9fa075a432e0f2 Mon Sep 17 00:00:00 2001 From: xsx Date: Sat, 15 Jun 2019 12:56:06 +0800 Subject: [PATCH 12/13] update version --- README.en.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.en.md b/README.en.md index 60a48ca..a416fd8 100644 --- a/README.en.md +++ b/README.en.md @@ -21,7 +21,7 @@ Dependency spring-boot-starter-data-redis wiki.xsx spring-boot-starter-fast-redis - 1.5.1 + 1.5.2 ``` #### Document diff --git a/README.md b/README.md index a3ac4ee..4a9bca0 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ wiki.xsx spring-boot-starter-fast-redis - 1.5.1 + 1.5.2 ``` -- Gitee From ec53b73a6d28b97c8c32d20296ec0aeb27c8007c Mon Sep 17 00:00:00 2001 From: xsx Date: Sat, 15 Jun 2019 13:22:03 +0800 Subject: [PATCH 13/13] optimization code --- .../xsx/core/handler/HandlerManagerProxy.java | 12 ++-- .../java/wiki/xsx/core/util/RedisUtil.java | 66 +++++++++---------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java b/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java index 1adc8f7..679a769 100644 --- a/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java +++ b/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java @@ -19,20 +19,24 @@ public class HandlerManagerProxy { /** * 获取助手 * @param type 助手类型 + * @param 返回类型 * @return 返回助手 */ - public RedisHandler getHandler(HandlerType type) { - return this.manager.getHandler(this.manager.getDefaultKey(), type); + @SuppressWarnings("unchecked") + public T getHandler(HandlerType type) { + return (T) this.manager.getHandler(this.manager.getDefaultKey(), type); } /** * 获取助手 * @param key KEY * @param type 助手类型 + * @param 返回类型 * @return 返回助手 */ - public RedisHandler getHandler(String key, HandlerType type) { - return this.manager.getHandler(key, type); + @SuppressWarnings("unchecked") + public T getHandler(String key, HandlerType type) { + return (T) this.manager.getHandler(key, type); } /** diff --git a/src/main/java/wiki/xsx/core/util/RedisUtil.java b/src/main/java/wiki/xsx/core/util/RedisUtil.java index 325d354..f810c5d 100644 --- a/src/main/java/wiki/xsx/core/util/RedisUtil.java +++ b/src/main/java/wiki/xsx/core/util/RedisUtil.java @@ -22,7 +22,7 @@ public class RedisUtil { * @return 返回数据库助手 */ public static DBHandler getDBHandler() { - return (DBHandler) MANAGER.getHandler(HandlerType.DB); + return MANAGER.getHandler(HandlerType.DB); } /** @@ -31,7 +31,7 @@ public class RedisUtil { * @return 返回数据库助手 */ public static DBHandler getDBHandler(int dbIndex) { - return (DBHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.DB); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.DB); } /** @@ -39,7 +39,7 @@ public class RedisUtil { * @return 返回键助手 */ public static KeyHandler getKeyHandler() { - return (KeyHandler) MANAGER.getHandler(HandlerType.KEY); + return MANAGER.getHandler(HandlerType.KEY); } /** @@ -48,7 +48,7 @@ public class RedisUtil { * @return 返回键助手 */ public static KeyHandler getKeyHandler(int dbIndex) { - return (KeyHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.KEY); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.KEY); } /** @@ -56,7 +56,7 @@ public class RedisUtil { * @return 返回数字助手 */ public static NumberHandler getNumberHandler() { - return (NumberHandler) MANAGER.getHandler(HandlerType.NUMBER); + return MANAGER.getHandler(HandlerType.NUMBER); } /** @@ -65,7 +65,7 @@ public class RedisUtil { * @return 返回数字助手 */ public static NumberHandler getNumberHandler(int dbIndex) { - return (NumberHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.NUMBER); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.NUMBER); } /** @@ -73,7 +73,7 @@ public class RedisUtil { * @return 返回字符串助手 */ public static StringHandler getStringHandler() { - return (StringHandler) MANAGER.getHandler(HandlerType.STRING); + return MANAGER.getHandler(HandlerType.STRING); } /** @@ -82,7 +82,7 @@ public class RedisUtil { * @return 返回字符串助手 */ public static StringHandler getStringHandler(int dbIndex) { - return (StringHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.STRING); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.STRING); } /** @@ -90,7 +90,7 @@ public class RedisUtil { * @return 返回哈希助手 */ public static HashHandler getHashHandler() { - return (HashHandler) MANAGER.getHandler(HandlerType.HASH); + return MANAGER.getHandler(HandlerType.HASH); } /** @@ -99,7 +99,7 @@ public class RedisUtil { * @return 返回哈希助手 */ public static HashHandler getHashHandler(int dbIndex) { - return (HashHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.HASH); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.HASH); } /** @@ -107,7 +107,7 @@ public class RedisUtil { * @return 返回列表助手 */ public static ListHandler getListHandler() { - return (ListHandler) MANAGER.getHandler(HandlerType.LIST); + return MANAGER.getHandler(HandlerType.LIST); } /** @@ -116,7 +116,7 @@ public class RedisUtil { * @return 返回列表助手 */ public static ListHandler getListHandler(int dbIndex) { - return (ListHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.LIST); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.LIST); } /** @@ -124,7 +124,7 @@ public class RedisUtil { * @return 返回无序集合助手 */ public static SetHandler getSetHandler() { - return (SetHandler) MANAGER.getHandler(HandlerType.SET); + return MANAGER.getHandler(HandlerType.SET); } /** @@ -133,7 +133,7 @@ public class RedisUtil { * @return 返回无序集合助手 */ public static SetHandler getSetHandler(int dbIndex) { - return (SetHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SET); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SET); } /** @@ -141,7 +141,7 @@ public class RedisUtil { * @return 返回有序集合助手 */ public static ZsetHandler getZsetHandler() { - return (ZsetHandler) MANAGER.getHandler(HandlerType.ZSET); + return MANAGER.getHandler(HandlerType.ZSET); } /** @@ -150,7 +150,7 @@ public class RedisUtil { * @return 返回有序集合助手 */ public static ZsetHandler getZsetHandler(int dbIndex) { - return (ZsetHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.ZSET); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.ZSET); } /** @@ -158,7 +158,7 @@ public class RedisUtil { * @return 返回基数助手 */ public static HyperLogLogHandler getHyperLogLogHandler() { - return (HyperLogLogHandler) MANAGER.getHandler(HandlerType.HYPERLOGLOG); + return MANAGER.getHandler(HandlerType.HYPERLOGLOG); } /** @@ -167,7 +167,7 @@ public class RedisUtil { * @return 返回基数助手 */ public static HyperLogLogHandler getHyperLogLogHandler(int dbIndex) { - return (HyperLogLogHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.HYPERLOGLOG); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.HYPERLOGLOG); } /** @@ -175,7 +175,7 @@ public class RedisUtil { * @return 返回位图助手 */ public static BitmapHandler getBitmapHandler() { - return (BitmapHandler) MANAGER.getHandler(HandlerType.BITMAP); + return MANAGER.getHandler(HandlerType.BITMAP); } /** @@ -184,7 +184,7 @@ public class RedisUtil { * @return 返回位图助手 */ public static BitmapHandler getBitmapHandler(int dbIndex) { - return (BitmapHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.BITMAP); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.BITMAP); } /** @@ -192,7 +192,7 @@ public class RedisUtil { * @return 返回地理位置助手 */ public static GeoHandler getGeoHandler() { - return (GeoHandler) MANAGER.getHandler(HandlerType.GEO); + return MANAGER.getHandler(HandlerType.GEO); } /** @@ -201,7 +201,7 @@ public class RedisUtil { * @return 返回地理位置助手 */ public static GeoHandler getGeoHandler(int dbIndex) { - return (GeoHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.GEO); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.GEO); } /** @@ -209,7 +209,7 @@ public class RedisUtil { * @return 返回lua脚本助手 */ public static ScriptHandler getScriptHandler() { - return (ScriptHandler) MANAGER.getHandler(HandlerType.SCRIPT); + return MANAGER.getHandler(HandlerType.SCRIPT); } /** @@ -218,7 +218,7 @@ public class RedisUtil { * @return 返回lua脚本助手 */ public static ScriptHandler getScriptHandler(int dbIndex) { - return (ScriptHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SCRIPT); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SCRIPT); } /** @@ -226,7 +226,7 @@ public class RedisUtil { * @return 返回发布订阅助手 */ public static PubSubHandler getPubSubHandler() { - return (PubSubHandler) MANAGER.getHandler(HandlerType.PUBSUB); + return MANAGER.getHandler(HandlerType.PUBSUB); } /** @@ -235,7 +235,7 @@ public class RedisUtil { * @return 返回发布订阅助手 */ public static PubSubHandler getPubSubHandler(int dbIndex) { - return (PubSubHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.PUBSUB); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.PUBSUB); } /** @@ -243,7 +243,7 @@ public class RedisUtil { * @return 返回分布式锁助手 */ public static RedisLockHandler getRedisLockHandler() { - return (RedisLockHandler) MANAGER.getHandler(HandlerType.REDISLOCK); + return MANAGER.getHandler(HandlerType.REDISLOCK); } /** @@ -252,7 +252,7 @@ public class RedisUtil { * @return 返回分布式锁助手 */ public static RedisLockHandler getRedisLockHandler(int dbIndex) { - return (RedisLockHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.REDISLOCK); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.REDISLOCK); } /** @@ -260,7 +260,7 @@ public class RedisUtil { * @return 返回哨兵助手 */ public static SentinelHandler getSentinelHandler() { - return (SentinelHandler) MANAGER.getHandler(HandlerType.SENTINEL); + return MANAGER.getHandler(HandlerType.SENTINEL); } /** @@ -269,7 +269,7 @@ public class RedisUtil { * @return 返回哨兵助手 */ public static SentinelHandler getSentinelHandler(int dbIndex) { - return (SentinelHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SENTINEL); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.SENTINEL); } /** @@ -277,7 +277,7 @@ public class RedisUtil { * @return 返回集群助手 */ public static ClusterHandler getClusterHandler() { - return (ClusterHandler) MANAGER.getHandler(HandlerType.CLUSTER); + return MANAGER.getHandler(HandlerType.CLUSTER); } /** @@ -285,7 +285,7 @@ public class RedisUtil { * @return 返回自定义命令助手 */ public static CustomCommandHandler getCustomCommandHandler() { - return (CustomCommandHandler) MANAGER.getHandler(HandlerType.CUSTOMCOMMAND); + return MANAGER.getHandler(HandlerType.CUSTOMCOMMAND); } /** @@ -294,7 +294,7 @@ public class RedisUtil { * @return 返回自定义命令助手 */ public static CustomCommandHandler getCustomCommandHandler(int dbIndex) { - return (CustomCommandHandler) MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.CUSTOMCOMMAND); + return MANAGER.getHandler(String.valueOf(dbIndex), HandlerType.CUSTOMCOMMAND); } /** -- Gitee