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"]);
        }
Beispiel #2
0
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            using (var client = GetClient())
            {
                if (newItem)
                {
                    var state = new RedisSessionState()
                    {
                        Items   = (SessionStateItemCollection)item.Items,
                        Timeout = item.Timeout,
                    };

                    var key = GetSessionIdKey(id);
                    UpdateSessionState(client, key, state);
                }
                else
                {
                    UpdateSessionStateIfLocked(client, id, (int)lockId, state =>
                    {
                        state.Items   = (SessionStateItemCollection)item.Items;
                        state.Locked  = false;
                        state.Timeout = item.Timeout;
                    });
                }
            }
        }
        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 ToMap()
        {
            var data = new RedisSessionState()
            {
                Created  = new DateTime(2011, 12, 22, 1, 1, 1, DateTimeKind.Utc),
                Locked   = true,
                LockId   = 999,
                LockDate = new DateTime(2011, 11, 22, 1, 1, 1, DateTimeKind.Utc),
                Timeout  = 3,
                Flags    = SessionStateActions.InitializeItem
            };

            data.Items["name"] = "Felix";
            data.Items["age"]  = 1;

            var map = data.ToMap();

            Assert.Equal(date1Bytes, map["created"]);
            Assert.Equal(new byte[] { 1 }, map["locked"]);
            Assert.Equal(lockIdBytes, map["lockId"]);
            Assert.Equal(date2Bytes, map["lockDate"]);
            Assert.Equal(new byte[] { 3, 0, 0, 0 }, map["timeout"]);
            Assert.Equal(new byte[] { 1, 0, 0, 0 }, map["flags"]);
            Assert.Equal(itemsBytes, map["items"]);
        }
        public static RedisSessionState GetSessionState(this IRedisClient redis, string key)
        {
            RedisSessionState state = null;

            RedisSessionState.TryParse(redis.GetAllEntriesFromHashRaw(key), out state);
            return(state);
        }
Beispiel #6
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);
        }
Beispiel #7
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));
                        }
                    });
                }
        }
Beispiel #8
0
 private void UpdateSessionState(IRedisClient client, string key, RedisSessionState state)
 {
     UseTransaction(client, transaction =>
     {
         transaction.QueueCommand(c => c.SetRangeInHashRaw(key, state.ToMap()));
         transaction.QueueCommand(c => c.ExpireEntryIn(key, TimeSpan.FromMinutes(state.Timeout)));
     });
 }
Beispiel #9
0
        public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
        {
            var key = GetSessionIdKey(id);

            tempSessionTimeout = timeout;
            using (var client = GetClient())
            {
                var state = new RedisSessionState()
                {
                    Timeout = timeout,
                    Flags   = SessionStateActions.InitializeItem
                };

                UpdateSessionState(client, key, state);
            }
        }
Beispiel #10
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 ToMap()
        {
            var data = new RedisSessionState()
            {
                Created = new DateTime(2011, 12, 22, 1, 1, 1, DateTimeKind.Utc),
                Locked = true,
                LockId = 999,
                LockDate = new DateTime(2011, 11, 22, 1, 1, 1, DateTimeKind.Utc),
                Timeout = 3,
                Flags = SessionStateActions.InitializeItem
            };

            data.Items["name"] = "Felix";
            data.Items["age"] = 1;

            var map = data.ToMap();
            Assert.Equal(date1Bytes, map["created"]);
            Assert.Equal(new byte[] { 1 }, map["locked"]);
            Assert.Equal(lockIdBytes, map["lockId"]);
            Assert.Equal(date2Bytes, map["lockDate"]);
            Assert.Equal(new byte[] { 3, 0, 0, 0 }, map["timeout"]);
            Assert.Equal(new byte[] { 1, 0, 0, 0 }, map["flags"]);
            Assert.Equal(itemsBytes, map["items"]);
        }
        public void TryParse_should_fail_if_null_data()
        {
            RedisSessionState data;

            Assert.False(RedisSessionState.TryParse(null, out data));
        }
 public static void SetSessionState(this IRedisClient redis, string key, RedisSessionState state)
 {
     redis.SetRangeInHashRaw(key, state.ToMap());
 }
 public static void SetSessionState(this IRedisClient redis, string key, RedisSessionState state)
 {
     redis.SetRangeInHashRaw(key, state.ToMap());
 }