コード例 #1
0
        public void Cfg_CreateConfig_CacheManagerHandleCollection()
        {
            // arrange act
            var col = new CacheManagerHandleCollection()
            {
                Name       = "name",
                UpdateMode = CacheUpdateMode.Up,
                EnablePerformanceCounters = true,
                EnableStatistics          = true,
                MaximumRetries            = 10012,
                RetryTimeout  = 234,
                BackplaneName = "backplane",
                BackplaneType = typeof(string).AssemblyQualifiedName
            };

            // assert
            col.Name.Should().Be("name");
            col.BackplaneName.Should().Be("backplane");
            col.BackplaneType.Should().Be(typeof(string).AssemblyQualifiedName);
            col.UpdateMode.Should().Be(CacheUpdateMode.Up);
            col.EnablePerformanceCounters.Should().BeTrue();
            col.EnableStatistics.Should().BeTrue();
            col.MaximumRetries.Should().Be(10012);
            col.RetryTimeout.Should().Be(234);
        }
コード例 #2
0
        public void Cfg_CreateConfig_CacheManagerHandleCollection()
        {
            // arrange act
            var col = new CacheManagerHandleCollection()
            {
                Name = "name",
                UpdateMode = CacheUpdateMode.Up,
                EnablePerformanceCounters = true,
                EnableStatistics = true,
                MaximumRetries = 10012,
                RetryTimeout = 234,
                BackplaneName = "backplane",
                BackplaneType = typeof(string).AssemblyQualifiedName
            };

            // assert
            col.Name.Should().Be("name");
            col.BackplaneName.Should().Be("backplane");
            col.BackplaneType.Should().Be(typeof(string).AssemblyQualifiedName);
            col.UpdateMode.Should().Be(CacheUpdateMode.Up);
            col.EnablePerformanceCounters.Should().BeTrue();
            col.EnableStatistics.Should().BeTrue();
            col.MaximumRetries.Should().Be(10012);
            col.RetryTimeout.Should().Be(234);
        }
コード例 #3
0
        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(managerCfg.UpdateMode, maxRetries.HasValue ? maxRetries.Value : int.MaxValue, retryTimeout.HasValue ? retryTimeout.Value : 10);

            if (string.IsNullOrWhiteSpace(managerCfg.BackPlateType))
            {
                if (!string.IsNullOrWhiteSpace(managerCfg.BackPlateName))
                {
                    throw new InvalidOperationException("BackPlateType cannot be null if BackPlateName is specified.");
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(managerCfg.BackPlateName))
                {
                    throw new InvalidOperationException("BackPlateName cannot be null if BackPlateType is specified.");
                }

                cfg.WithBackPlate(
                    Type.GetType(managerCfg.BackPlateType, true),
                    managerCfg.BackPlateName);
            }

            // build serializer if set
            if (!string.IsNullOrWhiteSpace(managerCfg.SerializerType))
            {
                var serializerType = Type.GetType(managerCfg.SerializerType, false);
                EnsureNotNull(serializerType, "Serializer type cannot be loaded, {0}", managerCfg.SerializerType);

                cfg.WithSerializer(CacheReflectionHelper.CreateSerializer(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,
                    IsBackPlateSource         = handleItem.IsBackPlateSource
                };

                // 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.HandleName));
                }

                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);
        }