/// <summary> /// Gets a hash from Redis and passes it to the constructor of RedisSessionStateItemCollection /// </summary> /// <param name="redisKey">The key of the Redis hash</param> /// <param name="context">The HttpContext of the current web request</param> /// <returns>An instance of RedisSessionStateItemCollection, may be empty if Redis call fails</returns> public static RedisSessionStateItemCollection GetItemFromRedis( string redisKey, HttpContextBase context, TimeSpan expirationTimeout) { RedisConnectionWrapper rConnWrap = RedisSessionStateStoreProvider.RedisConnWrapperFromContext( context); IDatabase redisConn = rConnWrap.GetConnection(); try { HashEntry[] redisData = redisConn.HashGetAll(redisKey); redisConn.KeyExpire(redisKey, expirationTimeout, CommandFlags.FireAndForget); return(new RedisSessionStateItemCollection( redisData, rConnWrap.ConnectionID, 0)); } catch (Exception e) { if (RedisSessionConfig.SessionExceptionLoggingDel != null) { RedisSessionConfig.SessionExceptionLoggingDel(e); } } return(new RedisSessionStateItemCollection()); }
/// <summary> /// Deletes an entire Session from Redis by removing the key. /// </summary> /// <param name="context">The HttpContext of the current request</param> /// <param name="id">The Session Id</param> /// <param name="lockId">The object used to lock the Session</param> /// <param name="item">The Session's properties</param> public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item) { RedisConnectionWrapper rConnWrap = RedisConnWrapperFromContext(new HttpContextWrapper(context)); var redisKey = RedisHashIdFromSessionId(new HttpContextWrapper(context), id); IDatabase redisConn = rConnWrap.GetConnection(redisKey); redisConn.KeyDelete(redisKey, 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); 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); }