public void TryParse_should_fail_if_incorrect_length()
        {
            RedisSessionState data;
            var raw = new Dictionary <string, byte[]>();

            Assert.False(RedisSessionState.TryParse(raw, out data));
        }
        public void TryParse_should_pass_with_valid_data()
        {
            var raw = new Dictionary <string, byte[]>()
            {
                { "created", date1Bytes },
                { "locked", new byte[] { 1 } },
                { "lockId", lockIdBytes },
                { "lockDate", date2Bytes },
                { "timeout", new byte[] { 3, 0, 0, 0 } },
                { "flags", new byte[] { 1, 0, 0, 0 } },
                { "items", itemsBytes }
            };

            RedisSessionState data;

            Assert.True(RedisSessionState.TryParse(raw, out data));
            Assert.Equal(new DateTime(2011, 12, 22, 1, 1, 1, DateTimeKind.Utc), data.Created);
            Assert.True(data.Locked);
            Assert.Equal(999, data.LockId);
            Assert.Equal(new DateTime(2011, 11, 22, 1, 1, 1, DateTimeKind.Utc), data.LockDate);
            Assert.Equal(3, data.Timeout);
            Assert.Equal(SessionStateActions.InitializeItem, data.Flags);
            Assert.Equal(2, data.Items.Count);
            Assert.Equal("Felix", data.Items["name"]);
            Assert.Equal(1, data.Items["age"]);
        }
        public static RedisSessionState GetSessionState(this IRedisClient redis, string key)
        {
            RedisSessionState state = null;

            RedisSessionState.TryParse(redis.GetAllEntriesFromHashRaw(key), out state);
            return(state);
        }
Пример #4
0
        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;
            SessionStateStoreData result = null;

            var key = GetSessionIdKey(id);

            using (var client = GetClient())
                using (var distributedLock = GetDistributedLock(client, key))
                {
                    if (distributedLock.LockState == DistributedLock.LOCK_NOT_ACQUIRED)
                    {
                        options.OnDistributedLockNotAcquired(id);
                        return(null);
                    }

                    var stateRaw = client.GetAllEntriesFromHashRaw(key);

                    RedisSessionState state;
                    if (!RedisSessionState.TryParse(stateRaw, out state))
                    {
                        return(null);
                    }

                    actions = state.Flags;

                    if (state.Locked)
                    {
                        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;
                    tempSessionTimeout = state.Timeout;
                    UseTransaction(client, transaction =>
                    {
                        transaction.QueueCommand(c => c.SetRangeInHashRaw(key, state.ToMap()));
                        transaction.QueueCommand(c => c.ExpireEntryIn(key, TimeSpan.FromMinutes(state.Timeout)));
                    });

                    var items = actions == SessionStateActions.InitializeItem ? new SessionStateItemCollection() : state.Items;

                    result = new SessionStateStoreData(items, staticObjectsGetter(context), state.Timeout);
                }

            return(result);
        }
Пример #5
0
        public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
        {
            var key = GetSessionIdKey(id);

            using (var client = GetClient())
                using (var distributedLock = GetDistributedLock(client, key))
                {
                    if (distributedLock.LockState == DistributedLock.LOCK_NOT_ACQUIRED)
                    {
                        options.OnDistributedLockNotAcquired(id);
                        return;
                    }

                    var stateRaw = client.GetAllEntriesFromHashRaw(key);

                    UseTransaction(client, transaction =>
                    {
                        RedisSessionState state;
                        if (RedisSessionState.TryParse(stateRaw, out state) && state.Locked && state.LockId == (int)lockId)
                        {
                            transaction.QueueCommand(c => c.Remove(key));
                        }
                    });
                }
        }
Пример #6
0
        private void UpdateSessionStateIfLocked(IRedisClient client, string id, int lockId, Action <RedisSessionState> stateAction)
        {
            var key = GetSessionIdKey(id);

            using (var distributedLock = GetDistributedLock(client, key))
            {
                if (distributedLock.LockState == DistributedLock.LOCK_NOT_ACQUIRED)
                {
                    options.OnDistributedLockNotAcquired(id);
                    return;
                }

                var stateRaw = client.GetAllEntriesFromHashRaw(key);
                RedisSessionState state;
                if (RedisSessionState.TryParse(stateRaw, out state) && state.Locked && state.LockId == lockId)
                {
                    stateAction(state);
                    UpdateSessionState(client, key, state);
                }
            }
        }
        public void TryParse_should_fail_if_null_data()
        {
            RedisSessionState data;

            Assert.False(RedisSessionState.TryParse(null, out data));
        }