diff --git a/README.en.md b/README.en.md index 4c7b5f6a2ed93b068298f6ed42979a65127f1e77..a416fd85ac4592e88ee56b893b7354f690b2e449 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 @@ -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 f5a3385c75e794f85c69ffbc36f1fdd59ce3ab35..4a9bca0c044c8c4f0915ae3fd1a703d765ebf233 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ wiki.xsx spring-boot-starter-fast-redis - 1.5.1 + 1.5.2 ``` @@ -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: diff --git a/pom.xml b/pom.xml index 005a2afa110a2ffb6dfee574347badf5e723e250..e8b3c534469ca1ff2ce4d52059668374a5c86866 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/CommonHandler.java b/src/main/java/wiki/xsx/core/handler/CommonHandler.java deleted file mode 100644 index f645909b989c06e2c9388322801dc2e740f82856..0000000000000000000000000000000000000000 --- 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 c738f0fde39258c578acd0ae96d8c2abe55b1dfa..dac333f647c3b69648c0778dcf0c2c2a6659437f 100644 --- a/src/main/java/wiki/xsx/core/handler/DBHandler.java +++ b/src/main/java/wiki/xsx/core/handler/DBHandler.java @@ -5,8 +5,9 @@ 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; import java.time.Clock; import java.util.Arrays; @@ -99,10 +100,6 @@ public final class DBHandler implements RedisHandler { * 对象模板 */ private RedisTemplate redisTemplate; - /** - * 字符串模板 - */ - private StringRedisTemplate stringRedisTemplate; /** * 数据库索引 */ @@ -115,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); } /** @@ -129,73 +124,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 @@ -233,6 +161,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 @@ -249,6 +203,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); @@ -264,139 +219,123 @@ public final class DBHandler implements RedisHandler { } /** - * 设置对象客户端连接名称 + * 设置客户端连接名称 * @see Redis Documentation: CLIENT SETNAME * @since redis 2.6.9 * @param name 名称 */ - public void setClientNameAsObj(String name) { + public void setClientName(String name) { this.redisTemplate - .getRequiredConnectionFactory() - .getConnection() - .setClientName(RedisSerializer.string().serialize(name)); + .getRequiredConnectionFactory() + .getConnection() + .setClientName(RedisSerializer.string().serialize(name)); } /** - * 设置字符串客户端连接名称 - * @see Redis Documentation: CLIENT SETNAME + * 获取客户端连接名称 + * @see Redis Documentation: CLIENT GETNAME * @since redis 2.6.9 - * @param name 名称 + * @return 返回客户端连接名称 */ - public void setClientName(String name) { - this.stringRedisTemplate - .getRequiredConnectionFactory() - .getConnection() - .setClientName(RedisSerializer.string().serialize(name)); + public String getClientName() { + return this.redisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); } /** - * 获取对象客户端连接名称 - * @see Redis Documentation: CLIENT GETNAME - * @since redis 2.6.9 + * 获取客户端连接列表 + * @return 返回客户端连接列表 */ - public String getClientNameAsObj() { - return this.redisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); + public List getClientList() { + return this.redisTemplate.getRequiredConnectionFactory().getConnection().getClientList(); } /** - * 获取字符串客户端连接名称 - * @see Redis Documentation: CLIENT GETNAME - * @since redis 2.6.9 + * 关闭客户端 + * @param ip 客户端IP + * @param port 客户端端口 */ - public String getClientName() { - return this.stringRedisTemplate.getRequiredConnectionFactory().getConnection().getClientName(); + public void killClient(String ip, int port) { + this.redisTemplate.killClient(ip, port); } /** - * 标记一个对象事务块的开始 - * @since redis 1.2.0 - * @see Redis Documentation: MULTI + * 角色的信息 + * @see Redis Documentation: ROLE + * @since redis 2.8.12 + * @return 返回当前是master,slave还是sentinel */ - public void beginTransactionAsObj() { - this.redisTemplate.multi(); + @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)); } /** - * 标记一个字符串事务块的开始 - * @since redis 1.2.0 - * @see Redis Documentation: MULTI + * 转为从服务器 + * @see Redis Documentation: SLAVEOF + * @since redis 1.0.0 + * @param ip 主服务器IP + * @param port 主服务器端口 */ - public void beginTransaction() { - this.stringRedisTemplate.multi(); + public void slaveOf(String ip, int port) { + this.redisTemplate.slaveOf(ip, port); } /** - * 提交所有对象事务块内的命令 - * @since redis 1.2.0 - * @see Redis Documentation: EXEC + * 转为主服务器 + * @see Redis Documentation: SLAVEOF NO ONE + * @since redis 1.0.0 */ - public List commitAsObj() { - return this.redisTemplate.exec(); + public void slaveOfNoOne() { + this.redisTemplate.slaveOfNoOne(); } /** - * 提交所有字符串事务块内的命令 + * 开始事务 * @since redis 1.2.0 - * @see Redis Documentation: EXEC + * @see Redis Documentation: MULTI */ - public List commit() { - return this.stringRedisTemplate.exec(); + public void beginTransaction() { + this.redisTemplate.multi(); } /** - * 取消对象事务 - * @see Redis Documentation: DISCARD - * @since redis 2.0.0 + * 提交事务 + * @since redis 1.2.0 + * @see Redis Documentation: EXEC + * @return 返回执行命令列表 */ - public void cancelTransactionAsObj() { - this.redisTemplate.discard(); + public List commit() { + 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(); } /** @@ -437,18 +376,51 @@ public final class DBHandler implements RedisHandler { } /** - * 获取spring redis模板 - * @return 返回对象模板 + * 检查连接 + * @see Redis Documentation: PING + * @since redis 1.0.0 + * @return 返回延迟时间(ms) */ - public RedisTemplate getRedisTemplate() { - return this.redisTemplate; + public Long ping() { + RedisConnection connection = this.redisTemplate.getRequiredConnectionFactory().getConnection(); + long begin = CLOCK.millis(); + connection.ping(); + long end = CLOCK.millis(); + return end - begin; } /** - * 获取spring string redis模板 - * @return 返回字符串模板 + * 服务器时间 + * @see Redis Documentation: TIME + * @since redis 2.6.0 + * @return 返回服务器时间(ms) */ - public StringRedisTemplate getStringRedisTemplate() { - return this.stringRedisTemplate; + 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)) + ); + } + + /** + * 获取spring redis模板 + * @return 返回对象模板 + */ + public RedisTemplate getRedisTemplate() { + return this.redisTemplate; } } diff --git a/src/main/java/wiki/xsx/core/handler/HandlerManager.java b/src/main/java/wiki/xsx/core/handler/HandlerManager.java index a47d9638d5ce593f30e63d211860ff82d7ab2158..5628f1a27cf625fc9d2be22ae9d121e129ec349f 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 0000000000000000000000000000000000000000..679a769063a7b4e40182bfab8bd9d7f3c49703d1 --- /dev/null +++ b/src/main/java/wiki/xsx/core/handler/HandlerManagerProxy.java @@ -0,0 +1,57 @@ +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 助手类型 + * @param 返回类型 + * @return 返回助手 + */ + @SuppressWarnings("unchecked") + public T getHandler(HandlerType type) { + return (T) this.manager.getHandler(this.manager.getDefaultKey(), type); + } + + /** + * 获取助手 + * @param key KEY + * @param type 助手类型 + * @param 返回类型 + * @return 返回助手 + */ + @SuppressWarnings("unchecked") + public T getHandler(String key, HandlerType type) { + return (T) 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 53adbc5749187912427ca0018792133699f98e34..73fbf04a004598396afa0ec3ce306686257757c6 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; /** * 哈希助手 @@ -331,6 +334,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 +342,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 +357,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 +386,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 +401,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 bf5706059bebd371a9cbb9a900ccf553b989f09e..4ea8d1c891d51deca13af57fca27039206a7683b 100644 --- a/src/main/java/wiki/xsx/core/handler/NumberHandler.java +++ b/src/main/java/wiki/xsx/core/handler/NumberHandler.java @@ -1,11 +1,13 @@ 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; 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; @@ -24,6 +26,10 @@ public final class NumberHandler implements RedisHandler { * 字符串模板 */ private ValueOperations stringOperations; + /** + * 字符串哈希模板 + */ + private HashOperations stringHashOperations; /** * 数据库索引 */ @@ -37,6 +43,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(); } /** @@ -67,208 +74,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: INCRBYFLOAT - * @since redis 2.6.0 + * 设置浮点数 + * @see Redis Documentation: HSET + * @since redis 2.0.0 * @param key 键 - * @param data 步长 - * @return 返回原值 + * @param hashKey hash键 + * @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: INCRBY - * @since redis 1.0.0 + * 设置浮点数(若存在则更新过期时间) + * @see Redis Documentation: SETEX + * @since redis 2.0.0 * @param key 键 - * @param data 步长 - * @return 返回增加后的值 + * @param value 值 + * @param timeout 过期时间 + * @param unit 时间单位 */ - public Long addLong(String key, long data) { - return this.stringOperations.increment(key, 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 getAndAddLong(String key, long data) { - return this.addLong(key, data) - data; + public Boolean setDoubleIfAbsent(String key, double value) { + return this.stringOperations.setIfAbsent(key, String.valueOf(value)); } /** - * 长整数自增 - * @see Redis Documentation: INCR + * 设置浮点数如果不存在 + * @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 + * @since redis 2.6.12 + * @param key 键 + * @param value 浮点数 + * @param timeout 过期时间 + * @param unit 时间单位 + * @return 返回布尔值,成功true,失败false + */ + public Boolean setDoubleIfAbsent(String key, double value, long timeout, TimeUnit unit) { + return this.stringOperations.setIfAbsent(key, String.valueOf(value), timeout, unit); + } + + /** + * 获取浮点数 + * @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: INCR + * 获取浮点数 + * @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: 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: HGET + * @see Redis Documentation: HSET + * @since redis 2.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: INCRBYFLOAT * @since redis 2.6.0 * @param key 键 * @param data 步长 - * @return 返回相减后的值 + * @return 返回增加后的值 */ - public Double subtractDouble(String key, double data) { - return this.addDouble(key, -data); + public Double addDouble(String key, double data) { + return this.stringOperations.increment(key, data); } /** - * 获取并减小浮点数 + * 增加浮点数 + * @see Redis Documentation: GET + * @see Redis Documentation: SET + * @since redis 2.0.0 + * @param key 键 + * @param data 步长 + * @return 返回增加后的值 + */ + public synchronized Double addDoubleBySync(String key, double data) { + Double old = this.getDouble(key); + double value = new BigDecimal(old==null?"0":Double.toString(old)).add(new BigDecimal(Double.toString(data))).doubleValue(); + this.setDouble(key, value); + return value; + } + + /** + * 增加浮点数 + * @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: HGET + * @see Redis Documentation: HSET + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回增加后的值 + */ + public synchronized Double addDoubleBySync(String key, String hashKey, double data) { + Double old = this.getDouble(key, hashKey); + double value = new BigDecimal(old==null?"0":Double.toString(old)).add(new BigDecimal(Double.toString(data))).doubleValue(); + this.setDouble(key, hashKey, value); + return value; + } + + /** + * 获取并增加浮点数 * @see Redis Documentation: INCRBYFLOAT * @since redis 2.6.0 * @param key 键 * @param data 步长 * @return 返回原值 */ - public Double getAndSubtractDouble(String key, double data) { - return this.addDouble(key, -data) + data; + public Double getAndAddDouble(String key, double data) { + return new BigDecimal(Double.toString(this.addDouble(key, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** - * 长整数减小 - * @see Redis Documentation: DECRBY - * @since redis 1.0.0 + * 获取并增加浮点数 + * @see Redis Documentation: GET + * @see Redis Documentation: SET + * @since redis 2.0.0 * @param key 键 * @param data 步长 - * @return 返回减小后的值 + * @return 返回原值 */ - public Long subtractLong(String key, long data) { - return this.stringOperations.decrement(key, data); + public synchronized Double getAndAddDoubleBySync(String key, double data) { + return new BigDecimal(Double.toString(this.addDoubleBySync(key, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** - * 获取并减小长整数 - * @see Redis Documentation: DECRBY - * @since redis 1.0.0 + * 获取并增加浮点数 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 * @param key 键 + * @param hashKey hash键 * @param data 步长 - * @return 返回减小后的值 + * @return 返回原值 */ - public Long getAndSubtractLong(String key, long data) { - return this.subtractLong(key, data) + data; + public Double getAndAddDouble(String key, String hashKey, double data) { + return new BigDecimal(Double.toString(this.addDouble(key, hashKey, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** - * 长整数递减 - * @see Redis Documentation: DECR - * @since redis 1.0.0 + * 获取并增加浮点数 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 2.0.0 * @param key 键 - * @return 返回递减后的值 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 */ - public Long decrementLong(String key) { - return this.stringOperations.decrement(key); + public synchronized Double getAndAddDoubleBySync(String key, String hashKey, double data) { + return new BigDecimal(Double.toString(this.addDoubleBySync(key, hashKey, data))).subtract(new BigDecimal(Double.toString(data))).doubleValue(); } /** - * 获取并递减长整数 - * @see Redis Documentation: DECR - * @since redis 1.0.0 + * 浮点数减小 + * @see Redis Documentation: INCRBYFLOAT + * @since redis 2.6.0 * @param key 键 - * @return 返回原值 + * @param data 步长 + * @return 返回相减后的值 */ - public Long getAndDecrementLong(String key) { - return this.decrementLong(key) + 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 * @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: SETEX - * @since redis 2.0.0 + * 浮点数减小 + * @see Redis Documentation: HINCRBYFLOAT + * @since redis 2.6.0 * @param key 键 - * @param value 值 - * @param timeout 过期时间 - * @param unit 时间单位 + * @param hashKey hash键 + * @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.0.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: SETNX - * @since redis 2.6.12 + * 获取并减小浮点数 + * @see Redis Documentation: INCRBYFLOAT + * @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 Double getAndSubtractDouble(String key, double data) { + return new BigDecimal(Double.toString(this.subtractDouble(key, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); } /** - * 获取浮点数 + * 获取并减小浮点数 * @see Redis Documentation: GET - * @since redis 1.0.0 + * @see Redis Documentation: SET + * @since redis 2.0.0 * @param key 键 - * @return 返回浮点数 + * @param data 步长 + * @return 返回原值 */ - public Double getDouble(String key) { - String value = this.stringOperations.get(key); - if (value!=null) { - return Double.valueOf(value); - } - return null; + public synchronized Double getAndSubtractDoubleBySync(String key, double data) { + return new BigDecimal(Double.toString(this.subtractDoubleBySync(key, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); + } + + /** + * 获取并减小浮点数 + * @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 new BigDecimal(Double.toString(this.subtractDouble(key, hashKey, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); + } + + /** + * 获取并减小浮点数 + * @see Redis Documentation: HGET + * @see Redis Documentation: HSET + * @since redis 2.0.0 + * @param key 键 + * @param hashKey hash键 + * @param data 步长 + * @return 返回原值 + */ + public synchronized Double getAndSubtractDoubleBySync(String key, String hashKey, double data) { + return new BigDecimal(Double.toString(this.subtractDoubleBySync(key, hashKey, data))).add(new BigDecimal(Double.toString(data))).doubleValue(); } /** @@ -282,6 +433,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 +470,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 @@ -337,29 +513,240 @@ public final class NumberHandler implements RedisHandler { } /** - * 获取并设置浮点数 + * 获取长整数 + * @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 * @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: GETSET + * @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, long newValue) { - String value = this.stringOperations.getAndSet(key, String.valueOf(newValue)); - return value!=null?Long.valueOf(value):null; + 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: INCRBY + * @since redis 1.0.0 + * @param key 键 + * @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 getAndAddLong(String key, long data) { + 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 + * @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键 + * @return 返回原值 + */ + 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; } /** @@ -369,10 +756,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 6cf12287e09363e54ebc82c9e7273779964f0835..d30da0c7fc622f2854642d95b3e4c5d259d76750 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 aeb90167bb68ea4ce093c5f879dc8f598be946c2..9241601402949eb3042a9a4e346f72788b598fc1 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); } /** diff --git a/src/main/java/wiki/xsx/core/util/RedisUtil.java b/src/main/java/wiki/xsx/core/util/RedisUtil.java index e470940893f173ec22d88c014b6461bf7d7460db..f810c5dbb1cfbfaf81ce8dbb24e2528944b7ce36 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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(); } }