/// <summary>
        /// Helper method for serializing objects to Redis
        /// </summary>
        /// <param name="confirmedChangedObjects">keys and values that have definitely changed</param>
        /// <param name="allObjects">keys and values that have been accessed during the current HttpContext</param>
        /// <param name="allObjectsOriginalState">keys and serialized values before we tampered with them</param>
        /// <param name="deletedObjects">keys that were deleted during the current HttpContext</param>
        /// <param name="currentRedisHashId">The current Redis key name</param>
        /// <param name="redisConn">A connection to Redis</param>
        public static void SerializeToRedis(
            HttpContextBase context,
            RedisSessionStateItemCollection redisItems,
            string currentRedisHashId,
            TimeSpan expirationTimeout)
        {
            List <HashEntry>  setItems = new List <HashEntry>();
            List <RedisValue> delItems = new List <RedisValue>();

            RedisConnectionWrapper rConnWrap = RedisSessionStateStoreProvider.RedisConnWrapperFromContext(
                context);

            foreach (KeyValuePair <string, string> changedObj in
                     redisItems.GetChangedObjectsEnumerator())
            {
                if (changedObj.Value != null)
                {
                    setItems.Add(
                        new HashEntry(
                            changedObj.Key,
                            changedObj.Value));
                }
                else
                {
                    delItems.Add(changedObj.Key);
                }
            }

            IDatabase redisConn = rConnWrap.GetConnection();

            redisConn.KeyExpire(
                currentRedisHashId,
                expirationTimeout,
                CommandFlags.FireAndForget);

            if (setItems.Count > 0)
            {
                redisConn.HashSet(
                    currentRedisHashId,
                    setItems.ToArray(),
                    CommandFlags.FireAndForget);
            }
            if (delItems != null && delItems.Count > 0)
            {
                redisConn.HashDelete(
                    currentRedisHashId,
                    delItems.ToArray(),
                    CommandFlags.FireAndForget);
            }
        }
        /// <summary>
        /// Helper method for serializing objects to Redis
        /// </summary>
        /// <param name="confirmedChangedObjects">keys and values that have definitely changed</param>
        /// <param name="allObjects">keys and values that have been accessed during the current HttpContext</param>
        /// <param name="allObjectsOriginalState">keys and serialized values before we tampered with them</param>
        /// <param name="deletedObjects">keys that were deleted during the current HttpContext</param>
        /// <param name="currentRedisHashId">The current Redis key name</param>
        /// <param name="redisConn">A connection to Redis</param>
        public static void SerializeToRedis(HttpContextBase context, RedisSessionStateItemCollection redisItems, string currentRedisHashId, TimeSpan expirationTimeout)
        {
            RedisConnectionWrapper rConnWrap = RedisSessionStateStoreProvider.RedisConnWrapperFromContext(context);

            var setItems = new List <HashEntry>();
            var delItems = new List <RedisValue>();

            // Determine if we are adding or removing keys, separate them into their own lists
            //      note that redisItems.GetChangedObjectsEnumerator contains complex logic
            foreach (KeyValuePair <string, string> changedObj in redisItems.GetChangedObjectsEnumerator())
            {
                if (changedObj.Value != null)
                {
                    setItems.Add(new HashEntry(changedObj.Key, changedObj.Value));
                }
                else
                {
                    delItems.Add(changedObj.Key);
                }
            }

            IDatabase redisConn = rConnWrap.GetConnection(currentRedisHashId);

            if (setItems.Count > 0)
            {
                HashEntry[] writeItems = setItems.ToArray();
                redisConn.HashSet(currentRedisHashId, writeItems, CommandFlags.FireAndForget);

                // call appropriate delegate if set for changing keys
                if (RedisSessionConfig.RedisWriteFieldDel != null)
                {
                    RedisSessionConfig.RedisWriteFieldDel(context, writeItems, currentRedisHashId);
                }
            }
            if (delItems != null && delItems.Count > 0)
            {
                RedisValue[] removeItems = delItems.ToArray();
                redisConn.HashDelete(currentRedisHashId, removeItems, CommandFlags.FireAndForget);

                // call appropriate delegate if set for removing keys
                if (RedisSessionConfig.RedisRemoveFieldDel != null)
                {
                    RedisSessionConfig.RedisRemoveFieldDel(context, removeItems, currentRedisHashId);
                }
            }

            // always refresh the timeout of the session hash
            redisConn.KeyExpire(currentRedisHashId, expirationTimeout, CommandFlags.FireAndForget);
        }
        /// <summary>
        /// Helper method for serializing objects to Redis
        /// </summary>
        /// <param name="confirmedChangedObjects">keys and values that have definitely changed</param>
        /// <param name="allObjects">keys and values that have been accessed during the current HttpContext</param>
        /// <param name="allObjectsOriginalState">keys and serialized values before we tampered with them</param>
        /// <param name="deletedObjects">keys that were deleted during the current HttpContext</param>
        /// <param name="currentRedisHashId">The current Redis key name</param>
        /// <param name="redisConn">A connection to Redis</param>
        public static void SerializeToRedis(
            HttpContextBase context,
            RedisSessionStateItemCollection redisItems,
            string currentRedisHashId,
            TimeSpan expirationTimeout)
        {
            List<HashEntry> setItems = new List<HashEntry>();
            List<RedisValue> delItems = new List<RedisValue>();

            RedisConnectionWrapper rConnWrap = RedisSessionStateStoreProvider.RedisConnWrapperFromContext(
                context);

            // Determine if we are adding or removing keys, separate them into their own lists
            //      note that redisItems.GetChangedObjectsEnumerator contains complex logic
            foreach (KeyValuePair<string, string> changedObj in
                redisItems.GetChangedObjectsEnumerator())
            {
                if (changedObj.Value != null)
                {
                    setItems.Add(
                        new HashEntry(
                            changedObj.Key,
                            changedObj.Value));
                }
                else
                {
                    delItems.Add(changedObj.Key);
                }
            }

            IDatabase redisConn = rConnWrap.GetConnection();

            if (setItems.Count > 0)
            {
                HashEntry[] writeItems = setItems.ToArray();
                redisConn.HashSet(
                    currentRedisHashId,
                    writeItems,
                    CommandFlags.FireAndForget);

                // call appropriate delegate if set for changing keys
                if(RedisSessionConfig.RedisWriteFieldDel != null)
                {
                    RedisSessionConfig.RedisWriteFieldDel(
                        context, 
                        writeItems, 
                        currentRedisHashId);
                }
            }
            if (delItems != null && delItems.Count > 0)
            {
                RedisValue[] removeItems = delItems.ToArray();
                redisConn.HashDelete(
                    currentRedisHashId,
                    removeItems,
                    CommandFlags.FireAndForget);

                // call appropriate delegate if set for removing keys
                if(RedisSessionConfig.RedisRemoveFieldDel != null)
                {
                    RedisSessionConfig.RedisRemoveFieldDel(
                        context,
                        removeItems,
                        currentRedisHashId);
                }
            }

            // always refresh the timeout of the session hash
            redisConn.KeyExpire(
                currentRedisHashId,
                expirationTimeout,
                CommandFlags.FireAndForget);
        }