예제 #1
0
		private void InitFromConfiguration(System.Configuration.Configuration configuration)
		{
			if (configuration != null)
			{
				CacheSettingsSection section = (CacheSettingsSection)configuration.GetSection("cacheSettings");

				if (section != null)
				{
					this.DefaultCacheName = section.DefaultCache.Name;
					this.EnableDistributeCache = section.EnableDistributeCache.Value;

					if (!String.IsNullOrEmpty(section.Failover.RetryingInterval))
					{
						this.FailoverRetryingInterval = TimeSpan.Parse(section.Failover.RetryingInterval);
					}

					if (!String.IsNullOrEmpty(section.Failover.ToLocalCache))
					{
						this.FailoverToLocalCache = Boolean.Parse(section.Failover.ToLocalCache);
					}

					for (int i = 0; i < section.Caches.Count; i++)
					{
						bool isAbsoluteLocalCache = section.Caches[i].CacheName.ToLower().Equals("local");

						CacheSetting cacheSetting = new CacheSetting();
						CachePosition cachePosition = isAbsoluteLocalCache ? CachePosition.Local : (CachePosition)Enum.Parse(typeof(CachePosition), section.Caches[i].Position, true);
						if (cachePosition == CachePosition.Inherit)
						{
							cachePosition = this.defaultPosition;
						}
						cacheSetting.Position = cachePosition;
						if (!String.IsNullOrEmpty(section.Caches[i].DependencyFile))
						{
							cacheSetting.DependencyFile = AppDomain.CurrentDomain.MapPhysicalPath("conf/", section.Caches[i].DependencyFile);
						}

						cacheSetting.Capacity = String.IsNullOrEmpty(section.Caches[i].Capacity) ? defaultCacheCapacity : Int32.Parse(section.Caches[i].Capacity);

						cacheSetting.AsyncUpdateInterval = String.IsNullOrEmpty(section.Caches[i].AsyncUpdateInterval) ? defaultAsyncUpdateInterval : (int)TimeSpan.Parse(section.Caches[i].AsyncUpdateInterval).TotalSeconds;

						RegionElementCollection regionElements = section.Caches[i].Regions;

						if (regionElements.Count > 0)
						{
							for (int j = 0; j < regionElements.Count; j++)
							{
								RegionSetting regionSetting = new RegionSetting();
								CachePosition regionPosition = isAbsoluteLocalCache ? CachePosition.Local : (CachePosition)Enum.Parse(typeof(CachePosition), regionElements[j].Position, true);
								if (regionPosition == CachePosition.Inherit)
								{
									regionPosition = cachePosition;
								}

								regionSetting.Position = regionPosition;

								regionSetting.DependencyFile = String.IsNullOrEmpty(regionElements[j].DependencyFile) ? cacheSetting.DependencyFile : AppDomain.CurrentDomain.MapPhysicalPath("conf/", regionElements[j].DependencyFile);
								regionSetting.Capacity = String.IsNullOrEmpty(regionElements[j].Capacity) ? cacheSetting.Capacity : Int32.Parse(regionElements[j].Capacity);
								regionSetting.AsyncUpdateInterval = String.IsNullOrEmpty(regionElements[j].AsyncUpdateInterval) ? cacheSetting.AsyncUpdateInterval : (int)TimeSpan.Parse(regionElements[j].AsyncUpdateInterval).TotalSeconds;

								cacheSetting.Regions.Add(regionElements[j].RegionName, regionSetting);
							}
						}

						this.Caches.Add(section.Caches[i].CacheName, cacheSetting);
					}

				}
			}

			if (!this.Caches.ContainsKey(this.DefaultCacheName))
			{
				this.Caches.Add(this.DefaultCacheName, new CacheSetting(this.defaultPosition, this.defaultCacheCapacity, this.defaultAsyncUpdateInterval));
			}

			if (!this.Caches.ContainsKey("local"))
			{
				this.Caches.Add("local", new CacheSetting(CachePosition.Local, this.defaultCacheCapacity, this.defaultAsyncUpdateInterval));
			}
		}
예제 #2
0
		private void InitFromConfiguration(System.Configuration.Configuration configuration)
		{
			if (configuration != null)
			{
				CacheSettingsSection section = (CacheSettingsSection)configuration.GetSection("cacheSettings");

				if (section != null)
				{
					if (section.CacheVersion != null)
					{
						if (!String.IsNullOrEmpty(section.CacheVersion.Value))
						{
							this.DistributeCacheName = RunContext.AppName + "_" + RunContext.AppVersion + "_" + section.CacheVersion.Value;
						}
					}

					if (section.LocalCache != null)
					{
						this.localCacheSetting.Position = CachePosition.Local;

						this.localCacheSetting.Capacity = String.IsNullOrEmpty(section.LocalCache.Capacity) ? defaultCacheCapacity : Int32.Parse(section.LocalCache.Capacity);
						this.localCacheSetting.AsyncUpdateInterval = String.IsNullOrEmpty(section.LocalCache.AsyncUpdateInterval) ? defaultAsyncUpdateInterval : TimeSpan.Parse(section.LocalCache.AsyncUpdateInterval);
						if (!String.IsNullOrEmpty(section.LocalCache.DependencyFile))
						{
							this.localCacheSetting.DependencyFile = AppDomain.CurrentDomain.MapPhysicalPath("conf/", section.LocalCache.DependencyFile);
						}

						for (int i = 0; i < section.LocalCache.Regions.Count; i++)
						{
							RegionSetting regionSetting = new RegionSetting();

							if (String.IsNullOrEmpty(section.LocalCache.Regions[i].RegionName))
							{
								throw new ConfigurationErrorsException("分区名称不能为空。",
									section.LocalCache.Regions[i].ElementInformation.Source,
									section.LocalCache.Regions[i].ElementInformation.LineNumber);
							}

							try
							{
								regionSetting.Position = CachePosition.Local;

								regionSetting.Capacity = String.IsNullOrEmpty(section.LocalCache.Regions[i].Capacity) ? this.localCacheSetting.Capacity : Int32.Parse(section.LocalCache.Regions[i].Capacity);

								regionSetting.AsyncUpdateInterval = String.IsNullOrEmpty(section.LocalCache.Regions[i].AsyncUpdateInterval) ? this.localCacheSetting.AsyncUpdateInterval : TimeSpan.Parse(section.LocalCache.Regions[i].AsyncUpdateInterval);
								regionSetting.AsyncTimeToLive = String.IsNullOrEmpty(section.LocalCache.Regions[i].AsyncTimeToLive) ? this.localCacheSetting.AsyncTimeToLive : TimeSpan.Parse(section.LocalCache.Regions[i].AsyncTimeToLive);

								regionSetting.DependencyFile = String.IsNullOrEmpty(section.LocalCache.Regions[i].DependencyFile) ? this.localCacheSetting.DependencyFile : AppDomain.CurrentDomain.MapPhysicalPath("conf/", section.LocalCache.Regions[i].DependencyFile);
							}
							catch (Exception err)
							{
								throw new ConfigurationErrorsException(String.Format("名称为 {0} 的缓存分区配置不正确。", section.LocalCache.Regions[i].RegionName), err,
									section.LocalCache.Regions[i].ElementInformation.Source,
									section.LocalCache.Regions[i].ElementInformation.LineNumber);
							}

							this.localCacheSetting.Regions.Add(section.LocalCache.Regions[i].RegionName, regionSetting);
						}
					}

					if (section.DistributeCache != null)
					{
						this.distributeCacheSetting.EnableDistributeCache = section.DistributeCache.EnableDistributeCache.Value;

						if (section.DistributeCache.PerformanceMonitor != null)
						{
							this.distributeCacheSetting.PerformanceMonitor = new PerformanceMonitorSetting(
								section.DistributeCache.PerformanceMonitor.Enabled,
								section.DistributeCache.PerformanceMonitor.BatchCount,
								section.DistributeCache.PerformanceMonitor.TraceThreshold
								);
						}
						else
						{
							this.distributeCacheSetting.PerformanceMonitor = PerformanceMonitorSetting.Default;
						}

						this.distributeCacheSetting.FailoverToLocalCache = String.IsNullOrEmpty(section.DistributeCache.Failover.ToLocalCache) ? defaultFailoverToLocalCache : section.DistributeCache.Failover.ToLocalCache == "true";
						this.distributeCacheSetting.FailoverRetryingInterval = String.IsNullOrEmpty(section.DistributeCache.Failover.RetryingInterval) ? defaultFailoverRetryingInterval : TimeSpan.Parse(section.DistributeCache.Failover.RetryingInterval);

						this.distributeCacheSetting.Providers = new DistributeCacheProviderCollection();

						// System.Web.Configuration.ProvidersHelper.InstantiateProviders(section.Providers, this.providers, typeof(DistributeCacheProvider));
						InstantiateProviders(section.DistributeCache.Providers, this.distributeCacheSetting.Providers, this);

						// 未配置 providers 和 DefaultDistributeCacheProvider 时使用默认提供程序,这允许配置文件中不需要配置 distributeCacheProviders 和 defaultDistributeCacheProvider 配置项
						if (this.distributeCacheSetting.Providers.Count == 0)
						{
							this.distributeCacheSetting.DefaultDistributeCacheProvider = XMS.Core.Caching.Memcached.MemcachedDistributeCacheProvider.CreateDefaultProvider(configuration);
							this.distributeCacheSetting.DefaultDistributeCacheProvider.cacheSettings = this;
							this.distributeCacheSetting.Providers.Add(this.distributeCacheSetting.DefaultDistributeCacheProvider);
						}
						else
						{
							this.distributeCacheSetting.DefaultDistributeCacheProvider = this.distributeCacheSetting.Providers[section.DistributeCache.DefaultDistributeCacheProvider.Name];

							if (this.distributeCacheSetting.DefaultDistributeCacheProvider == null)
							{
								throw new ConfigurationErrorsException(String.Format("未找到名称为 {0} 的分布式缓存提供程序。", section.DistributeCache.DefaultDistributeCacheProvider.Name),
									section.DistributeCache.DefaultDistributeCacheProvider.ElementInformation.Source,
									section.DistributeCache.DefaultDistributeCacheProvider.ElementInformation.Properties[DefaultDistributeCacheProviderElement.Property_Name].LineNumber);
							}
						}

						CachePosition cachePosition = (CachePosition)Enum.Parse(typeof(CachePosition), section.DistributeCache.Position, true);
						if (cachePosition == CachePosition.Inherit)
						{
							cachePosition = defaultPosition;
						}

						this.distributeCacheSetting.Position = cachePosition;

						this.distributeCacheSetting.Capacity = String.IsNullOrEmpty(section.DistributeCache.Capacity) ? defaultCacheCapacity : Int32.Parse(section.DistributeCache.Capacity);
						this.distributeCacheSetting.AsyncUpdateInterval = String.IsNullOrEmpty(section.DistributeCache.AsyncUpdateInterval) ? defaultAsyncUpdateInterval : TimeSpan.Parse(section.DistributeCache.AsyncUpdateInterval);
						if (!String.IsNullOrEmpty(section.DistributeCache.DependencyFile))
						{
							this.distributeCacheSetting.DependencyFile = AppDomain.CurrentDomain.MapPhysicalPath("conf/", section.DistributeCache.DependencyFile);
						}

						for (int i = 0; i < section.DistributeCache.Regions.Count; i++)
						{
							RegionSetting regionSetting = new RegionSetting();

							if (String.IsNullOrEmpty(section.DistributeCache.Regions[i].RegionName))
							{
								throw new ConfigurationErrorsException("分区名称不能为空。",
									section.DistributeCache.Regions[i].ElementInformation.Source,
									section.DistributeCache.Regions[i].ElementInformation.LineNumber);
							}

							try
							{
								CachePosition regionPosition = (CachePosition)Enum.Parse(typeof(CachePosition), section.DistributeCache.Regions[i].Position, true);
								if (regionPosition == CachePosition.Inherit)
								{
									regionPosition = this.distributeCacheSetting.Position;
								}

								regionSetting.Position = regionPosition;

								regionSetting.Capacity = String.IsNullOrEmpty(section.DistributeCache.Regions[i].Capacity) ? this.distributeCacheSetting.Capacity : Int32.Parse(section.DistributeCache.Regions[i].Capacity);

								regionSetting.AsyncUpdateInterval = String.IsNullOrEmpty(section.DistributeCache.Regions[i].AsyncUpdateInterval) ? this.distributeCacheSetting.AsyncUpdateInterval : TimeSpan.Parse(section.DistributeCache.Regions[i].AsyncUpdateInterval);
								regionSetting.AsyncTimeToLive = String.IsNullOrEmpty(section.DistributeCache.Regions[i].AsyncTimeToLive) ? this.distributeCacheSetting.AsyncTimeToLive : TimeSpan.Parse(section.DistributeCache.Regions[i].AsyncTimeToLive);

								regionSetting.DependencyFile = String.IsNullOrEmpty(section.DistributeCache.Regions[i].DependencyFile) ? this.distributeCacheSetting.DependencyFile : AppDomain.CurrentDomain.MapPhysicalPath("conf/", section.DistributeCache.Regions[i].DependencyFile);
							}
							catch (Exception err)
							{
								throw new ConfigurationErrorsException(String.Format("名称为 {0} 的缓存分区配置不正确。", section.DistributeCache.Regions[i].RegionName), err,
		section.DistributeCache.Regions[i].ElementInformation.Source,
		section.DistributeCache.Regions[i].ElementInformation.LineNumber);
							}

							this.distributeCacheSetting.Regions.Add(section.DistributeCache.Regions[i].RegionName, regionSetting);
						}
					}

				}
			}
		}