public bool Set(ConfigType type, string code, string json) { if (json.IsNull()) { return(false); } var descriptor = GetDescriptor(m => m.Code.EqualsIgnoreCase(code) && m.Type == type); //旧实例 var oldInstance = descriptor.Instance; //新实例 descriptor.Instance = (IConfig)JsonConvert.DeserializeObject(json, descriptor.ImplementType); //持久化 _storageProvider.SaveJson(type, descriptor.Code, json); #region ==触发变更事件== foreach (var changeEventType in descriptor.ChangeEvents) { var eventInstance = _serviceProvider.GetService(changeEventType); var method = changeEventType.GetMethod("OnChanged"); if (method != null) { method.Invoke(eventInstance, new object[] { descriptor.Instance, oldInstance }); } } #endregion return(true); }
public bool Set(ConfigType type, string code, string json) { var descriptor = _configs.FirstOrDefault(m => m.Code.EqualsIgnoreCase(code) && m.Type == type); if (descriptor == null) { throw new NotImplementedException("没有找到配置类型"); } //持久化 if (_storageProvider.SaveJson(type, code, json).GetAwaiter().GetResult()) { var key = _redisHelper.GetKey($"{CACHE_KEY}:{descriptor.Type.ToString().ToUpper()}:{descriptor.Code.ToUpper()}"); var config = JsonConvert.DeserializeObject(json, descriptor.ImplementType); _redisHelper.Db.StringSetAsync(key, _redisSerializer.Serialize(config)).GetAwaiter().GetResult(); return(true); } return(false); }