Esempio n. 1
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(redisCfg.SessionTimeOut));
                            }
                            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(redisCfg.SessionTimeOut));

                        // 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);
        }