com.logicbus.redis.toolkit.InfoTool Maven / Gradle / Ivy
package com.logicbus.redis.toolkit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.anysoft.util.Pair;
import org.apache.commons.lang3.StringUtils;
import com.anysoft.util.IOTools;
import com.logicbus.redis.client.Connection;
import com.logicbus.redis.client.Toolkit;
import com.logicbus.redis.util.SafeEncoder;
/**
* 信息工具
* @author yyduan
* @version 1.6.12.56 [20191226]
* - 优化信息输出接口
*/
public class InfoTool extends Toolkit {
protected static Pattern pattern = Pattern.compile("^([^\\:]+):(.*)$");
public InfoTool(Connection _conn) {
super(_conn);
}
public static enum Command {
INFO;
public final byte [] raw;
Command(){
raw = SafeEncoder.encode(name());
}
}
public void _info(final String section){
sendCommand(Command.INFO.raw, section);
}
public String info(final String section){
_info(section);
return getBulkReply();
}
public void info(final String section,List> result){
String ret = info(section);
if (StringUtils.isNotEmpty(ret)){
BufferedReader reader = new BufferedReader(new StringReader(ret));
String line = null;
try{
while ((line = reader.readLine()) != null){
Matcher matcher = pattern.matcher(line);
if (matcher.find()){
result.add(new Pair.Default(matcher.group(1),matcher.group(2)));
}
}
}catch (IOException ex){
}finally{
IOTools.close(reader);
}
}
}
public void server(List> result){
info("server",result);
}
public void clients(List> result){
info("clients",result);
}
public void memory(List> result){
info("memory",result);
}
public void persistence(List> result){
info("persistence",result);
}
public void stats(List> result){
info("stats",result);
}
public void replication(List> result){
info("replication",result);
}
public void cpu(List> result){
info("cpu",result);
}
public void commandstats(List> result){
info("commandstats",result);
}
public void cluster(List> result){
info("cluster",result);
}
public void keyspace(List> result){
info("keyspace",result);
}
public void all(List> result){
info("all",result);
}
public void defaults(List> result){
info("default",result);
}
}