private void UpdateSessionStateIfLocked(IRedisClient client, string key, int lockId, Action <RedisSessionState> stateAction) { var stateRaw = client.GetAllEntriesFromHashRaw(key); RedisSessionState state; if (RedisSessionState.TryParse(stateRaw, out state) && state.Locked && state.LockId == (int)lockId) { stateAction(state); this.UpdateSessionState(client, key, state); } }
private SessionStateStoreData GetItem(bool isExclusive, HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) { var key = this.GetSessionIdKey(id); locked = false; lockAge = TimeSpan.Zero; lockId = null; actions = SessionStateActions.None; using (var client = this.GetClientAndWatch(key)) { var stateRaw = client.GetAllEntriesFromHashRaw(key); RedisSessionState state; if (!RedisSessionState.TryParse(stateRaw, out state)) { client.UnWatch(); return(null); } actions = state.Flags; var items = actions == SessionStateActions.InitializeItem ? new SessionStateItemCollection() : state.Items; if (state.Locked) { client.UnWatch(); locked = true; lockId = state.LockId; lockAge = DateTime.UtcNow - state.LockDate; return(null); } if (isExclusive) { locked = state.Locked = true; state.LockDate = DateTime.UtcNow; lockAge = TimeSpan.Zero; lockId = ++state.LockId; } state.Flags = SessionStateActions.None; using (var t = client.CreateTransaction()) { t.QueueCommand(c => c.SetRangeInHashRaw(key, state.ToMap())); t.QueueCommand(c => c.ExpireEntryIn(key, TimeSpan.FromMinutes(state.Timeout))); t.Commit(); } return(new SessionStateStoreData(items, this.staticObjectsGetter(context), state.Timeout)); } }
public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item) { var key = this.GetSessionIdKey(id); using (var client = this.GetClientAndWatch(key)) { var stateRaw = client.GetAllEntriesFromHashRaw(key); using (var transaction = client.CreateTransaction()) { RedisSessionState state; if (RedisSessionState.TryParse(stateRaw, out state) && state.Locked && state.LockId == (int)lockId) { transaction.QueueCommand(c => c.Remove(key)); } transaction.Commit(); } } }