/// <summary> /// Returns read-only session-state data from the session data store. /// </summary> /// <param name="context">The HttpContext for the current request.</param> /// <param name="id">The SessionID for the current request.</param> /// <param name="locked">When this method returns, contains a Boolean value that is set to true if a lock is successfully obtained; otherwise, false.</param> /// <param name="lockAge">When this method returns, contains a TimeSpan object that is set to the amount of time that an item in the session data store has been locked.</param> /// <param name="lockId">When this method returns, contains an object that is set to the lock identifier for the current request. For details on the lock identifier, see "Locking Session-Store Data" in the SessionStateStoreProviderBase class summary.</param> /// <param name="actions">When this method returns, contains one of the SessionStateActions values, indicating whether the current session is an uninitialized, cookieless session.</param> /// <returns>A SessionStateStoreData populated with session values and information from the session data store.</returns> public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) { var cacheKey = string.Format(_cacheKey, id, _applicationName); var now = DateTime.Now; // Get the current session DacheSessionState currentSession = null; _cacheClient.TryGet <DacheSessionState>(cacheKey, out currentSession); // Obtain a lock if possible. Ignore the record if it is expired. // Set locked to true if the record was not updated and false if it was locked = !(currentSession != null && !currentSession.Locked && currentSession.Expires > now); if (!locked) { currentSession.Locked = true; currentSession.LockDate = now; _cacheClient.AddOrUpdate(cacheKey, currentSession); } return(GetItem(context, id, out locked, out lockAge, out lockId, out actions)); }
/// <summary> /// Updates the expiration date and time of an item in the session data store. /// </summary> /// <param name="context">The HttpContext for the current request.</param> /// <param name="id">The session identifier for the current request.</param> public override void ResetItemTimeout(HttpContext context, string id) { var cacheKey = string.Format(_cacheKey, id, _applicationName); DacheSessionState currentSession = null; if (_cacheClient.TryGet <DacheSessionState>(cacheKey, out currentSession)) { currentSession.Expires = DateTime.Now.AddMinutes(_sessionStateSection.Timeout.TotalMinutes); _cacheClient.AddOrUpdate(cacheKey, currentSession); } }
/// <summary> /// Deletes item data from the session data store. /// </summary> /// <param name="context">The HttpContext for the current request.</param> /// <param name="id">The session identifier for the current request.</param> /// <param name="lockId">The lock identifier for the current request.</param> /// <param name="item">The SessionStateStoreData that represents the item to delete from the data store.</param> public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item) { var cacheKey = string.Format(_cacheKey, id, _applicationName); var now = DateTime.Now; // Get the current session DacheSessionState currentSession = null; if (_cacheClient.TryGet <DacheSessionState>(cacheKey, out currentSession)) { if (currentSession.LockId == (int)lockId) { _cacheClient.Remove(cacheKey); } } }
/// <summary> /// Releases a lock on an item in the session data store. /// </summary> /// <param name="context">The HttpContext for the current request.</param> /// <param name="id">The session identifier for the current request.</param> /// <param name="lockId">The lock identifier for the current request.</param> public override void ReleaseItemExclusive(HttpContext context, string id, object lockId) { var cacheKey = string.Format(_cacheKey, id, _applicationName); var now = DateTime.Now; // Get the current session DacheSessionState currentSession = null; if (_cacheClient.TryGet <DacheSessionState>(cacheKey, out currentSession)) { if (currentSession.LockId == (int)lockId) { currentSession.Locked = false; currentSession.Expires = now.AddMinutes(_sessionStateSection.Timeout.TotalMinutes); _cacheClient.AddOrUpdate(cacheKey, currentSession); } } }
/// <summary> /// Updates the session-item information in the session-state data store with values from the current request, and clears the lock on the data. /// </summary> /// <param name="context">The HttpContext for the current request.</param> /// <param name="id">The session identifier for the current request.</param> /// <param name="item">The SessionStateStoreData object that contains the current session values to be stored.</param> /// <param name="lockId">The lock identifier for the current request.</param> /// <param name="newItem">true to identify the session item as a new item; false to identify the session item as an existing item.</param> public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { // Serialize the SessionStateItemCollection as a string var sessionItems = (SessionStateItemCollection)item.Items; byte[] serializedItems = new byte[0]; if (sessionItems != null) { using (var memoryStream = new MemoryStream()) { using (var writer = new BinaryWriter(memoryStream)) { sessionItems.Serialize(writer); writer.Close(); serializedItems = memoryStream.ToArray(); } } } var now = DateTime.UtcNow; var cacheKey = string.Format(_cacheKey, id, _applicationName); // Try and get the existing session item DacheSessionState currentSession = null; bool success = _cacheClient.TryGet <DacheSessionState>(cacheKey, out currentSession); // Create or update the session item _cacheClient.AddOrUpdate(cacheKey, new DacheSessionState { SessionId = id, ApplicationName = _applicationName, Created = success ? currentSession.Created : now, Expires = now.AddMinutes(item.Timeout), LockDate = success ? currentSession.LockDate : now, LockId = success ? currentSession.LockId : 0, Timeout = success ? currentSession.Timeout : item.Timeout, Locked = false, SessionItems = serializedItems, Flags = success ? currentSession.Flags : SessionStateActions.None }); }
/// <summary> /// Returns read-only session-state data from the session data store. /// </summary> /// <param name="context">The HttpContext for the current request.</param> /// <param name="id">The SessionID for the current request.</param> /// <param name="locked">When this method returns, contains a Boolean value that is set to true if the requested session item is locked at the session data store; otherwise, false.</param> /// <param name="lockAge">When this method returns, contains a TimeSpan object that is set to the amount of time that an item in the session data store has been locked.</param> /// <param name="lockId">When this method returns, contains an object that is set to the lock identifier for the current request. For details on the lock identifier, see "Locking Session-Store Data" in the SessionStateStoreProviderBase class summary.</param> /// <param name="actions">When this method returns, contains one of the SessionStateActions values, indicating whether the current session is an uninitialized, cookieless session.</param> /// <returns>A SessionStateStoreData populated with session values and information from the session data store.</returns> public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) { // Initial values for return value and out parameters SessionStateStoreData item = null; lockAge = TimeSpan.Zero; lockId = null; locked = false; actions = 0; // String to hold serialized SessionStateItemCollection byte[] serializedItems = null; // True if a record is found in the database bool foundRecord = false; // Timeout value from the data store int timeout = 0; var cacheKey = string.Format(_cacheKey, id, _applicationName); var now = DateTime.Now; // Get the current session DacheSessionState currentSession = null; _cacheClient.TryGet <DacheSessionState>(cacheKey, out currentSession); if (currentSession != null) { if (currentSession.Expires < now) { // The record was expired - mark it as not locked locked = false; // Delete the current session _cacheClient.Remove(cacheKey); } else { foundRecord = true; } serializedItems = currentSession.SessionItems; lockId = currentSession.LockId; lockAge = now.Subtract(currentSession.LockDate); actions = currentSession.Flags; timeout = currentSession.Timeout; } // If the record was found and you obtained a lock, then set the lockId, clear the actions, and create the SessionStateStoreItem to return. if (foundRecord && !locked) { var incrementedLockId = (int)lockId + 1; lockId = incrementedLockId; currentSession.LockId = incrementedLockId; currentSession.Flags = SessionStateActions.None; // If the actions parameter is not InitializeItem, // deserialize the stored SessionStateItemCollection. if (actions == SessionStateActions.InitializeItem) { item = CreateNewStoreData(context, (int)_sessionStateSection.Timeout.TotalMinutes); } else { // Deserialize MemoryStream memoryStream = new MemoryStream(serializedItems); SessionStateItemCollection sessionItems = new SessionStateItemCollection(); if (memoryStream.Length > 0) { BinaryReader reader = new BinaryReader(memoryStream); sessionItems = SessionStateItemCollection.Deserialize(reader); } item = new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), timeout); } } return(item); }