add areas cache
This commit is contained in:
@@ -90,7 +90,10 @@
|
||||
/// </summary>
|
||||
public const string ContentCategories = "Content.Categories";
|
||||
|
||||
public const string Area = "Area";
|
||||
/// <summary>
|
||||
/// 国家数据缓存树形结构
|
||||
/// </summary>
|
||||
public const string CountryTree = "Area.CountryTree";
|
||||
|
||||
/// <summary>
|
||||
/// 国家数据缓存
|
||||
|
||||
@@ -34,13 +34,39 @@ namespace Atomx.Data.CacheServices
|
||||
Task ResetArea(Area area);
|
||||
|
||||
/// <summary>
|
||||
/// 缓存
|
||||
/// 获取国家-省份-城市树形数据
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<KeyValueTree>> GetKeyValueTree();
|
||||
List<KeyValueTree> GetAreaTreeAsync(long countryId);
|
||||
}
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
public async Task<List<KeyValueTree>> GetAreaTreeAsync(long countryId, bool? reload = false)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<List<KeyValueTree>>(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<List<Area>> GetCountry(bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
36
Atomx.Utils/Extension/TreeExtension.cs
Normal file
36
Atomx.Utils/Extension/TreeExtension.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Atomx.Utils.Extension
|
||||
{
|
||||
public static class TreeExtension
|
||||
{
|
||||
public static List<T> ToTree<T>(this List<T> list, Func<T, string> getId, Func<T, string> getParentId, Action<T, List<T>> setChildren, string rootParentId = "0")
|
||||
{
|
||||
var lookup = new Dictionary<string, T>();
|
||||
var result = new List<T>();
|
||||
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<T>();
|
||||
setChildren(parent, children);
|
||||
children.Add(item);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user