public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { var key = this.GetSessionIdKey(id); using (var client = this.GetClientAndWatch(key)) { if (newItem) { var state = new RedisSessionState() { Items = (SessionStateItemCollection)item.Items, Timeout = item.Timeout, }; this.UpdateSessionState(client, key, state); } else { this.UpdateSessionStateIfLocked(client, key, (int)lockId, state => { state.Items = (SessionStateItemCollection)item.Items; state.Locked = false; state.Timeout = item.Timeout; }); } } }
private void UpdateSessionState(IRedisClient client, string key, RedisSessionState state) { 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(); } }
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 CreateUninitializedItem(HttpContext context, string id, int timeout) { var key = this.GetSessionIdKey(id); using (var client = this.GetClientAndWatch(key)) { var state = new RedisSessionState() { Timeout = timeout, Flags = SessionStateActions.InitializeItem }; this.UpdateSessionState(client, key, state); } }
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(); } } }
public static bool TryParse(IDictionary <string, byte[]> raw, out RedisSessionState data) { if (raw == null || raw.Count != 7) { data = null; return(false); } SessionStateItemCollection sessionItems; using (var ms = new MemoryStream(raw["items"])) { if (ms.Length > 0) { using (var reader = new BinaryReader(ms)) { sessionItems = SessionStateItemCollection.Deserialize(reader); } } else { sessionItems = new SessionStateItemCollection(); } } data = new RedisSessionState() { Created = new DateTime(BitConverter.ToInt64(raw["created"], 0)), Locked = BitConverter.ToBoolean(raw["locked"], 0), LockId = raw["lockId"].Length == 0 ? 0 : BitConverter.ToInt32(raw["lockId"], 0), LockDate = raw["lockDate"].Length == 0 ? DateTime.MinValue : new DateTime(BitConverter.ToInt64(raw["lockDate"], 0)), Timeout = BitConverter.ToInt32(raw["timeout"], 0), Flags = (SessionStateActions)BitConverter.ToInt32(raw["flags"], 0), Items = sessionItems }; return(true); }
public static bool TryParse(IDictionary<string, byte[]> raw, out RedisSessionState data) { if (raw == null || raw.Count != 7) { data = null; return false; } SessionStateItemCollection sessionItems; using (var ms = new MemoryStream(raw["items"])) { if (ms.Length > 0) { using (var reader = new BinaryReader(ms)) { sessionItems = SessionStateItemCollection.Deserialize(reader); } } else { sessionItems = new SessionStateItemCollection(); } } data = new RedisSessionState() { Created = new DateTime(BitConverter.ToInt64(raw["created"], 0)), Locked = BitConverter.ToBoolean(raw["locked"], 0), LockId = raw["lockId"].Length == 0 ? 0 : BitConverter.ToInt32(raw["lockId"], 0), LockDate = raw["lockDate"].Length == 0 ? DateTime.MinValue : new DateTime(BitConverter.ToInt64(raw["lockDate"], 0)), Timeout = BitConverter.ToInt32(raw["timeout"], 0), Flags = (SessionStateActions)BitConverter.ToInt32(raw["flags"], 0), Items = sessionItems }; return true; }
private void UpdateSessionState(IRedisClient client, string key, RedisSessionState state) { 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(); } }
public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { var key = this.GetSessionIdKey(id); using (var client = this.GetClientAndWatch(key)) { if (newItem) { var state = new RedisSessionState() { Items = (SessionStateItemCollection)item.Items, Timeout = item.Timeout, }; this.UpdateSessionState(client, key, state); } else { this.UpdateSessionStateIfLocked(client, key, (int)lockId, state => { state.Items = (SessionStateItemCollection)item.Items; state.Locked = false; state.Timeout = item.Timeout; }); } } }
public override void CreateUninitializedItem(HttpContext context, string id, int timeout) { var key = this.GetSessionIdKey(id); using (var client = this.GetClientAndWatch(key)) { var state = new RedisSessionState() { Timeout = timeout, Flags = SessionStateActions.InitializeItem }; this.UpdateSessionState(client, key, state); } }