Exemple #1
0
 public CacheHandle(
     CacheConfig cacheConfig)
 {
     CacheConfig = cacheConfig;
     Cache       = Utilities.CreateCache(
         cacheConfig.CacheId,
         cacheConfig.connectionOptions);
 }
Exemple #2
0
 internal static AsyncCircuitBreakerPolicy GetAsyncCircuitBreakerPolicy(
     RegionConfig regionConfig,
     CacheConfig cacheConfig,
     INHibernateLogger logger)
 {
     return(Policy
            .Handle <Alachisoft.NCache.Runtime.Exceptions.CacheException>(ex =>
                                                                          ex.ErrorCode ==
                                                                          NCacheErrorCodes.NO_SERVER_AVAILABLE)
            .CircuitBreakerAsync(
                exceptionsAllowedBeforeBreaking:
                regionConfig.CircuitBreakerExceptionsAllowed,
                durationOfBreak:
                TimeSpan.FromSeconds(
                    regionConfig.CircuitBreakerDurationOfBreak),
                onBreak:
                (ex, ts, ctx) =>
     {
         if (logger.IsDebugEnabled())
         {
             logger.Debug(
                 $"AsyncCircuitbreaker policy:\n" +
                 $"\tRegion: {ctx["region"]}" +
                 $"\tOperation: {ctx["operation"]}" +
                 $"\tCircuit broken for " +
                 $"{cacheConfig.CacheId}\n" +
                 $"\tReason:{ex.Message}");
         }
     },
                onReset: (ctx) =>
     {
         if (logger.IsDebugEnabled())
         {
             logger.Debug(
                 $"AsyncCircuitbreaker policy:\n" +
                 $"\tRegion: {ctx["region"]}" +
                 $"\tCircuit reset for {cacheConfig.CacheId}");
         }
     },
                onHalfOpen: () =>
     {
         if (logger.IsDebugEnabled())
         {
             logger.Debug(
                 $"AsyncCircuitbreaker policy:\n" +
                 $"\tCircuit half-open for" +
                 $" {cacheConfig.CacheId}");
         }
     }
                ));
 }
        private static void AddCacheConfig(
            XmlNode cacheConfigurationNode,
            ApplicationConfig applicationConfig)
        {
            CacheConfig cacheConfig = new CacheConfig();

            var regionPrefix =
                GetString(cacheConfigurationNode, "region-prefix", false);

            if (!string.IsNullOrWhiteSpace(regionPrefix))
            {
                cacheConfig.RegionPrefix = regionPrefix;
            }

            cacheConfig.CacheId =
                GetString(cacheConfigurationNode, "cache-name", true);

            if (applicationConfig.CacheConfigurations.ContainsKey(
                    cacheConfig.CacheConfigId))
            {
                throw new ConfigurationErrorsException(
                          $"Multiple cache configurations with identical cache config id " +
                          $"{cacheConfig.CacheConfigId} given");
            }

            var isDefault =
                GetBoolean(cacheConfigurationNode, "is-default", false);

            if (isDefault.HasValue)
            {
                cacheConfig.IsDefault = isDefault.Value;
            }

            cacheConfig.ConnectionTimeout =
                GetDouble(cacheConfigurationNode, "connection-timout");

            cacheConfig.KeepAliveInterval =
                GetDouble(cacheConfigurationNode, "keep-alive-interval");

            cacheConfig.EnableKeepAlive =
                GetBoolean(cacheConfigurationNode, "enable-keep-alive");

            cacheConfig.CommandRetryInterval =
                GetDouble(cacheConfigurationNode, "command-retry-interval");

            cacheConfig.CommandRetries =
                GetInteger(cacheConfigurationNode, "command-retries");

            cacheConfig.RetryConnectionDelay =
                GetDouble(cacheConfigurationNode, "retry-connection-delay");

            cacheConfig.RetryInterval =
                GetDouble(cacheConfigurationNode, "retry-interval");

            cacheConfig.ConnectionRetries =
                GetInteger(cacheConfigurationNode, "connection-retries");

            cacheConfig.EnableClientLogs =
                GetBoolean(cacheConfigurationNode, "enable-client-logs");

            cacheConfig.ClientRequestTimeout =
                GetDouble(cacheConfigurationNode, "client-request-timeout");

            cacheConfig.LoadBalance =
                GetBoolean(cacheConfigurationNode, "load-balance");

            cacheConfig.AppName =
                GetString(cacheConfigurationNode, "app-name");

            cacheConfig.ClientBindIP =
                GetString(cacheConfigurationNode, "client-bind-ip");

            cacheConfig.LogLevel =
                GetNCacheLogLevel(cacheConfigurationNode, "log-level");

            cacheConfig.Mode =
                GetNCacheIsolationLevel(cacheConfigurationNode, "mode");

            cacheConfig.SyncMode =
                GetNCacheClientSyncMode(cacheConfigurationNode, "sync-mode");

            var user =
                GetString(cacheConfigurationNode, "user");

            var password =
                GetString(cacheConfigurationNode, "password");

            if ((string.IsNullOrWhiteSpace(user) &&
                 !string.IsNullOrWhiteSpace(password)) ||
                (!string.IsNullOrWhiteSpace(user) &&
                 string.IsNullOrWhiteSpace(password)))
            {
                throw new ConfigurationErrorsException(
                          $"User name or password is not given with the other given");
            }
            else if (
                !string.IsNullOrWhiteSpace(user) &&
                !string.IsNullOrWhiteSpace(password))
            {
                cacheConfig.UserCredentials = new NCacheCredentials(user, password);
            }

            var serverNodesRoot =
                cacheConfigurationNode.SelectNodes("servers");

            if (serverNodesRoot.Count > 1)
            {
                throw new ConfigurationErrorsException(
                          $"A cache-configuration section can have only 1 servers section");
            }

            if (serverNodesRoot.Count == 1)
            {
                var serverNodes =
                    serverNodesRoot[0].SelectNodes("server");

                if (serverNodes.Count == 0)
                {
                    throw new ConfigurationErrorsException(
                              "servers subsection empty");
                }
                else
                {
                    Dictionary <string, int?> serverDict =
                        new Dictionary <string, int?>();

                    foreach (XmlNode serverNode in serverNodes)
                    {
                        var ipAddress =
                            GetString(serverNode, "server-ip", true);
                        var port =
                            GetInteger(serverNode, "port");

                        if (!serverDict.ContainsKey(ipAddress))
                        {
                            serverDict.Add(ipAddress, port);
                        }
                        else
                        {
                            throw new ConfigurationErrorsException(
                                      $"Multiple servers with identical ip addresses given.");
                        }
                    }

                    cacheConfig.ServerList = GetNCacheServerInfoList(serverDict);
                }
            }

            applicationConfig.CacheConfigurations.Add(
                cacheConfig.CacheConfigId,
                cacheConfig);
        }
Exemple #4
0
#pragma warning disable 618
        public ICache BuildCache(
            string regionName,
            IDictionary <string, string> properties)
#pragma warning restore 618
        {
            if (regionName == null)
            {
                regionName = String.Empty;
            }

            properties.TryGetValue(
                Cfg.Environment.CacheRegionPrefix,
                out var regionPrefix);

            var parsedRegionPrefix = string.IsNullOrWhiteSpace(regionPrefix) ?
                                     "nhibernate" :
                                     regionPrefix;

            RegionConfig regionConfig;


            if (applicationConfiguration.CacheRegions.ContainsKey(regionName))
            {
                regionConfig =
                    applicationConfiguration.CacheRegions[regionName];
            }
            else
            {
                regionConfig = BuildRegionConfiguration(
                    regionName,
                    parsedRegionPrefix);
            }

            var regionCacheId =
                regionConfig.CacheId;

            if (string.IsNullOrWhiteSpace(regionCacheId))
            {
                regionConfig.CacheId = GetDefaultCacheId(parsedRegionPrefix);
            }

            var regionCacheConfigId = $"{parsedRegionPrefix}:{regionConfig.CacheId}";;

            CacheConfig cacheConfig =
                applicationConfiguration.CacheConfigurations[regionCacheConfigId];

            if (!cacheHandles.ContainsKey(regionCacheConfigId))
            {
                lock (appConfigLocker)
                {
                    if (!cacheHandles.ContainsKey(regionCacheConfigId))
                    {
                        cacheHandles.Add(
                            regionCacheConfigId,
                            new CacheHandle(
                                cacheConfig));
                    }
                }
            }

            return(new NCacheClient(
                       cacheHandles[regionCacheConfigId],
                       parsedRegionPrefix,
                       regionConfig,
                       properties));
        }