add areas cache
This commit is contained in:
@@ -90,7 +90,10 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public const string ContentCategories = "Content.Categories";
|
public const string ContentCategories = "Content.Categories";
|
||||||
|
|
||||||
public const string Area = "Area";
|
/// <summary>
|
||||||
|
/// 国家数据缓存树形结构
|
||||||
|
/// </summary>
|
||||||
|
public const string CountryTree = "Area.CountryTree";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 国家数据缓存
|
/// 国家数据缓存
|
||||||
|
|||||||
@@ -34,26 +34,52 @@ namespace Atomx.Data.CacheServices
|
|||||||
Task ResetArea(Area area);
|
Task ResetArea(Area area);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 缓存
|
/// 获取国家-省份-城市树形数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="countryId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<List<KeyValueTree>> GetKeyValueTree();
|
List<KeyValueTree> GetAreaTreeAsync(long countryId);
|
||||||
}
|
}
|
||||||
public partial class CacheService : ICacheService
|
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)
|
public async Task<List<Area>> GetCountry(bool? reload = false)
|
||||||
{
|
{
|
||||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||||
|
|
||||||
var cacheData = await GetCacheAsync<List<Area>>(CacheKeys.Country);
|
var cacheData = await GetCacheAsync<List<Area>>(CacheKeys.Country);
|
||||||
if(cacheData == null || reloadData)
|
if (cacheData == null || reloadData)
|
||||||
{
|
{
|
||||||
var countries = (from p in _dbContext.Areas
|
var countries = (from p in _dbContext.Areas
|
||||||
where p.ParentId == 0
|
where p.ParentId == 0
|
||||||
select p).ToList();
|
select p).ToList();
|
||||||
await SetCacheAsync(CacheKeys.Country, countries);
|
await SetCacheAsync(CacheKeys.Country, countries);
|
||||||
return countries;
|
return countries;
|
||||||
|
|
||||||
}
|
}
|
||||||
return cacheData;
|
return cacheData;
|
||||||
}
|
}
|
||||||
@@ -80,8 +106,8 @@ namespace Atomx.Data.CacheServices
|
|||||||
if (cacheData == null || reloadData)
|
if (cacheData == null || reloadData)
|
||||||
{
|
{
|
||||||
var data = (from p in _dbContext.Areas
|
var data = (from p in _dbContext.Areas
|
||||||
where p.ParentId == 0
|
where p.ParentId == 0
|
||||||
select p).ToList();
|
select p).ToList();
|
||||||
await SetCacheAsync(CacheKeys.Country, data);
|
await SetCacheAsync(CacheKeys.Country, data);
|
||||||
return data;
|
return data;
|
||||||
|
|
||||||
|
|||||||
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