public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (name == null || name.Length == 0)
            {
                name = "MyCacheStore";
            }

            if (String.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "Redis as a session data store");
            }
            base.Initialize(name, config);
            
            // If configuration exists then use it otherwise read from config file and create one
            if (configuration == null)
            {
                lock (configurationCreationLock)
                {
                    if (configuration == null)
                    {
                        configuration = ProviderConfiguration.ProviderConfigurationForOutputCache(config);
                    }
                }
            }
        }
 public StackExchangeClientConnection(ProviderConfiguration configuration)
 {
     _configuration = configuration;
     _configOption = ParseConfiguration(_configuration);
     if (!string.IsNullOrEmpty(_configOption.ServiceName))
     {
       _sentinelConfiguration = CreateSentinellConfiguration(_configOption);
       ModifyEndpointsForSentinelConfiguration(_configOption);
     }
     ConnectToRedis(_configuration);
 }
 public StackExchangeClientConnection(ProviderConfiguration configuration)
 {
     _configuration = configuration;
     _configOption  = ParseConfiguration(_configuration);
     if (!string.IsNullOrEmpty(_configOption.ServiceName))
     {
         _sentinelConfiguration = CreateSentinellConfiguration(_configOption);
         ModifyEndpointsForSentinelConfiguration(_configOption);
     }
     ConnectToRedis(_configuration);
     this.redisUtility  = new RedisUtility(configuration);
     this.configuration = configuration;
     ConfigurationOptions configOption;
 }
        public StackExchangeClientConnection(ProviderConfiguration configuration)
        {
            this.configuration = configuration;
            this.redisUtility = new RedisUtility(configuration);
            ConfigurationOptions configOption;

            // If connection string is given then use it otherwise use individual options
            if (!string.IsNullOrEmpty(configuration.ConnectionString))
            {
                configOption = ConfigurationOptions.Parse(configuration.ConnectionString);
                // Setting explicitly 'abortconnect' to false. It will overwrite customer provided value for 'abortconnect'
                // As it doesn't make sense to allow to customer to set it to true as we don't give them access to ConnectionMultiplexer
                // in case of failure customer can not create ConnectionMultiplexer so right choice is to automatically create it by providing AbortOnConnectFail = false
                configOption.AbortOnConnectFail = false;
            }
            else
            {
                configOption = new ConfigurationOptions();
                if (configuration.Port == 0)
                {
                    configOption.EndPoints.Add(configuration.Host);
                }
                else
                {
                    configOption.EndPoints.Add(configuration.Host + ":" + configuration.Port);
                }
                configOption.Password = configuration.AccessKey;
                configOption.Ssl = configuration.UseSsl;
                configOption.AbortOnConnectFail = false;

                if (configuration.ConnectionTimeoutInMilliSec != 0)
                {
                    configOption.ConnectTimeout = configuration.ConnectionTimeoutInMilliSec;
                }

                if (configuration.OperationTimeoutInMilliSec != 0)
                {
                    configOption.SyncTimeout = configuration.OperationTimeoutInMilliSec;
                }
            }
            if (LogUtility.logger == null)
            {
                redisMultiplexer = ConnectionMultiplexer.Connect(configOption);
            }
            else
            {
                redisMultiplexer = ConnectionMultiplexer.Connect(configOption, LogUtility.logger);
            }
            this.connection = redisMultiplexer.GetDatabase(configuration.DatabaseId);
        }
Beispiel #5
0
        public StackExchangeClientConnection(ProviderConfiguration configuration)
        {
            this.configuration = configuration;
            ConfigurationOptions configOption;

            // If connection string is given then use it otherwise use individual options
            if (!string.IsNullOrEmpty(configuration.ConnectionString))
            {
                configOption = ConfigurationOptions.Parse(configuration.ConnectionString);
                // Setting explicitly 'abortconnect' to false. It will overwrite customer provided value for 'abortconnect'
                // As it doesn't make sense to allow to customer to set it to true as we don't give them access to ConnectionMultiplexer
                // in case of failure customer can not create ConnectionMultiplexer so right choice is to automatically create it by providing AbortOnConnectFail = false
                configOption.AbortOnConnectFail = false;
            }
            else
            {
                configOption = new ConfigurationOptions();
                if (configuration.Port == 0)
                {
                    configOption.EndPoints.Add(configuration.Host);
                }
                else
                {
                    configOption.EndPoints.Add(configuration.Host + ":" + configuration.Port);
                }
                configOption.Password           = configuration.AccessKey;
                configOption.Ssl                = configuration.UseSsl;
                configOption.AbortOnConnectFail = false;

                if (configuration.ConnectionTimeoutInMilliSec != 0)
                {
                    configOption.ConnectTimeout = configuration.ConnectionTimeoutInMilliSec;
                }

                if (configuration.OperationTimeoutInMilliSec != 0)
                {
                    configOption.SyncTimeout = configuration.OperationTimeoutInMilliSec;
                }
            }
            if (LogUtility.logger == null)
            {
                redisMultiplexer = ConnectionMultiplexer.Connect(configOption);
            }
            else
            {
                redisMultiplexer = ConnectionMultiplexer.Connect(configOption, LogUtility.logger);
            }
            this.connection = redisMultiplexer.GetDatabase(configuration.DatabaseId);
        }
        internal RedisObjectCache(string name, NameValueCollection config)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            Name          = name;
            configuration = ProviderConfiguration.ProviderConfigurationForObjectCache(config, name);
            cache         = new RedisObjectCacheConnectionWrapper(configuration, name);
        }
        internal static ProviderConfiguration ProviderConfigurationForOutputCache(NameValueCollection config)
        {
            ProviderConfiguration configuration = new ProviderConfiguration(config);
            
            // No retry login for output cache provider
            configuration.RetryTimeout = TimeSpan.Zero;
            
            // Session state specific attribute which are not applicable to output cache
            configuration.ThrowOnError = true;
            configuration.RequestTimeout = TimeSpan.Zero;
            configuration.SessionTimeout = TimeSpan.Zero;

            LogUtility.LogInfo("Host: {0}, Port: {1}, UseSsl: {2}, DatabaseId: {3}, ApplicationName: {4}",
                                            configuration.Host, configuration.Port, configuration.UseSsl, configuration.DatabaseId, configuration.ApplicationName);
            return configuration;
        }
        internal static ProviderConfiguration ProviderConfigurationForOutputCache(NameValueCollection config)
        {
            ProviderConfiguration configuration = new ProviderConfiguration(config);

            // No retry login for output cache provider
            configuration.RetryTimeout = TimeSpan.Zero;

            // Session state specific attribute which are not applicable to output cache
            configuration.ThrowOnError   = true;
            configuration.RequestTimeout = TimeSpan.Zero;
            configuration.SessionTimeout = TimeSpan.Zero;

            LogUtility.LogInfo("Host: {0}, Port: {1}, UseSsl: {2}, DatabaseId: {3}, ApplicationName: {4}",
                               configuration.Host, configuration.Port, configuration.UseSsl, configuration.DatabaseId, configuration.ApplicationName);
            return(configuration);
        }
Beispiel #9
0
        internal static ProviderConfiguration ProviderConfigurationForObjectCache(NameValueCollection config, string cacheName)
        {
            ProviderConfiguration configuration = new ProviderConfiguration(config);

            configuration.RetryTimeout = TimeSpan.Zero;
            configuration.ThrowOnError = GetBoolSettings(config, "throwOnError", false);

            // Session state specific attribute which are not applicable to output cache
            configuration.RequestTimeout = TimeSpan.Zero;
            configuration.SessionTimeout = TimeSpan.Zero;

            configuration.ApplicationName += "_ObjectCache_" + cacheName;

            LogUtility.LogInfo("Host: {0}, Port: {1}, UseSsl: {2}, DatabaseId: {3}, ApplicationName: {4}, ThrowOnError: {5}",
                               configuration.Host, configuration.Port, configuration.UseSsl, configuration.DatabaseId, configuration.ApplicationName, configuration.ThrowOnError);
            return(configuration);
        }
Beispiel #10
0
        public RedisSharedConnection(ProviderConfiguration configuration)
        {
            _configuration = configuration;

            // If connection string is given then use it otherwise use individual options
            if (!string.IsNullOrEmpty(configuration.ConnectionString))
            {
                _configOption = ConfigurationOptions.Parse(configuration.ConnectionString);
                // Setting explicitly 'abortconnect' to false. It will overwrite customer provided value for 'abortconnect'
                // As it doesn't make sense to allow to customer to set it to true as we don't give them access to ConnectionMultiplexer
                // in case of failure customer can not create ConnectionMultiplexer so right choice is to automatically create it by providing AbortOnConnectFail = false
                _configOption.AbortOnConnectFail = false;
            }
            else
            {
                _configOption = new ConfigurationOptions();
                if (configuration.Port == 0)
                {
                    _configOption.EndPoints.Add(configuration.Host);
                }
                else
                {
                    _configOption.EndPoints.Add(configuration.Host + ":" + configuration.Port);
                }
                _configOption.Password           = configuration.AccessKey;
                _configOption.Ssl                = configuration.UseSsl;
                _configOption.AbortOnConnectFail = false;

                if (configuration.ConnectionTimeoutInMilliSec != 0)
                {
                    _configOption.ConnectTimeout = configuration.ConnectionTimeoutInMilliSec;
                }

                if (configuration.OperationTimeoutInMilliSec != 0)
                {
                    _configOption.SyncTimeout = configuration.OperationTimeoutInMilliSec;
                }

                if (configuration.WriteBuffer != 0)
                {
                    _configOption.WriteBuffer = configuration.WriteBuffer;
                }
            }
            CreateMultiplexer();
        }
Beispiel #11
0
        public StackExchangeClientConnection(ProviderConfiguration configuration)
        {
            this.configuration = configuration;
            ConfigurationOptions configOption;

            // If connection string is given then use it otherwise use individual options
            if (!string.IsNullOrEmpty(configuration.ConnectionString))
            {
                configOption = ConfigurationOptions.Parse(configuration.ConnectionString);
            }
            else
            {
                configOption = new ConfigurationOptions();
                if (configuration.Port == 0)
                {
                    configOption.EndPoints.Add(configuration.Host);
                }
                else
                {
                    configOption.EndPoints.Add(configuration.Host + ":" + configuration.Port);
                }
                configOption.Password           = configuration.AccessKey;
                configOption.Ssl                = configuration.UseSsl;
                configOption.AbortOnConnectFail = false;

                if (configuration.ConnectionTimeoutInMilliSec != 0)
                {
                    configOption.ConnectTimeout = configuration.ConnectionTimeoutInMilliSec;
                }

                if (configuration.OperationTimeoutInMilliSec != 0)
                {
                    configOption.SyncTimeout = configuration.OperationTimeoutInMilliSec;
                }
            }
            if (LogUtility.logger == null)
            {
                redisMultiplexer = ConnectionMultiplexer.Connect(configOption);
            }
            else
            {
                redisMultiplexer = ConnectionMultiplexer.Connect(configOption, LogUtility.logger);
            }
            this.connection = redisMultiplexer.GetDatabase(configuration.DatabaseId);
        }
        public StackExchangeClientConnection(ProviderConfiguration configuration)
        {
            _configuration = configuration;
            ConfigurationOptions configOption;

            // If connection string is given then use it otherwise use individual options
            if (!string.IsNullOrEmpty(configuration.ConnectionString))
            {
                configOption = ConfigurationOptions.Parse(configuration.ConnectionString);

                if (!string.IsNullOrEmpty(configOption.ServiceName))
                {
                    ModifyEndpointsForSentinelConfiguration(configOption);
                }
            }
            else
            {
                configOption = new ConfigurationOptions();
                if (configuration.Port == 0)
                {
                    configOption.EndPoints.Add(configuration.Host);
                }
                else
                {
                    configOption.EndPoints.Add(configuration.Host + ":" + configuration.Port);
                }
                configOption.Password = configuration.AccessKey;
                configOption.Ssl = configuration.UseSsl;
                configOption.AbortOnConnectFail = false;

                if (configuration.ConnectionTimeoutInMilliSec != 0)
                {
                    configOption.ConnectTimeout = configuration.ConnectionTimeoutInMilliSec;
                }

                if (configuration.OperationTimeoutInMilliSec != 0)
                {
                    configOption.SyncTimeout = configuration.OperationTimeoutInMilliSec;
                }
            }

            _redisMultiplexer = LogUtility.logger == null ? ConnectionMultiplexer.Connect(configOption) : ConnectionMultiplexer.Connect(configOption, LogUtility.logger);

            _connection = _redisMultiplexer.GetDatabase(configuration.DatabaseId);
        }
 public RedisOutputCacheConnectionWrapper(ProviderConfiguration configuration)
 {
     this.configuration = configuration;
     
     // Shared connection is created by server when it starts. don't want to lock everytime when check == null.
     // so that is why pool == null exists twice.
     if (sharedConnection == null)
     {
         lock (lockForSharedConnection)
         {
             if (sharedConnection == null)
             {
                 sharedConnection = new RedisSharedConnection(configuration,() => new StackExchangeClientConnection(configuration));
             }
         }
     }
     redisConnection = sharedConnection.TryGetConnection();
 }
Beispiel #14
0
        public RedisOutputCacheConnectionWrapper(ProviderConfiguration configuration)
        {
            this.configuration = configuration;

            // Shared connection is created by server when it starts. don't want to lock everytime when check == null.
            // so that is why pool == null exists twice.
            if (sharedConnection == null)
            {
                lock (lockForSharedConnection)
                {
                    if (sharedConnection == null)
                    {
                        sharedConnection = new RedisSharedConnection(configuration, () => new StackExchangeClientConnection(configuration));
                    }
                }
            }
            redisConnection = sharedConnection.TryGetConnection();
        }
        public RedisOutputCacheConnectionWrapper(ProviderConfiguration configuration)
        {
            this.configuration = configuration;

            // only single object of RedisSharedConnection will be created and then reused
            if (sharedConnection == null)
            {
                lock (lockForSharedConnection)
                {
                    if (sharedConnection == null)
                    {
                        sharedConnection = new RedisSharedConnection(configuration);
                        redisUtility     = new RedisUtility(configuration);
                    }
                }
            }
            redisConnection = new StackExchangeClientConnection(configuration, redisUtility, sharedConnection);
        }
Beispiel #16
0
        public RedisObjectCacheConnectionWrapper(ProviderConfiguration configuration, string name)
        {
            this.configuration = configuration;

            // Shared connection is created by server when it starts. don't want to lock everytime when check == null.
            // so that is why pool == null exists twice.
            if (!sharedConnections.ContainsKey(name))
            {
                lock (lockForSharedConnection)
                {
                    if (!sharedConnections.ContainsKey(name))
                    {
                        sharedConnections[name] = new RedisSharedConnection(configuration, () => new StackExchangeClientConnection(configuration));
                        redisUtility            = new RedisUtility(configuration);
                    }
                }
            }
            redisConnection = sharedConnections[name].TryGetConnection();
        }
Beispiel #17
0
        private static void ConfigureHosts(ProviderConfiguration configuration, ConfigurationOptions configOption)
        {
            foreach (var host in configuration.Host.Split(',', ';'))
            {
                if (string.IsNullOrEmpty(host))
                {
                    continue;
                }

                if (configuration.Port == 0)
                {
                    configOption.EndPoints.Add(host);
                }
                else
                {
                    configOption.EndPoints.Add(host + ":" + configuration.Port);
                }
            }
        }
Beispiel #18
0
        public RedisConnectionWrapper(ProviderConfiguration configuration, string id)
        {
            this.configuration = configuration;
            Keys = new KeyGenerator(id, configuration.ApplicationName);

            // Pool is created by server when it starts. don't want to lock everytime when check pool == null.
            // so that is why pool == null exists twice.
            if (sharedConnection == null)
            {
                lock (lockForSharedConnection)
                {
                    if (sharedConnection == null)
                    {
                        sharedConnection = new RedisSharedConnection(configuration, () => new StackExchangeClientConnection(configuration));
                    }
                }
            }
            redisConnection = sharedConnection.TryGetConnection();
        }
 public RedisConnectionWrapper(ProviderConfiguration configuration, string id)
 {
     this.configuration = configuration;
     Keys = new KeyGenerator(id, configuration.ApplicationName);
     
     // Pool is created by server when it starts. don't want to lock everytime when check pool == null.
     // so that is why pool == null exists twice.
     if (sharedConnection == null)
     {
         lock (lockForSharedConnection)
         {
             if (sharedConnection == null)
             {
                 sharedConnection = new RedisSharedConnection(configuration,() => new StackExchangeClientConnection(configuration));
             }
         }
     }
     redisConnection = sharedConnection.TryGetConnection();
 }
        public RedisConnectionWrapper(ProviderConfiguration configuration, string id)
        {
            this.configuration = configuration;
            Keys = new KeyGenerator(id, configuration.ApplicationName);

            // only single object of RedisSharedConnection will be created and then reused
            if (sharedConnection == null)
            {
                lock (lockForSharedConnection)
                {
                    if (sharedConnection == null)
                    {
                        sharedConnection = new RedisSharedConnection(configuration);
                        redisUtility     = new RedisUtility(configuration);
                    }
                }
            }
            redisConnection = new StackExchangeClientConnection(configuration, redisUtility, sharedConnection);
        }
        internal static ProviderConfiguration ProviderConfigurationForSessionState(NameValueCollection config)
        {
            ProviderConfiguration configuration = new ProviderConfiguration(config);
            
            configuration.ThrowOnError = GetBoolSettings(config, "throwOnError", true);
            int retryTimeoutInMilliSec = GetIntSettings(config, "retryTimeoutInMilliseconds", 5000);
            configuration.RetryTimeout = new TimeSpan(0, 0, 0, 0, retryTimeoutInMilliSec);
            
            // Get request timeout from config
            HttpRuntimeSection httpRuntimeSection = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
            configuration.RequestTimeout = httpRuntimeSection.ExecutionTimeout;

            // Get session timeout from config
            SessionStateSection sessionStateSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
            configuration.SessionTimeout = sessionStateSection.Timeout;

            LogUtility.LogInfo("Host: {0}, Port: {1}, ThrowOnError: {2}, UseSsl: {3}, RetryTimeout: {4}, DatabaseId: {5}, ApplicationName: {6}, RequestTimeout: {7}, SessionTimeout: {8}",
                                            configuration.Host, configuration.Port, configuration.ThrowOnError, configuration.UseSsl, configuration.RetryTimeout, configuration.DatabaseId, configuration.ApplicationName, configuration.RequestTimeout, configuration.SessionTimeout);
            return configuration;
        }
        internal static ProviderConfiguration ProviderConfigurationForSessionState(NameValueCollection config)
        {
            ProviderConfiguration configuration = new ProviderConfiguration(config);

            configuration.ThrowOnError = GetBoolSettings(config, "throwOnError", true);
            int retryTimeoutInMilliSec = GetIntSettings(config, "retryTimeoutInMilliseconds", 5000);

            configuration.RetryTimeout = new TimeSpan(0, 0, 0, 0, retryTimeoutInMilliSec);

            // Get request timeout from config
            HttpRuntimeSection httpRuntimeSection = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;

            configuration.RequestTimeout = httpRuntimeSection.ExecutionTimeout;

            // Get session timeout from config
            SessionStateSection sessionStateSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");

            configuration.SessionTimeout = sessionStateSection.Timeout;

            LogUtility.LogInfo("Host: {0}, Port: {1}, ThrowOnError: {2}, UseSsl: {3}, RetryTimeout: {4}, DatabaseId: {5}, ApplicationName: {6}, RequestTimeout: {7}, SessionTimeout: {8}",
                               configuration.Host, configuration.Port, configuration.ThrowOnError, configuration.UseSsl, configuration.RetryTimeout, configuration.DatabaseId, configuration.ApplicationName, configuration.RequestTimeout, configuration.SessionTimeout);
            return(configuration);
        }
        private static IConnectionMultiplexerFactory GetConnectionMultiplexerFactory(ProviderConfiguration configuration)
        {
            // use factory type if given
            if (!string.IsNullOrEmpty(configuration.ConnectionMultiplexerFactoryType))
            {
                var factoryType = Type.GetType(configuration.ConnectionMultiplexerFactoryType, throwOnError: true);
                var factory     = (IConnectionMultiplexerFactory)Activator.CreateInstance(factoryType);
                return(factory);
            }

            // otherwise use connection string if given
            if (!string.IsNullOrEmpty(configuration.ConnectionString))
            {
                return(new ConnectionStringBasedConnectionMultiplexerFactory(configuration));
            }

            // otherwise use individual provider fields
            if (!string.IsNullOrEmpty(configuration.Host))
            {
                return(new ProviderFieldsBasedConnectionMultiplexerFactory(configuration));
            }

            throw new ConfigurationErrorsException("Provider configuration is invalid, either provider 'connectionString', or 'host' (and other fields), or 'connectionMultiplexerFactoryType'");
        }
 public RedisSharedConnection(ProviderConfiguration configuration) : this(GetConnectionMultiplexerFactory(configuration))
 {
 }
 public StackExchangeClientConnection(ProviderConfiguration configuration, RedisUtility redisUtility, RedisSharedConnection sharedConnection)
 {
     _configuration    = configuration;
     _redisUtility     = redisUtility;
     _sharedConnection = sharedConnection;
 }
Beispiel #26
0
 public RedisSharedConnection(ProviderConfiguration configuration, Func <IRedisClientConnection> factory)
 {
     this.configuration = configuration;
     lockObject         = new object();
     this.factory       = factory;
 }
 public RedisUtility(ProviderConfiguration configuration)
 {
     _configuration = configuration;
     _serializer = GetSerializer();
 }
        private ConfigurationOptions ParseConfiguration(ProviderConfiguration configuration)
        {
            ConfigurationOptions configOption;
              // If connection string is given then use it otherwise use individual options
              if (!string.IsNullOrEmpty(configuration.ConnectionString))
              {
            configOption = ConfigurationOptions.Parse(configuration.ConnectionString);
              }
              else
              {
            configOption = new ConfigurationOptions();
            if (configuration.Port == 0)
            {
              configOption.EndPoints.Add(configuration.Host);
            }
            else
            {
              configOption.EndPoints.Add(configuration.Host + ":" + configuration.Port);
            }
              configOption.Password = configuration.AccessKey;
              configOption.Ssl = configuration.UseSsl;
              configOption.AbortOnConnectFail = false;

            if (configuration.ConnectionTimeoutInMilliSec != 0)
            {
              configOption.ConnectTimeout = configuration.ConnectionTimeoutInMilliSec;
            }

            if (configuration.OperationTimeoutInMilliSec != 0)
            {
              configOption.SyncTimeout = configuration.OperationTimeoutInMilliSec;
            }
              }
              return configOption;
        }
 public RedisSharedConnection(ProviderConfiguration configuration, Func<IRedisClientConnection> factory) 
 {
     this.configuration = configuration;
     lockObject = new object();
     this.factory = factory;
 }
 public RedisUtility(ProviderConfiguration configuration)
 {
     _configuration = configuration;
     _serializer    = GetSerializer();
 }
        private void ConnectToRedis(ProviderConfiguration configuration)
        {
            _redisMultiplexer = LogUtility.logger == null
            ? ConnectionMultiplexer.Connect(_configOption)
            : ConnectionMultiplexer.Connect(_configOption, LogUtility.logger);

              _connection = _redisMultiplexer.GetDatabase(configuration.DatabaseId);
        }