ip:AreaUtils、IPUtils 优化

pull/2/head
YunaiV 2022-12-13 20:19:54 +08:00
parent 4a2cb33cd7
commit 40ce481e2e
3 changed files with 56 additions and 68 deletions

View File

@ -6,6 +6,7 @@ import cn.hutool.core.text.csv.CsvUtil;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
@ -17,19 +18,20 @@ import java.util.Map;
*
* @author
*/
@Slf4j
public class AreaUtils {
/**
* SEARCHER
*/
@SuppressWarnings("InstantiationOfUtilityClass")
private final static AreaUtils INSTANCE = new AreaUtils();
private static Map<Integer, Area> areas;
static {
// 懒加载,使用时初始化
init();
}
/**
*
*/
private static synchronized void init() {
private AreaUtils() {
long now = System.currentTimeMillis();
areas = new HashMap<>();
areas.put(Area.ID_GLOBAL, new Area(Area.ID_GLOBAL, "全球", 0,
null, new ArrayList<>()));
@ -51,6 +53,7 @@ public class AreaUtils {
area.setParent(parent);
parent.getChildren().add(area);
}
log.info("启动加载 AreaUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
}
/**

View File

@ -1,82 +1,74 @@
package cn.iocoder.yudao.framework.ip.core.utils;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.iocoder.yudao.framework.ip.core.Area;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import java.io.IOException;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
/**
* IP
* <p>
* ip2region.xdb<a href="https://gitee.com/zhijiantianya/ip2region"/>
* region
*
* @author
* IP ip2region.xdb <a href="https://gitee.com/zhijiantianya/ip2region"/>
*
* @author wanglhup
*/
@Slf4j
public class IPUtils {
/**
* ip
*
*/
private static Searcher SEARCHER;
/**
* SEARCHER
* SEARCHER
*/
@SuppressWarnings("InstantiationOfUtilityClass")
private final static IPUtils INSTANCE = new IPUtils();
/**
* IP
*/
private static Searcher SEARCHER;
/**
*
*/
private IPUtils() {
String dbPath = "ip2region.xdb";
dbPath = IPUtils.class.getClassLoader().getResource(dbPath).getPath();
try {
SEARCHER = Searcher.newWithBuffer(Searcher.loadContentFromFile(dbPath));
long now = System.currentTimeMillis();
byte[] bytes = ResourceUtil.readBytes("ip2region.xdb");
SEARCHER = Searcher.newWithBuffer(bytes);
log.info("启动加载 IPUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
} catch (IOException e) {
// 加载xdb文件异常,不影响启动
log.error("启动加载IP SEARCH异常", e);
SEARCHER = null;
log.error("启动加载 IPUtils 失败", e);
}
}
/**
* IPID127.0.0.1
* @param ip ip
* IP
*
* @param ip IP 127.0.0.1
* @return id
*/
@SneakyThrows
public static Integer getAreaId(String ip) {
try {
return Integer.parseInt(SEARCHER.search(ip));
} catch (Exception e) {
throw new ServiceException(INTERNAL_SERVER_ERROR);
}
}
/**
* IPID{@link Searcher#checkIP(String)}
* @param ip ip
* @return id
* IP
*
* @param ip IP {@link Searcher#checkIP(String)}
* @return
*/
@SneakyThrows
public static Integer getAreaId(long ip) {
try {
return Integer.parseInt(SEARCHER.search(ip));
} catch (Exception e) {
throw new ServiceException(INTERNAL_SERVER_ERROR);
}
}
/**
* IP127.0.0.1
* @param ip ip
* IP
*
* @param ip IP 127.0.0.1
* @return
*/
public static Area getArea(String ip) {
@ -84,8 +76,9 @@ public class IPUtils {
}
/**
* IP{@link Searcher#checkIP(String)}
* @param ip ip
* IP
*
* @param ip IP {@link Searcher#checkIP(String)}
* @return
*/
public static Area getArea(long ip) {

View File

@ -9,47 +9,39 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* {@link IPUtils}
*
* @author wanglhup
*/
class IPUtilsTest {
public class IPUtilsTest {
@Test
void getAreaId() {
public void testGetAreaId_string() {
// 120.202.4.0|120.202.4.255|420600
Integer areaId = IPUtils.getAreaId("120.202.4.50");
assertEquals(420600, areaId);
}
@Test
void testGetAreaId() {
public void testGetAreaId_long() throws Exception {
// 120.203.123.0|120.203.133.255|360900
long ip = 0L;
try {
ip = Searcher.checkIP("120.203.123.250");
} catch (Exception e) {
// ignore
}
long ip = Searcher.checkIP("120.203.123.250");
Integer areaId = IPUtils.getAreaId(ip);
assertEquals(360900, areaId);
}
@Test
void getArea() {
public void testGetArea_string() {
// 120.202.4.0|120.202.4.255|420600
Area area = IPUtils.getArea("120.202.4.50");
assertEquals("襄阳市", area.getName());
}
@Test
void testGetArea() {
public void testGetArea_long() throws Exception {
// 120.203.123.0|120.203.133.255|360900
long ip = 0L;
try {
ip = Searcher.checkIP("120.203.123.252");
} catch (Exception e) {
// ignore
}
long ip = Searcher.checkIP("120.203.123.252");
Area area = IPUtils.getArea(ip);
assertEquals("宜春市", area.getName());
}
}