コード例 #1
0
        private SessionStateItem Get(HttpContext context, bool acquireLock, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
        {
            locked  = false;
            lockId  = null;
            lockAge = TimeSpan.Zero;
            actions = SessionStateActions.None;

            var e = SessionStateItem.Load(this.client, id, false);

            if (e == null)
            {
                return(null);
            }

            if (acquireLock)
            {
                // repeat until we can update the retrieved
                // item (i.e. nobody changes it between the
                // time we get it from the store and updates its attributes)
                // Save() will return false if Cas() fails
                while (true)
                {
                    if (e.LockId > 0)
                    {
                        break;
                    }

                    actions = e.Flag;

                    e.LockId   = e.HeadCas;
                    e.LockTime = DateTime.UtcNow;
                    e.Flag     = SessionStateActions.None;

                    // try to update the item in the store
                    if (e.Save(this.client, id, true, true))
                    {
                        locked = true;
                        lockId = e.LockId;

                        return(e);
                    }

                    // it has been modifed between we loaded and tried to save it
                    e = SessionStateItem.Load(this.client, id, false);
                    if (e == null)
                    {
                        return(null);
                    }
                }
            }

            locked  = true;
            lockAge = DateTime.UtcNow - e.LockTime;
            lockId  = e.LockId;
            actions = SessionStateActions.None;

            return(acquireLock ? null : e);
        }
コード例 #2
0
        public override void ResetItemTimeout(HttpContext context, string id)
        {
            var e = SessionStateItem.Load(this.client, id, false);

            if (e != null)
            {
                e.Save(this.client, id, false, true);
            }
        }
コード例 #3
0
        public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
        {
            if (!(lockId is ulong))
            {
                return;
            }

            var tmp = (ulong)lockId;
            var e   = SessionStateItem.Load(this.client, id, true);

            if (e != null && e.LockId == tmp)
            {
                SessionStateItem.Remove(this.client, id);
            }
        }
コード例 #4
0
        public void Load_WhenNotSuccess_ReturnNull(ResponseStatus status)
        {
            //arrange
            var result = new Mock <IOperationResult <byte[]> >();

            result.Setup(x => x.Status).Returns(status);

            var bucket = new Mock <IBucket>();

            bucket.Setup(x => x.Get <byte[]>(It.IsAny <string>())).Returns(result.Object);

            //act
            var item = SessionStateItem.Load(bucket.Object, "thekey", false);

            //assert
            Assert.IsNull(item);
        }
コード例 #5
0
        public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
        {
            if (!(lockId is ulong))
            {
                return;
            }

            var tmp = (ulong)lockId;
            var e   = SessionStateItem.Load(this.client, id, true);

            if (e != null && e.LockId == tmp)
            {
                e.LockId   = 0;
                e.LockTime = DateTime.MinValue;

                e.Save(this.client, id, true, true);
            }
        }
コード例 #6
0
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            SessionStateItem e        = null;
            bool             existing = false;

            if (!newItem)
            {
                if (!(lockId is ulong))
                {
                    return;
                }

                var tmp = (ulong)lockId;
                e        = SessionStateItem.Load(this.client, id, true);
                existing = e != null;

                // if we're expecting an existing item, but
                // it's not in the cache
                // or it's not locked
                // or it's locked by someone else, then quit
                if (!newItem &&
                    (!existing ||
                     e.LockId == 0 ||
                     e.LockId != tmp))
                {
                    return;
                }
            }

            if (!existing)
            {
                e = new SessionStateItem();
            }

            // set the new data and reset the locks
            e.Timeout  = item.Timeout;
            e.Data     = (SessionStateItemCollection)item.Items;
            e.Flag     = SessionStateActions.None;
            e.LockId   = 0;
            e.LockTime = DateTime.MinValue;

            e.Save(this.client, id, false, existing && !newItem);
        }