Commit 67426d72f4b59347475b41b0dca434481faf6c19
1 parent
c88f33c9
refactor(ip): 使用 static 块优化 IPUtils 和 AreaUtils 初始化逻辑
Showing
2 changed files
with
42 additions
and
41 deletions
yudao-framework/yudao-spring-boot-starter-biz-ip/src/main/java/cn/iocoder/yudao/framework/ip/core/utils/AreaUtils.java
| @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||
| 8 | import cn.iocoder.yudao.framework.ip.core.Area; | 8 | import cn.iocoder.yudao.framework.ip.core.Area; |
| 9 | import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum; | 9 | import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum; |
| 10 | import lombok.NonNull; | 10 | import lombok.NonNull; |
| 11 | +import lombok.experimental.UtilityClass; | ||
| 11 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
| 12 | 13 | ||
| 13 | import java.util.ArrayList; | 14 | import java.util.ArrayList; |
| @@ -25,44 +26,46 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. | @@ -25,44 +26,46 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. | ||
| 25 | * @author 芋道源码 | 26 | * @author 芋道源码 |
| 26 | */ | 27 | */ |
| 27 | @Slf4j | 28 | @Slf4j |
| 29 | +@UtilityClass | ||
| 28 | public class AreaUtils { | 30 | public class AreaUtils { |
| 29 | 31 | ||
| 30 | /** | 32 | /** |
| 31 | - * 初始化 SEARCHER | ||
| 32 | - */ | ||
| 33 | - @SuppressWarnings("InstantiationOfUtilityClass") | ||
| 34 | - private final static AreaUtils INSTANCE = new AreaUtils(); | ||
| 35 | - | ||
| 36 | - /** | ||
| 37 | * Area 内存缓存,提升访问速度 | 33 | * Area 内存缓存,提升访问速度 |
| 38 | */ | 34 | */ |
| 39 | private static Map<Integer, Area> areas; | 35 | private static Map<Integer, Area> areas; |
| 40 | 36 | ||
| 41 | - private AreaUtils() { | ||
| 42 | - long now = System.currentTimeMillis(); | ||
| 43 | - areas = new HashMap<>(); | ||
| 44 | - areas.put(Area.ID_GLOBAL, new Area(Area.ID_GLOBAL, "全球", 0, | ||
| 45 | - null, new ArrayList<>())); | ||
| 46 | - // 从 csv 中加载数据 | ||
| 47 | - List<CsvRow> rows = CsvUtil.getReader().read(ResourceUtil.getUtf8Reader("area.csv")).getRows(); | ||
| 48 | - rows.remove(0); // 删除 header | ||
| 49 | - for (CsvRow row : rows) { | ||
| 50 | - // 创建 Area 对象 | ||
| 51 | - Area area = new Area(Integer.valueOf(row.get(0)), row.get(1), Integer.valueOf(row.get(2)), | ||
| 52 | - null, new ArrayList<>()); | ||
| 53 | - // 添加到 areas 中 | ||
| 54 | - areas.put(area.getId(), area); | ||
| 55 | - } | 37 | + static { |
| 38 | + init(); | ||
| 39 | + } | ||
| 56 | 40 | ||
| 57 | - // 构建父子关系:因为 Area 中没有 parentId 字段,所以需要重复读取 | ||
| 58 | - for (CsvRow row : rows) { | ||
| 59 | - Area area = areas.get(Integer.valueOf(row.get(0))); // 自己 | ||
| 60 | - Area parent = areas.get(Integer.valueOf(row.get(3))); // 父 | ||
| 61 | - Assert.isTrue(area != parent, "{}:父子节点相同", area.getName()); | ||
| 62 | - area.setParent(parent); | ||
| 63 | - parent.getChildren().add(area); | 41 | + /** |
| 42 | + * 初始化 | ||
| 43 | + */ | ||
| 44 | + private static void init() { | ||
| 45 | + try { | ||
| 46 | + long now = System.currentTimeMillis(); | ||
| 47 | + areas = new HashMap<>(); | ||
| 48 | + areas.put(Area.ID_GLOBAL, new Area(Area.ID_GLOBAL, "全球", 0, null, new ArrayList<>())); | ||
| 49 | + // 从 csv 中加载数据 | ||
| 50 | + List<CsvRow> rows = CsvUtil.getReader().read(ResourceUtil.getUtf8Reader("area.csv")).getRows(); | ||
| 51 | + rows.remove(0); // 删除 header | ||
| 52 | + for (CsvRow row : rows) { | ||
| 53 | + Area area = new Area(Integer.valueOf(row.get(0)), row.get(1), Integer.valueOf(row.get(2)), null, new ArrayList<>()); | ||
| 54 | + areas.put(area.getId(), area); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + // 构建父子关系:因为 Area 中没有 parentId 字段,所以需要重复读取 | ||
| 58 | + for (CsvRow row : rows) { | ||
| 59 | + Area area = areas.get(Integer.valueOf(row.get(0))); // 自己 | ||
| 60 | + Area parent = areas.get(Integer.valueOf(row.get(3))); // 父 | ||
| 61 | + Assert.isTrue(area != parent, "{}:父子节点相同", area.getName()); | ||
| 62 | + area.setParent(parent); | ||
| 63 | + parent.getChildren().add(area); | ||
| 64 | + } | ||
| 65 | + log.info("启动加载 AreaUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now); | ||
| 66 | + } catch (Exception e) { | ||
| 67 | + throw new RuntimeException("AreaUtils 初始化失败", e); | ||
| 64 | } | 68 | } |
| 65 | - log.info("启动加载 AreaUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now); | ||
| 66 | } | 69 | } |
| 67 | 70 | ||
| 68 | /** | 71 | /** |
yudao-framework/yudao-spring-boot-starter-biz-ip/src/main/java/cn/iocoder/yudao/framework/ip/core/utils/IPUtils.java
| @@ -3,11 +3,10 @@ package cn.iocoder.yudao.framework.ip.core.utils; | @@ -3,11 +3,10 @@ package cn.iocoder.yudao.framework.ip.core.utils; | ||
| 3 | import cn.hutool.core.io.resource.ResourceUtil; | 3 | import cn.hutool.core.io.resource.ResourceUtil; |
| 4 | import cn.iocoder.yudao.framework.ip.core.Area; | 4 | import cn.iocoder.yudao.framework.ip.core.Area; |
| 5 | import lombok.SneakyThrows; | 5 | import lombok.SneakyThrows; |
| 6 | +import lombok.experimental.UtilityClass; | ||
| 6 | import lombok.extern.slf4j.Slf4j; | 7 | import lombok.extern.slf4j.Slf4j; |
| 7 | import org.lionsoul.ip2region.xdb.Searcher; | 8 | import org.lionsoul.ip2region.xdb.Searcher; |
| 8 | 9 | ||
| 9 | -import java.io.IOException; | ||
| 10 | - | ||
| 11 | /** | 10 | /** |
| 12 | * IP 工具类 | 11 | * IP 工具类 |
| 13 | * | 12 | * |
| @@ -16,30 +15,29 @@ import java.io.IOException; | @@ -16,30 +15,29 @@ import java.io.IOException; | ||
| 16 | * @author wanglhup | 15 | * @author wanglhup |
| 17 | */ | 16 | */ |
| 18 | @Slf4j | 17 | @Slf4j |
| 18 | +@UtilityClass | ||
| 19 | public class IPUtils { | 19 | public class IPUtils { |
| 20 | 20 | ||
| 21 | /** | 21 | /** |
| 22 | - * 初始化 SEARCHER | ||
| 23 | - */ | ||
| 24 | - @SuppressWarnings("InstantiationOfUtilityClass") | ||
| 25 | - private final static IPUtils INSTANCE = new IPUtils(); | ||
| 26 | - | ||
| 27 | - /** | ||
| 28 | * IP 查询器,启动加载到内存中 | 22 | * IP 查询器,启动加载到内存中 |
| 29 | */ | 23 | */ |
| 30 | private static Searcher SEARCHER; | 24 | private static Searcher SEARCHER; |
| 31 | 25 | ||
| 26 | + static { | ||
| 27 | + init(); | ||
| 28 | + } | ||
| 29 | + | ||
| 32 | /** | 30 | /** |
| 33 | - * 私有化构造 | 31 | + * 初始化 |
| 34 | */ | 32 | */ |
| 35 | - private IPUtils() { | 33 | + private static void init() { |
| 36 | try { | 34 | try { |
| 37 | long now = System.currentTimeMillis(); | 35 | long now = System.currentTimeMillis(); |
| 38 | byte[] bytes = ResourceUtil.readBytes("ip2region.xdb"); | 36 | byte[] bytes = ResourceUtil.readBytes("ip2region.xdb"); |
| 39 | SEARCHER = Searcher.newWithBuffer(bytes); | 37 | SEARCHER = Searcher.newWithBuffer(bytes); |
| 40 | log.info("启动加载 IPUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now); | 38 | log.info("启动加载 IPUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now); |
| 41 | - } catch (IOException e) { | ||
| 42 | - log.error("启动加载 IPUtils 失败", e); | 39 | + } catch (Exception e) { |
| 40 | + throw new RuntimeException("IPUtils 初始化失败", e); | ||
| 43 | } | 41 | } |
| 44 | } | 42 | } |
| 45 | 43 |