Ejemplo n.º 1
0
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            // Serialize the SessionStateItemCollection as a string.
            string sessionItems = this.Serialize((SessionStateItemCollection)item.Items);

            using (IRedisClient redisClient = this.CreateRedisClient())
            {
                try
                {
                    if (newItem)
                    {
                        SessionItem currentSessionItem = redisClient.Get<SessionItem>(this.GetRedisKey(id));

                        if (currentSessionItem != null && currentSessionItem.Expires < DateTime.Now)
                        {
                            redisClient.Delete<SessionItem>(currentSessionItem);
                        }

                        SessionItem sessionItem = new SessionItem()
                        {
                            SessionId = id,
                            ApplicationName = this.ApplicationName,
                            CreatedAt = DateTime.Now,
                            Expires = DateTime.Now.AddMinutes((Double)item.Timeout),
                            Timeout = item.Timeout,
                            LockId = 0,
                            LockDate = DateTime.Now,
                            Locked = false,
                            SessionItems = sessionItems,
                            Flags = 0
                        };

                        redisClient.Set<SessionItem>(this.GetRedisKey(id), sessionItem, DateTime.Now.AddMinutes(item.Timeout));
                    }
                    else
                    {
                        SessionItem currentSessionItem = redisClient.Get<SessionItem>(this.GetRedisKey(id));

                        if (currentSessionItem != null && currentSessionItem.LockId == (int?)lockId)
                        {
                            currentSessionItem.Locked = false;
                            currentSessionItem.SessionItems = sessionItems;
                            redisClient.Set<SessionItem>(this.GetRedisKey(id), currentSessionItem, DateTime.UtcNow.AddMinutes(item.Timeout));
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
 
        }
Ejemplo n.º 2
0
        public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
        {
            using (IRedisClient redisClient = this.CreateRedisClient())
            {
                try
                {
                    SessionItem sessionItem = new SessionItem()
                    {
                        SessionId = id,
                        ApplicationName = this.ApplicationName,
                        CreatedAt = DateTime.Now,
                        Expires = DateTime.Now.AddMinutes((Double)timeout),
                        Timeout = timeout,
                        LockId = 0,
                        LockDate = DateTime.Now,
                        Locked = true,
                        SessionItems = string.Empty,
                        Flags = 1
                    };

                    redisClient.Set<SessionItem>(this.GetRedisKey(id), sessionItem, DateTime.Now.AddMinutes(timeout));
                }
                catch (Exception e) 
                {
                    throw e;
                }
            }
        }