add areas cache
This commit is contained in:
@@ -34,26 +34,52 @@ 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;
|
||||
|
||||
var cacheData = await GetCacheAsync<List<Area>>(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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user