[collector]fix 由于链接复用不佳造成创建过多链接监控异常 (#26)

This commit is contained in:
tomsun28
2022-03-14 20:36:47 +08:00
committed by GitHub
parent 4ce0698834
commit 790bcc6f16
3 changed files with 7 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package com.usthe.collector.collect.common.cache;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.ToString;
/** /**
* 缓存key唯一标识符 * 缓存key唯一标识符
@@ -10,6 +11,7 @@ import lombok.Data;
*/ */
@Data @Data
@Builder @Builder
@ToString
public class CacheIdentifier { public class CacheIdentifier {
private String ip; private String ip;

View File

@@ -21,9 +21,9 @@ import java.util.concurrent.TimeUnit;
public class CommonCache { public class CommonCache {
/** /**
* 默认缓存时间 10minute * 默认缓存时间 800s
*/ */
private static final long DEFAULT_CACHE_TIMEOUT = 10 * 60 * 1000L; private static final long DEFAULT_CACHE_TIMEOUT = 800 * 1000L;
/** /**
* 默认最大缓存数量 * 默认最大缓存数量
@@ -173,9 +173,11 @@ public class CommonCache {
public Optional<Object> getCache(Object key, boolean refreshCache) { public Optional<Object> getCache(Object key, boolean refreshCache) {
Long[] cacheTime = timeoutMap.get(key); Long[] cacheTime = timeoutMap.get(key);
if (cacheTime == null || cacheTime.length != CACHE_TIME_LENGTH) { if (cacheTime == null || cacheTime.length != CACHE_TIME_LENGTH) {
log.warn("[cache] not hit the cache, key {}.", key);
return Optional.empty(); return Optional.empty();
} }
if (cacheTime[0] + cacheTime[1] < System.currentTimeMillis()) { if (cacheTime[0] + cacheTime[1] < System.currentTimeMillis()) {
log.warn("[cache] is timeout, key {}.", key);
timeoutMap.remove(key); timeoutMap.remove(key);
cacheMap.remove(key); cacheMap.remove(key);
return Optional.empty(); return Optional.empty();

View File

@@ -134,7 +134,7 @@ public class JdbcCommonCollect extends AbstractCollect {
// 设置查询最大行数1000行 // 设置查询最大行数1000行
statement.setMaxRows(1000); statement.setMaxRows(1000);
JdbcConnect jdbcConnect = new JdbcConnect(connection); JdbcConnect jdbcConnect = new JdbcConnect(connection);
CommonCache.getInstance().addCache(identifier, jdbcConnect, 10000L); CommonCache.getInstance().addCache(identifier, jdbcConnect);
return statement; return statement;
} }