public void Init() { #if NETCOREAPP3_1 //初始化配置文件 Assembly entryAssembly = Assembly.GetExecutingAssembly(); Configuration configuration = ConfigurationExtension.GetConfigurationFromAssembly(entryAssembly); CacheManagerSection.Initialize(configuration); RedisSection.Initialize(configuration); #endif }
public void Refresh() { if (DataConfigHelper.CacheConfigLoader != null) { DataConfigHelper.CacheConfigLoader.Reload(); } Dictionary <string, ICacheManager <object> > mgrs = new Dictionary <string, ICacheManager <object> >(); CacheManagerSection section = ConfigurationManager.GetSection(CACHE_SECTION_NAME) as CacheManagerSection; if (section != null && section.CacheManagers != null) { foreach (var item in section.CacheManagers) { ICacheManagerConfiguration cfg = null; if (DataConfigHelper.CacheConfigLoader != null) { cfg = DataConfigHelper.CacheConfigLoader.GetCacheConfig(item.Name); } if (mgrs.ContainsKey(item.Name)) { mgrs.Remove(item.Name); } var cache = cfg == null?CacheFactory.FromConfiguration <object>(item.Name) : CacheFactory.FromConfiguration <object>(item.Name, cfg); if (cache != null) { mgrs.Add(item.Name, cache); } } } m_Mgrs = mgrs; // thread-safe (reads and writes of reference types are atomic) }
internal static CacheManagerConfiguration LoadFromSection(CacheManagerSection section, string configName) { NotNullOrWhiteSpace(configName, nameof(configName)); var handleDefsSection = section.CacheHandleDefinitions; Ensure(handleDefsSection.Count > 0, "There are no cache handles defined."); // load handle definitions as lookup var handleDefs = new SortedList<string, CacheHandleConfiguration>(); foreach (CacheHandleDefinition def in handleDefsSection) { //// don't validate at this point, otherwise we will get an exception if any defined handle doesn't match with the requested type... //// CacheReflectionHelper.ValidateCacheHandleGenericTypeArguments(def.HandleType, cacheValue); var normId = def.Id.ToUpper(CultureInfo.InvariantCulture); handleDefs.Add( normId, new CacheHandleConfiguration(def.Id) { HandleType = def.HandleType, ExpirationMode = def.DefaultExpirationMode, ExpirationTimeout = GetTimeSpan(def.DefaultTimeout, "defaultTimeout") }); } // retrieve the handles collection with the correct name CacheManagerHandleCollection managerCfg = section.CacheManagers.FirstOrDefault(p => p.Name.Equals(configName, StringComparison.OrdinalIgnoreCase)); EnsureNotNull(managerCfg, "No cache manager configuration found for name [{0}]", configName); int? maxRetries = managerCfg.MaximumRetries; if (maxRetries.HasValue && maxRetries.Value <= 0) { throw new InvalidOperationException("Maximum number of retries must be greater than zero."); } int? retryTimeout = managerCfg.RetryTimeout; if (retryTimeout.HasValue && retryTimeout.Value < 0) { throw new InvalidOperationException("Retry timeout must be greater than or equal to zero."); } // build configuration var cfg = new CacheManagerConfiguration() { UpdateMode = managerCfg.UpdateMode, MaxRetries = maxRetries.HasValue ? maxRetries.Value : 50, RetryTimeout = retryTimeout.HasValue ? retryTimeout.Value : 100 }; if (string.IsNullOrWhiteSpace(managerCfg.BackplaneType)) { if (!string.IsNullOrWhiteSpace(managerCfg.BackplaneName)) { throw new InvalidOperationException("Backplane type cannot be null if backplane name is specified."); } } else { if (string.IsNullOrWhiteSpace(managerCfg.BackplaneName)) { throw new InvalidOperationException("Backplane name cannot be null if backplane type is specified."); } var backplaneType = Type.GetType(managerCfg.BackplaneType, false); EnsureNotNull(backplaneType, "Backplane type not found, '{0}'. Make sure to install the corresponding nuget package.", managerCfg.BackplaneType); cfg.BackplaneType = backplaneType; cfg.BackplaneConfigurationKey = managerCfg.BackplaneName; } // build serializer if set if (!string.IsNullOrWhiteSpace(managerCfg.SerializerType)) { var serializerType = Type.GetType(managerCfg.SerializerType, false); EnsureNotNull(serializerType, "Serializer type not found, {0}.", managerCfg.SerializerType); cfg.SerializerType = serializerType; } foreach (CacheManagerHandle handleItem in managerCfg) { var normRefId = handleItem.RefHandleId.ToUpper(CultureInfo.InvariantCulture); Ensure( handleDefs.ContainsKey(normRefId), "Referenced cache handle [{0}] cannot be found in cache handles definition.", handleItem.RefHandleId); var handleDef = handleDefs[normRefId]; var handle = new CacheHandleConfiguration(handleItem.Name) { HandleType = handleDef.HandleType, ExpirationMode = handleDef.ExpirationMode, ExpirationTimeout = handleDef.ExpirationTimeout, EnableStatistics = managerCfg.EnableStatistics, EnablePerformanceCounters = managerCfg.EnablePerformanceCounters, IsBackplaneSource = handleItem.IsBackplaneSource }; // override default timeout if it is defined in this section. if (!string.IsNullOrWhiteSpace(handleItem.Timeout)) { handle.ExpirationTimeout = GetTimeSpan(handleItem.Timeout, "timeout"); } // override default expiration mode if it is defined in this section. if (!string.IsNullOrWhiteSpace(handleItem.ExpirationMode)) { try { handle.ExpirationMode = (ExpirationMode)Enum.Parse(typeof(ExpirationMode), handleItem.ExpirationMode); } catch (ArgumentException ex) { throw new InvalidOperationException("Invalid value '" + handleItem.ExpirationMode + "'for expiration mode", ex); } } if (handle.ExpirationMode != ExpirationMode.None && handle.ExpirationTimeout == TimeSpan.Zero) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, "Expiration mode set without a valid timeout specified for handle [{0}]", handle.Name)); } cfg.CacheHandleConfigurations.Add(handle); } Ensure(cfg.CacheHandleConfigurations.Count > 0, "There are no valid cache handles linked to the cache manager configuration [{0}]", configName); return cfg; }
internal static CacheManagerConfiguration LoadFromSection(CacheManagerSection section, string configName) { NotNullOrWhiteSpace(configName, nameof(configName)); var handleDefsSection = section.CacheHandleDefinitions; Ensure(handleDefsSection.Count > 0, "There are no cache handles defined."); // load handle definitions as lookup var handleDefs = new SortedList <string, CacheHandleConfiguration>(); foreach (var def in handleDefsSection) { //// don't validate at this point, otherwise we will get an exception if any defined handle doesn't match with the requested type... //// CacheReflectionHelper.ValidateCacheHandleGenericTypeArguments(def.HandleType, cacheValue); var normId = def.Id.ToUpper(CultureInfo.InvariantCulture); handleDefs.Add( normId, new CacheHandleConfiguration(def.Id) { HandleType = def.HandleType, ExpirationMode = def.DefaultExpirationMode, ExpirationTimeout = GetTimeSpan(def.DefaultTimeout, "defaultTimeout") }); } // retrieve the handles collection with the correct name var managerCfg = section.CacheManagers.FirstOrDefault(p => p.Name.Equals(configName, StringComparison.OrdinalIgnoreCase)); EnsureNotNull(managerCfg, "No cache manager configuration found for name [{0}]", configName); var maxRetries = managerCfg.MaximumRetries; if (maxRetries.HasValue && maxRetries.Value <= 0) { throw new InvalidOperationException("Maximum number of retries must be greater than zero."); } var retryTimeout = managerCfg.RetryTimeout; if (retryTimeout.HasValue && retryTimeout.Value < 0) { throw new InvalidOperationException("Retry timeout must be greater than or equal to zero."); } // build configuration var cfg = new CacheManagerConfiguration() { UpdateMode = managerCfg.UpdateMode, MaxRetries = maxRetries ?? 50, RetryTimeout = retryTimeout ?? 100 }; if (string.IsNullOrWhiteSpace(managerCfg.BackplaneType)) { if (!string.IsNullOrWhiteSpace(managerCfg.BackplaneName)) { throw new InvalidOperationException("Backplane type cannot be null if backplane name is specified."); } } else { if (string.IsNullOrWhiteSpace(managerCfg.BackplaneName)) { throw new InvalidOperationException("Backplane name cannot be null if backplane type is specified."); } var backplaneType = Type.GetType(managerCfg.BackplaneType, false); EnsureNotNull(backplaneType, "Backplane type not found, '{0}'. Make sure to install the corresponding nuget package.", managerCfg.BackplaneType); cfg.BackplaneType = backplaneType; cfg.BackplaneConfigurationKey = managerCfg.BackplaneName; } // build serializer if set if (!string.IsNullOrWhiteSpace(managerCfg.SerializerType)) { var serializerType = Type.GetType(managerCfg.SerializerType, false); EnsureNotNull(serializerType, "Serializer type not found, {0}.", managerCfg.SerializerType); cfg.SerializerType = serializerType; } foreach (var handleItem in managerCfg) { var normRefId = handleItem.RefHandleId.ToUpper(CultureInfo.InvariantCulture); Ensure( handleDefs.ContainsKey(normRefId), "Referenced cache handle [{0}] cannot be found in cache handles definition.", handleItem.RefHandleId); var handleDef = handleDefs[normRefId]; var handle = new CacheHandleConfiguration(handleItem.Name) { HandleType = handleDef.HandleType, ExpirationMode = handleDef.ExpirationMode, ExpirationTimeout = handleDef.ExpirationTimeout, EnableStatistics = managerCfg.EnableStatistics, EnablePerformanceCounters = managerCfg.EnablePerformanceCounters, IsBackplaneSource = handleItem.IsBackplaneSource }; // override default timeout if it is defined in this section. if (!string.IsNullOrWhiteSpace(handleItem.Timeout)) { handle.ExpirationTimeout = GetTimeSpan(handleItem.Timeout, "timeout"); } // override default expiration mode if it is defined in this section. if (!string.IsNullOrWhiteSpace(handleItem.ExpirationMode)) { try { handle.ExpirationMode = (ExpirationMode)Enum.Parse(typeof(ExpirationMode), handleItem.ExpirationMode); } catch (ArgumentException ex) { throw new InvalidOperationException("Invalid value '" + handleItem.ExpirationMode + "'for expiration mode", ex); } } if (handle.ExpirationMode != ExpirationMode.None && handle.ExpirationTimeout == TimeSpan.Zero) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, "Expiration mode set without a valid timeout specified for handle [{0}]", handle.Name)); } cfg.CacheHandleConfigurations.Add(handle); } Ensure(cfg.CacheHandleConfigurations.Count > 0, "There are no valid cache handles linked to the cache manager configuration [{0}]", configName); return(cfg); }