|
|
|
@ -118,9 +118,96 @@ namespace Volo.Abp.SettingManagement |
|
|
|
await Cache.SetManyAsync(cacheItems, considerUow: true); |
|
|
|
} |
|
|
|
|
|
|
|
[UnitOfWork] |
|
|
|
public async Task<List<SettingValue>> GetListAsync(string[] names, string providerName, string providerKey) |
|
|
|
{ |
|
|
|
Check.NotNullOrEmpty(names, nameof(names)); |
|
|
|
|
|
|
|
var result = new List<SettingValue>(); |
|
|
|
|
|
|
|
if (names.Length == 1) |
|
|
|
{ |
|
|
|
var name = names.First(); |
|
|
|
result.Add(new SettingValue(name, (await GetCacheItemAsync(name, providerName, providerKey)).Value)); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
var cacheItems = await GetCacheItemsAsync(names, providerName, providerKey); |
|
|
|
foreach (var item in cacheItems) |
|
|
|
{ |
|
|
|
result.Add(new SettingValue(GetSettingNameFormCacheKeyOrNull(item.Key), item.Value?.Value)); |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual async Task<List<KeyValuePair<string, SettingCacheItem>>> GetCacheItemsAsync(string[] names, string providerName, string providerKey) |
|
|
|
{ |
|
|
|
var cacheKeys = names.Select(x => CalculateCacheKey(x, providerName, providerKey)).ToList(); |
|
|
|
|
|
|
|
var cacheItems = (await Cache.GetManyAsync(cacheKeys, considerUow: true)).ToList(); |
|
|
|
|
|
|
|
if (cacheItems.All(x => x.Value != null)) |
|
|
|
{ |
|
|
|
return cacheItems; |
|
|
|
} |
|
|
|
|
|
|
|
var notCacheKeys = cacheItems.Where(x => x.Value == null).Select(x => x.Key).ToList(); |
|
|
|
|
|
|
|
var newCacheItems = await SetCacheItemsAsync(providerName, providerKey, notCacheKeys); |
|
|
|
|
|
|
|
var result = new List<KeyValuePair<string, SettingCacheItem>>(); |
|
|
|
foreach (var key in cacheKeys) |
|
|
|
{ |
|
|
|
var item = newCacheItems.FirstOrDefault(x => x.Key == key); |
|
|
|
if (item.Value == null) |
|
|
|
{ |
|
|
|
item = cacheItems.FirstOrDefault(x => x.Key == key); |
|
|
|
} |
|
|
|
|
|
|
|
result.Add(new KeyValuePair<string, SettingCacheItem>(key, item.Value)); |
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<List<KeyValuePair<string, SettingCacheItem>>> SetCacheItemsAsync( |
|
|
|
string providerName, |
|
|
|
string providerKey, |
|
|
|
IEnumerable<string> cacheKeys) |
|
|
|
{ |
|
|
|
var settingDefinitions = SettingDefinitionManager.GetAll(); |
|
|
|
var settingsDictionary = (await SettingRepository.GetListAsync(providerName, providerKey)) |
|
|
|
.ToDictionary(s => s.Name, s => s.Value); |
|
|
|
|
|
|
|
var cacheItems = new List<KeyValuePair<string, SettingCacheItem>>(); |
|
|
|
|
|
|
|
foreach (var settingDefinition in settingDefinitions) |
|
|
|
{ |
|
|
|
var settingValue = settingsDictionary.GetOrDefault(settingDefinition.Name); |
|
|
|
cacheItems.Add( |
|
|
|
new KeyValuePair<string, SettingCacheItem>( |
|
|
|
CalculateCacheKey(settingDefinition.Name, providerName, providerKey), |
|
|
|
new SettingCacheItem(settingValue) |
|
|
|
) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
await Cache.SetManyAsync(cacheItems, considerUow: true); |
|
|
|
|
|
|
|
return cacheItems.Where(x => cacheKeys.Contains(x.Key)).ToList(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected virtual string CalculateCacheKey(string name, string providerName, string providerKey) |
|
|
|
{ |
|
|
|
return SettingCacheItem.CalculateCacheKey(name, providerName, providerKey); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual string GetSettingNameFormCacheKeyOrNull(string key) |
|
|
|
{ |
|
|
|
//TODO: throw ex when name is null?
|
|
|
|
return SettingCacheItem.GetSettingNameFormCacheKey(key); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|