!1 缓存空间大小从配置中取

* 缓存空间大小从配置中取
This commit is contained in:
黄志安
2019-10-24 01:08:23 +08:00
committed by matrixy
parent b82a0da9e9
commit e5892f0023
2 changed files with 39 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package cn.org.hentai.dns.stat;
import cn.org.hentai.dns.util.Configs;
import cn.org.hentai.dns.util.Conversion;
import cn.org.hentai.dns.util.Packet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,7 +34,9 @@ public final class StatManager
private StatManager()
{
logs = Packet.create(1024 * 1024 * 200);
String lmem = Configs.get("dns.stat-logger.memory");
int i = Conversion.toByte(lmem);
logs = Packet.create(i);
domainNameMap = new HashMap();
sequence = 1;

View File

@@ -0,0 +1,35 @@
package cn.org.hentai.dns.util;
/**
* Created by huangzhian on 2019/10/21.
*/
public class Conversion {
/**
* 将配置的缓存空间单位KMG转换为byte,
* 因为int类型的限制将最大缓存空间设为1G
*/
public static int toByte(String memory) {
int count = 0;
String mem;
if (memory == null) {
return 0;
}
mem = memory.toUpperCase();
if (mem.matches("[0-9]+[KMG]{1}")) {
String submem = memory.substring(0, memory.length() - 1);
count = Integer.parseInt(submem);
} else if (mem.matches("[0-9]+")) {
count = Integer.parseInt(memory);
}
if (mem.endsWith("M")) {
return (count > 1024 ? 1024 : count) << 20;
} else if (mem.endsWith("G")) {
return (count > 1 ? 1 : count) << 30;
} else if (mem.endsWith("K")) {
return count << 10;
} else {
return count;
}
}
}