From 41371cfbee5b462543599c277866e3c8f280f7df Mon Sep 17 00:00:00 2001 From: yxw <17074267@qq.com> Date: Tue, 30 Dec 2025 18:07:41 +0800 Subject: [PATCH] add areas cache --- Atomx.Common/Constants/CacheKeys.cs | 5 ++- Atomx.Data/CacheServices/AreaCacheService.cs | 42 ++++++++++++++++---- Atomx.Utils/Extension/TreeExtension.cs | 36 +++++++++++++++++ 3 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 Atomx.Utils/Extension/TreeExtension.cs diff --git a/Atomx.Common/Constants/CacheKeys.cs b/Atomx.Common/Constants/CacheKeys.cs index b47a496..12cb4be 100644 --- a/Atomx.Common/Constants/CacheKeys.cs +++ b/Atomx.Common/Constants/CacheKeys.cs @@ -90,7 +90,10 @@ /// public const string ContentCategories = "Content.Categories"; - public const string Area = "Area"; + /// + /// 国家数据缓存树形结构 + /// + public const string CountryTree = "Area.CountryTree"; /// /// 国家数据缓存 diff --git a/Atomx.Data/CacheServices/AreaCacheService.cs b/Atomx.Data/CacheServices/AreaCacheService.cs index 42def31..ab2e39b 100644 --- a/Atomx.Data/CacheServices/AreaCacheService.cs +++ b/Atomx.Data/CacheServices/AreaCacheService.cs @@ -34,26 +34,52 @@ namespace Atomx.Data.CacheServices Task ResetArea(Area area); /// - /// 缓存 + /// 获取国家-省份-城市树形数据 /// + /// /// - Task> GetKeyValueTree(); + List GetAreaTreeAsync(long countryId); } public partial class CacheService : ICacheService { + public async Task> GetAreaTreeAsync(long countryId, bool? reload = false) + { + var cacheData = await GetCacheAsync>(CacheKeys.CountryTree); + if (cacheData == null || reload.HasValue && reload.Value) + { + var state = _dbContext.StateProvinces.Where(p => p.CountryId == countryId).ToList(); + var cities = _dbContext.Areas.Where(p => p.CountryId == countryId).ToList(); + cacheData = (from s in state + select new KeyValueTree + { + Key = s.Id.ToString(), + Value = s.Name, + Children = (from c in cities + where c.ParentId == s.Id + select new KeyValueTree + { + Key = c.Id.ToString(), + Value = c.Name + }).ToList() + }).ToList(); + await SetCacheAsync(CacheKeys.Country, cacheData); + } + return cacheData; + } + public async Task> GetCountry(bool? reload = false) { bool reloadData = reload.HasValue ? reload.Value : false; var cacheData = await GetCacheAsync>(CacheKeys.Country); - if(cacheData == null || reloadData) + if (cacheData == null || reloadData) { var countries = (from p in _dbContext.Areas - where p.ParentId == 0 - select p).ToList(); + where p.ParentId == 0 + select p).ToList(); await SetCacheAsync(CacheKeys.Country, countries); return countries; - + } return cacheData; } @@ -80,8 +106,8 @@ namespace Atomx.Data.CacheServices if (cacheData == null || reloadData) { var data = (from p in _dbContext.Areas - where p.ParentId == 0 - select p).ToList(); + where p.ParentId == 0 + select p).ToList(); await SetCacheAsync(CacheKeys.Country, data); return data; diff --git a/Atomx.Utils/Extension/TreeExtension.cs b/Atomx.Utils/Extension/TreeExtension.cs new file mode 100644 index 0000000..118002a --- /dev/null +++ b/Atomx.Utils/Extension/TreeExtension.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Atomx.Utils.Extension +{ + public static class TreeExtension + { + public static List ToTree(this List list, Func getId, Func getParentId, Action> setChildren, string rootParentId = "0") + { + var lookup = new Dictionary(); + var result = new List(); + foreach (var item in list) + { + var id = getId(item); + lookup[id] = item; + } + foreach (var item in list) + { + var parentId = getParentId(item); + if (parentId == rootParentId || !lookup.ContainsKey(parentId)) + { + result.Add(item); + } + else + { + var parent = lookup[parentId]; + var children = new List(); + setChildren(parent, children); + children.Add(item); + } + } + return result; + } + } +}