public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
        {
            using (RedisClient client = this.RedisSessionClient) {
                SessionItem sessionItem = new SessionItem();
                sessionItem.CreatedAt = DateTime.Now.ToUniversalTime();
                sessionItem.LockDate = DateTime.Now.ToUniversalTime();
                sessionItem.LockID = 0;
                sessionItem.Timeout = timeout;
                sessionItem.Locked = false;
                sessionItem.SessionItems = string.Empty;
                sessionItem.Flags = 0;

                client.Set<SessionItem>(this.RedisKey(id), sessionItem, DateTime.UtcNow.AddMinutes(timeout));
            }
        }
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            using (RedisClient client = this.RedisSessionClient)
            {
                // Serialize the SessionStateItemCollection as a string.
                string sessionItems = Serialize((SessionStateItemCollection)item.Items);

                try
                {
                    if (newItem)
                    {
                        SessionItem sessionItem = new SessionItem();
                        sessionItem.CreatedAt = DateTime.UtcNow;
                        sessionItem.LockDate = DateTime.UtcNow;
                        sessionItem.LockID = 0;
                        sessionItem.Timeout = item.Timeout;
                        sessionItem.Locked = false;
                        sessionItem.SessionItems = sessionItems;
                        sessionItem.Flags = 0;

                        client.Set<SessionItem>(this.RedisKey(id), sessionItem, DateTime.UtcNow.AddMinutes(item.Timeout));
                    }
                    else
                    {
                        SessionItem currentSessionItem = client.Get<SessionItem>(this.RedisKey(id));
                        if (currentSessionItem != null && currentSessionItem.LockID == (int?)lockId)
                        {
                            currentSessionItem.Locked = false;
                            currentSessionItem.SessionItems = sessionItems;
                            client.Set<SessionItem>(this.RedisKey(id), currentSessionItem, DateTime.UtcNow.AddMinutes(item.Timeout));
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
Exemplo n.º 3
0
        private SessionStateStoreData GetSessionStoreItem(bool lockRecord,
                                                          HttpContext context,
                                                          string id,
                                                          out bool locked,
                                                          out TimeSpan lockAge,
                                                          out object lockId,
                                                          out SessionStateActions actionFlags)
        {
            // Initial values for return value and out parameters.
            SessionStateStoreData item = null;

            lockAge     = TimeSpan.Zero;
            lockId      = null;
            locked      = false;
            actionFlags = 0;

            // String to hold serialized SessionStateItemCollection.
            string serializedItems = "";

            // Timeout value from the data store.
            int timeout = 0;

            using (RedisClient client = this.RedisSessionClient)
            {
                try
                {
                    if (lockRecord)
                    {
                        locked = false;
                        SessionItem currentItem = client.Get <SessionItem>(this.RedisKey(id));

                        if (currentItem != null)
                        {
                            // If the item is locked then do not attempt to update it
                            if (!currentItem.Locked)
                            {
                                currentItem.Locked   = true;
                                currentItem.LockDate = DateTime.UtcNow;
                                client.Set <SessionItem>(this.RedisKey(id), currentItem, DateTime.UtcNow.AddMinutes(sessionStateConfig.Timeout.TotalMinutes));
                            }
                            else
                            {
                                locked = true;
                            }
                        }
                    }

                    SessionItem currentSessionItem = client.Get <SessionItem>(this.RedisKey(id));

                    if (currentSessionItem != null)
                    {
                        serializedItems = currentSessionItem.SessionItems;
                        lockId          = currentSessionItem.LockID;
                        lockAge         = DateTime.UtcNow.Subtract(currentSessionItem.LockDate);
                        actionFlags     = (SessionStateActions)currentSessionItem.Flags;
                        timeout         = currentSessionItem.Timeout;
                    }
                    else
                    {
                        locked = false;
                    }

                    if (currentSessionItem != null && !locked)
                    {
                        // Delete the old item before inserting the new one
                        client.Remove(this.RedisKey(id));

                        lockId = (int?)lockId + 1;
                        currentSessionItem.LockID = lockId != null ? (int)lockId : 0;
                        currentSessionItem.Flags  = 0;

                        client.Set <SessionItem>(this.RedisKey(id), currentSessionItem, DateTime.UtcNow.AddMinutes(sessionStateConfig.Timeout.TotalMinutes));

                        // If the actionFlags parameter is not InitializeItem,
                        // deserialize the stored SessionStateItemCollection.
                        if (actionFlags == SessionStateActions.InitializeItem)
                        {
                            item = CreateNewStoreData(context, 30);
                        }
                        else
                        {
                            item = Deserialize(context, serializedItems, timeout);
                        }
                    }
                }

                catch (Exception e)
                {
                    throw e;
                }
            }

            return(item);
        }