예제 #1
0
        public async Task <T> Resolve <T>() where T : IConfig, new()
        {
            var type     = typeof(T);
            var cacheKey = CacheKeyPrefix + type.FullName;

            if (!_cacheHandler.TryGetValue(cacheKey, out T config))
            {
                config = new T();

                var list = await _storage.GetAll();

                if (list != null && list.Any())
                {
                    var properties = type.GetProperties();
                    foreach (var property in properties)
                    {
                        var key = type.FullName + "." + property.Name;
                        var configDescriptor = list.FirstOrDefault(m => m.Key.EqualsIgnoreCase(key));
                        if (configDescriptor != null)
                        {
                            property.SetValue(config, Convert.ChangeType(configDescriptor.Value, property.PropertyType));
                        }
                        CacheDic.TryAdd(key, cacheKey);
                    }
                }
                //在判断一次缓存
                if (!await _cacheHandler.ExistsAsync(cacheKey))
                {
                    await _cacheHandler.SetAsync(cacheKey, config);
                }
            }

            return(config);
        }