【fix sonar】RedisServiceImpl.java文件

This commit is contained in:
tianwenbo
2023-03-03 11:53:38 +08:00
parent de53a72187
commit 825f75a8f5
@@ -31,6 +31,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class RedisServiceImpl implements RedisService { public class RedisServiceImpl implements RedisService {
private static String CREATE_TIME ="create_time";
private static String USER_MEMORY ="used_memory";
@Resource @Resource
private RedisConnectionFactory redisConnectionFactory; private RedisConnectionFactory redisConnectionFactory;
@@ -40,6 +42,8 @@ public class RedisServiceImpl implements RedisService {
@Override @Override
public List<RedisInfo> getRedisInfo() throws RedisConnectException { public List<RedisInfo> getRedisInfo() throws RedisConnectException {
Properties info = redisConnectionFactory.getConnection().info(); Properties info = redisConnectionFactory.getConnection().info();
if(info==null)
return new ArrayList<>();
List<RedisInfo> infoList = new ArrayList<>(); List<RedisInfo> infoList = new ArrayList<>();
RedisInfo redisInfo = null; RedisInfo redisInfo = null;
for (Map.Entry<Object, Object> entry : info.entrySet()) { for (Map.Entry<Object, Object> entry : info.entrySet()) {
@@ -55,7 +59,7 @@ public class RedisServiceImpl implements RedisService {
public Map<String, Object> getKeysSize() throws RedisConnectException { public Map<String, Object> getKeysSize() throws RedisConnectException {
Long dbSize = redisConnectionFactory.getConnection().dbSize(); Long dbSize = redisConnectionFactory.getConnection().dbSize();
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("create_time", System.currentTimeMillis()); map.put(CREATE_TIME, System.currentTimeMillis());
map.put("dbSize", dbSize); map.put("dbSize", dbSize);
log.info("--getKeysSize--: " + map.toString()); log.info("--getKeysSize--: " + map.toString());
@@ -66,15 +70,17 @@ public class RedisServiceImpl implements RedisService {
public Map<String, Object> getMemoryInfo() throws RedisConnectException { public Map<String, Object> getMemoryInfo() throws RedisConnectException {
Map<String, Object> map = null; Map<String, Object> map = null;
Properties info = redisConnectionFactory.getConnection().info(); Properties info = redisConnectionFactory.getConnection().info();
if(info==null)
return new HashMap<>();
for (Map.Entry<Object, Object> entry : info.entrySet()) { for (Map.Entry<Object, Object> entry : info.entrySet()) {
String key = oConvertUtils.getString(entry.getKey()); String key = oConvertUtils.getString(entry.getKey());
if ("used_memory".equals(key)) { if (USER_MEMORY.equals(key)) {
map = new HashMap<>(); map = new HashMap<>();
map.put("used_memory", entry.getValue()); map.put(USER_MEMORY, entry.getValue());
map.put("create_time", System.currentTimeMillis()); map.put(CREATE_TIME, System.currentTimeMillis());
} }
} }
log.info("--getMemoryInfo--: " + map.toString()); log.info("--getMemoryInfo--: " + (map==null?":":map.toString()));
return map; return map;
} }
@@ -109,10 +115,10 @@ public class RedisServiceImpl implements RedisService {
jo.put("value",map.get("dbSize")); jo.put("value",map.get("dbSize"));
}else{ }else{
map = getMemoryInfo(); map = getMemoryInfo();
Integer used_memory = Integer.valueOf(map.get("used_memory").toString()); Integer used_memory = Integer.valueOf(map.get(USER_MEMORY).toString());
jo.put("value",used_memory/1000); jo.put("value",used_memory/1000);
} }
String create_time = DateUtil.formatTime(DateUtil.date((Long) map.get("create_time")-(4-i)*1000)); String create_time = DateUtil.formatTime(DateUtil.date((Long) map.get(CREATE_TIME)-(4-i)*1000));
jo.put("name",create_time); jo.put("name",create_time);
json.add(jo); json.add(jo);
} }