redisTemplate;
-
- /** 数据库索引 */
- private int dbIndex;
-
- /**
- * 数据库助手构造
- *
- * @param dbIndex 数据库索引
- */
- DBHandler(Integer dbIndex) {
- this.dbIndex = dbIndex;
- this.redisTemplate = HandlerManager.createRedisTemplate(dbIndex);
- }
-
- /**
- * 数据库助手构造
- *
- * @param transactionHandler 事务助手
- */
- DBHandler(TransactionHandler transactionHandler) {
- this.dbIndex = transactionHandler.getDbIndex();
- this.redisTemplate = transactionHandler.getRedisTemplate();
- }
-
- /**
- * 获取当前数据库索引
- *
- * @since redis 1.0.0
- * @return 返回当前数据库索引
- */
- public int getDBIndex() {
- return this.dbIndex;
- }
-
- /**
- * 获取当前数据库信息
- *
- * @see Redis Documentation: INFO
- * @since redis 1.0.0
- * @return 返回当前数据库信息
- */
- public Properties getDBInfo() {
- return this.redisTemplate.getRequiredConnectionFactory().getConnection().serverCommands().info();
- }
-
- /**
- * 获取当前数据库信息
- *
- * @see Redis Documentation: INFO
- * @since redis 1.0.0
- * @param dbOption 选项
- *
- * SERVER: Redis服务器的一般信息
- *
- *
- * CLIENTS: 客户端的连接部分相关信息
- *
- *
- * MEMORY: 内存消耗相关信息
- *
- *
- * PERSISTENCE: RDB和AOF相关信息
- *
- *
- * STATS: 一般统计信息
- *
- *
- * REPLICATION: 主/从复制信息
- *
- *
- * CPU: CPU的相关信息
- *
- *
- * COMMANDSTATS: Redis命令统计相关信息
- *
- *
- * CLUSTER: Redis集群信息
- *
- *
- * KEYSPACE: 数据库的相关统计信息
- *
- *
- * ALL: 所有信息
- *
- *
- * DEFAULT: 默认设置的信息
- *
- * @return 返回当前数据库信息
- */
- public Properties getDBInfo(DBOption dbOption) {
- RedisServerCommands serverCommands = this.redisTemplate.getRequiredConnectionFactory().getConnection()
- .serverCommands();
- if (dbOption != null) {
- return serverCommands.info(dbOption.option);
- }
- 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
- * @since redis 2.0.0
- * @param param 参数名
- * @param value 参数值
- */
- public void setConfig(String param, String value) {
- this.redisTemplate.getRequiredConnectionFactory().getConnection().setConfig(param, value);
- }
-
- /**
- * 获取数据库配置信息
- *
- * @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);
- }
-
- /**
- * 重置配置状态
- *
- * @see Redis Documentation:
- * CONFIG RESETSTAT
- * @since redis 2.0.0
- */
- public void resetConfigStats() {
- this.redisTemplate.getRequiredConnectionFactory().getConnection().resetConfigStats();
- }
-
- /**
- * 设置客户端连接名称
- *
- * @see Redis Documentation:
- * CLIENT SETNAME
- * @since redis 2.6.9
- * @param name 名称
- */
- public void setClientName(String name) {
- this.redisTemplate.getRequiredConnectionFactory().getConnection()
- .setClientName(RedisSerializer.string().serialize(name));
- }
-
- /**
- * 获取客户端连接名称
- *
- * @see Redis Documentation:
- * CLIENT GETNAME
- * @since redis 2.6.9
- * @return 返回客户端连接名称
- */
- public String getClientName() {
- return this.redisTemplate.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