/// <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) { var contextBase = new HttpContextWrapper(context); var sessionId = RedisSessionStateStoreProvider.RedisHashIdFromSessionId(contextBase, id); var rConnWrap = RedisSessionStateStoreProvider.RedisConnWrapper(); rConnWrap.Remove(sessionId); }
/// <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) { var setItems = new List <KeyValuePair <string, string> >(); var delItems = new List <string>(); // 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(changedObj); } else { delItems.Add(changedObj.Key); } } var rConnWrap = RedisSessionStateStoreProvider.RedisConnWrapper(); if (setItems.Count > 0) { rConnWrap.HashSet(currentRedisHashId, setItems); // call appropriate delegate if set for changing keys if (RedisSessionConfig.RedisWriteFieldDel != null) { RedisSessionConfig.RedisWriteFieldDel(context, setItems, currentRedisHashId); } } if (delItems != null && delItems.Count > 0) { rConnWrap.HashDelete(currentRedisHashId, delItems); // call appropriate delegate if set for removing keys if (RedisSessionConfig.RedisRemoveFieldDel != null) { RedisSessionConfig.RedisRemoveFieldDel(context, delItems, currentRedisHashId); } } // always refresh the timeout of the session hash rConnWrap.KeyExpire(currentRedisHashId, expirationTimeout); }
/// <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) { var rConnWrap = RedisSessionStateStoreProvider.RedisConnWrapper(); try { var redisData = rConnWrap.HashGetAll(redisKey); rConnWrap.KeyExpire(redisKey, expirationTimeout); return(new RedisSessionStateItemCollection(redisData, rConnWrap.ConnectionId)); } catch (Exception e) { if (RedisSessionConfig.SessionExceptionLoggingDel != null) { RedisSessionConfig.SessionExceptionLoggingDel(e); } } return(new RedisSessionStateItemCollection()); }