public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item) { var redis = GetRedisConnection(); var getLock = redis.Hashes.Get(0, lockHashKey, id); var lockIdAsBytes = (byte[])lockId; LockData lockData; if (getLock.Result != null && LockData.TryParse(getLock.Result, out lockData) && Enumerable.SequenceEqual(lockData.LockId, lockIdAsBytes)) { redis.Keys.Remove(0, GetKeyForSessionId(id)); redis.Hashes.Remove(0, lockHashKey, id); } }
public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { var redis = GetRedisConnection(); var getLock = redis.Hashes.Get(0, lockHashKey, id); var lockIdAsBytes = (byte[])lockId; var ms = new MemoryStream(); var writer = new BinaryWriter(ms); if (item.Items as SessionStateItemCollection != null) { ((SessionStateItemCollection)item.Items).Serialize(writer); } writer.Close(); byte[] sessionData = ms.ToArray(); var sessionItemHash = new Dictionary <string, byte[]>(); sessionItemHash.Add("initialize", new byte[] { 0 }); sessionItemHash.Add("data", sessionData); sessionItemHash.Add("timeoutMinutes", BitConverter.GetBytes(item.Timeout)); LockData lockData; getLock.Wait(); if (getLock.Result == null) { redis.Hashes.Set(0, GetKeyForSessionId(id), sessionItemHash, false); } else if (LockData.TryParse(getLock.Result, out lockData) && Enumerable.SequenceEqual(lockData.LockId, lockIdAsBytes)) { redis.Hashes.Set(0, GetKeyForSessionId(id), sessionItemHash, false); redis.Hashes.Remove(0, lockHashKey, id); } }
public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) { var redis = GetRedisConnection(); var getLockData = redis.Hashes.Get(0, lockHashKey, id); actions = SessionStateActions.None; locked = false; lockId = null; lockAge = TimeSpan.MinValue; getLockData.Wait(); var rawLockData = getLockData.Result; if (rawLockData == null) { var lockData = LockData.New(); using (var trans = redis.CreateTransaction()) { var setLock = trans.Hashes.SetIfNotExists(0, lockHashKey, id, lockData.ToByteArray()); var getSessionData = trans.Hashes.GetAll(0, GetKeyForSessionId(id)); trans.Execute(); setLock.Wait(); if (setLock.Result) { locked = true; lockAge = new TimeSpan(0); lockId = lockData.LockId; var sessionItems = new SessionStateItemCollection(); var sessionDataHash = redis.Wait(getSessionData); if (sessionDataHash.Count == 3) { actions = sessionDataHash["initialize"][0] == 1 ? SessionStateActions.InitializeItem : SessionStateActions.None; var ms = new MemoryStream(sessionDataHash["data"]); if (ms.Length > 0) { var reader = new BinaryReader(ms); sessionItems = SessionStateItemCollection.Deserialize(reader); } var timeoutMinutes = BitConverter.ToInt32(sessionDataHash["timeoutMinutes"], 0); redis.Keys.Expire(0, GetKeyForSessionId(id), timeoutMinutes * 60); } return(new SessionStateStoreData(sessionItems, SessionStateUtility.GetSessionStaticObjects(context), redisConfig.SessionTimeout)); } else { rawLockData = redis.Hashes.Get(0, lockHashKey, id).Result; if (rawLockData != null && LockData.TryParse(rawLockData, out lockData)) { locked = true; lockId = lockData.LockId; lockAge = DateTime.UtcNow - lockData.LockUtcTime; } return(null); } } } else { LockData lockData; if (LockData.TryParse(rawLockData, out lockData)) { locked = true; lockId = lockData.LockId; lockAge = DateTime.UtcNow - lockData.LockUtcTime; } return(null); } }