/// <summary> /// 使用当前请求中的值更新会话状态数据存储区中的会话项信息,并清除对数据的锁定 /// </summary> /// <param name="context">上下文</param> /// <param name="id">SessionID</param> /// <param name="item">Session选项</param> /// <param name="lockId">锁ID</param> /// <param name="newItem">如果为 true,则将会话项标识为新项;如果为 false,则将会话项标识为现有的项</param> public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { var binary = SessionSerializer.Serialize(item.Items as SessionStateItemCollection); var session = new SessionItem() { LockId = lockId == null ? 0 : (int)lockId, Binary = binary, TimeOut = item.Timeout, ActionFlag = SessionStateActions.None }; this.cachedEx.Set(id, session, TimeSpan.FromMinutes(item.Timeout)); }
/// <summary> /// 从会话数据存储区中返回只读会话状态数据 /// </summary> /// <param name="isExclusive">是否是竞争锁的</param> /// <param name="context">当前请求的 HttpContext</param> /// <param name="id">当前请求的 SessionState.HttpSessionState.SessionID</param> /// <param name="locked">如果请求的会话项在会话数据存储区被锁定,请包含一个设置为 true 的布尔值;否则请包含一个设置为 false 的布尔值</param> /// <param name="lockAge">请包含一个设置为会话数据存储区中的项锁定时间的 System.TimeSpan 对象</param> /// <param name="lockId">请包含一个设置为当前请求的锁定标识符的对象</param> /// <param name="actions">请包含 SessionState.SessionStateActions 值之一,指示当前会话是否为未初始化的无Cookie会话</param> /// <returns></returns> private SessionStateStoreData GetItem(bool isExclusive, HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) { locked = false; lockAge = TimeSpan.Zero; lockId = null; actions = SessionStateActions.None; var session = this.cachedEx.Get <SessionItem>(id).Value; if (session == null) { return(null); } actions = session.ActionFlag; if (session.Locked) { locked = true; lockId = session.LockId; lockAge = DateTime.UtcNow - session.LockTime; return(null); } if (isExclusive == true) { locked = session.Locked = true; session.LockTime = DateTime.UtcNow; lockAge = TimeSpan.Zero; lockId = ++session.LockId; } session.ActionFlag = SessionStateActions.None; this.cachedEx.Set(id, session, TimeSpan.FromMinutes(session.TimeOut)); var staticObjects = SessionStateUtility.GetSessionStaticObjects(context); var sessionCollection = actions == SessionStateActions.InitializeItem ? new SessionStateItemCollection() : SessionSerializer.Deserialize(session.Binary); return(new SessionStateStoreData(sessionCollection, staticObjects, session.TimeOut)); }