Beispiel #1
0
        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.ProviderConfigurationForSessionState(config);
                        redisUtility  = new RedisUtility(configuration);
                    }
                }
            }
        }
Beispiel #2
0
        internal static ISessionStateItemCollection GetSessionDataStatic(object rowDataFromRedis)
        {
            RedisResult rowDataAsRedisResult = (RedisResult)rowDataFromRedis;

            RedisResult[] lockScriptReturnValueArray = (RedisResult[])rowDataAsRedisResult;
            Debug.Assert(lockScriptReturnValueArray != null);

            ISessionStateItemCollection sessionData = null;

            if (lockScriptReturnValueArray.Length > 1 && lockScriptReturnValueArray[1] != null)
            {
                RedisResult[] data = (RedisResult[])lockScriptReturnValueArray[1];

                // LUA script returns data as object array so keys and values are store one after another
                // This list has to be even because it contains pair of <key, value> as {key, value, key, value}
                if (data != null && data.Length != 0 && data.Length % 2 == 0)
                {
                    sessionData = new ChangeTrackingSessionStateItemCollection();
                    // In every cycle of loop we are getting one pair of key value and putting it into session items
                    // thats why increment is by 2 because we want to move to next pair
                    for (int i = 0; (i + 1) < data.Length; i += 2)
                    {
                        string key = (string)data[i];
                        object val = RedisUtility.GetObjectFromBytes((byte[])data[i + 1]);
                        if (key != null)
                        {
                            sessionData[key] = val;
                        }
                    }
                }
            }
            return(sessionData);
        }
Beispiel #3
0
        private bool TryUpdateIfLockIdMatchPrepare(object lockId, ISessionStateItemCollection data, int sessionTimeout, out string[] keyArgs, out object[] valueArgs)
        {
            keyArgs   = null;
            valueArgs = null;
            if (data != null)
            {
                List <object> list = new List <object>();
                ChangeTrackingSessionStateItemCollection sessionItems = (ChangeTrackingSessionStateItemCollection)data;
                int noOfItemsRemoved = RedisUtility.AppendRemoveItemsInList(sessionItems, list);
                int noOfItemsUpdated = RedisUtility.AppendUpdatedOrNewItemsInList(sessionItems, list);

                keyArgs      = new string[] { Keys.LockKey, Keys.DataKey, Keys.InternalKey };
                valueArgs    = new object[list.Count + 8]; // this +8 is for first wight values in ARGV that we will add now
                valueArgs[0] = lockId;
                valueArgs[1] = sessionTimeout;
                valueArgs[2] = noOfItemsRemoved;
                valueArgs[3] = 9;                    // In Lua index starts from 1 so first item deleted will be 9th.
                valueArgs[4] = noOfItemsRemoved + 8; // index for last removed item
                valueArgs[5] = noOfItemsUpdated;
                valueArgs[6] = noOfItemsRemoved + 9; // first item updated will be next to last item removed
                valueArgs[7] = list.Count + 8;       // index for last item in list in LUA

                // if nothing is changed in session then also execute update script to update session timeout
                if (list.Count != 0)
                {
                    list.CopyTo(valueArgs, 8);
                }
                return(true);
            }
            return(false);
        }
Beispiel #4
0
        public object Add(string key, object entry, DateTime utcExpiry)
        {
            key = GetKeyForRedis(key);
            TimeSpan expiryTime = utcExpiry - DateTime.UtcNow;

            string[] keyArgs   = new string[] { key };
            object[] valueArgs = new object[] { RedisUtility.GetBytesFromObject(entry), expiryTime.TotalMilliseconds };

            object rowDataFromRedis = redisConnection.Eval(addScript, keyArgs, valueArgs);

            return(RedisUtility.GetObjectFromBytes(redisConnection.GetOutputCacheDataFromResult(rowDataFromRedis)));
        }
Beispiel #5
0
        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(configOption.DefaultDatabase ?? configuration.DatabaseId);
        }
 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);
        }
        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);
        }
        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));
                        redisUtility     = new RedisUtility(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);
        }
        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));
                        redisUtility = new RedisUtility(configuration);
                    }
                }
            }
            redisConnection = sharedConnection.TryGetConnection();
        }
Beispiel #12
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));
                        redisUtility     = new RedisUtility(configuration);
                    }
                }
            }
            redisConnection = sharedConnection.TryGetConnection();
        }
Beispiel #13
0
 private bool SetPrepare(ISessionStateItemCollection data, int sessionTimeout, out string[] keyArgs, out object[] valueArgs)
 {
     keyArgs   = null;
     valueArgs = null;
     if (data != null && data.Count > 0)
     {
         ChangeTrackingSessionStateItemCollection sessionItems = (ChangeTrackingSessionStateItemCollection)data;
         List <object> list = RedisUtility.GetNewItemsAsList(sessionItems);
         if (list.Count > 0)
         {
             keyArgs      = new string[] { Keys.DataKey, Keys.InternalKey };
             valueArgs    = new object[list.Count + 2]; // this +2 is for first 2 values in ARGV that we will add now
             valueArgs[0] = list.Count + 2;
             valueArgs[1] = sessionTimeout;
             list.CopyTo(valueArgs, 2);
             return(true);
         }
     }
     return(false);
 }
Beispiel #14
0
 public ChangeTrackingSessionStateItemCollection(RedisUtility redisUtility)
 {
     this.redisUtility = redisUtility;
     innerCollection   = new SessionStateItemCollection();
 }
Beispiel #15
0
/*-------End of Add operation-----------------------------------------------------------------------------------------------------------------------------------------------*/

        public void Set(string key, object entry, DateTime utcExpiry)
        {
            key = GetKeyForRedis(key);
            byte[] data = RedisUtility.GetBytesFromObject(entry);
            redisConnection.Set(key, data, utcExpiry);
        }
        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));
                        redisUtility = new RedisUtility(configuration);
                    }
                }
            }
            redisConnection = sharedConnection.TryGetConnection();
        }
Beispiel #17
0
 public object Get(string key)
 {
     key = GetKeyForRedis(key);
     byte[] data = redisConnection.Get(key);
     return(RedisUtility.GetObjectFromBytes(data));
 }
 public StackExchangeClientConnection(ProviderConfiguration configuration, RedisUtility redisUtility, RedisSharedConnection sharedConnection)
 {
     _configuration    = configuration;
     _redisUtility     = redisUtility;
     _sharedConnection = sharedConnection;
 }
 public ChangeTrackingSessionStateItemCollection(RedisUtility utility)
 {
     _utility        = utility;
     innerCollection = new SessionStateItemCollection();
 }