Esempio n. 1
0
 /// <summary>
 /// 设置超时时间
 /// </summary>
 /// <param name="context"></param>
 /// <param name="id"></param>
 public override void ResetItemTimeout(HttpContext context, string id)
 {
     using (SessionStateEF db = new SessionStateEF())
     {
         var session = db.ASPStateTempSessions.Find(id);
         if (session == null)
         {
             return;
         }
         session.Expires = DateTime.Now + _expiresTime;
         db.SaveChanges();
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 新建或者释放锁定的项并设置Session的值
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="item"></param>
        /// <param name="lockId"></param>
        /// <param name="newItem"></param>
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            using (SessionStateEF db = new SessionStateEF())
            {
                // 判断是否是新建,如果是新建则和CreateUninitializedItem不同在于Timeout和有初始值。
                if (newItem)
                {
                    var session = new ASPStateTempSessions
                    {
                        Created     = DateTime.Now,
                        Expires     = DateTime.Now.AddMinutes(item.Timeout),
                        Flags       = (int)SessionStateActions.None,
                        LockDate    = DateTime.Now,
                        Locked      = false,
                        SessionId   = id,
                        LockId      = 0,
                        Timeout     = item.Timeout,
                        SessionItem = Serialize((SessionStateItemCollection)item.Items)
                    };
                    db.ASPStateTempSessions.Add(session);
                    db.SaveChanges();
                }
                else// 释放锁定的项并设置Session的值
                {
                    var session = db.ASPStateTempSessions.FirstOrDefault(i => i.SessionId == id);
                    if (session == null)
                    {
                        return;
                    }

                    session.Expires     = DateTime.Now.AddMinutes(item.Timeout);
                    session.Locked      = false;
                    session.LockId      = Convert.ToInt32(lockId);
                    session.SessionItem = Serialize((SessionStateItemCollection)item.Items);
                    db.SaveChanges();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 删除Session,会在Session.Abandon()的时候调用
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="lockId"></param>
        /// <param name="item"></param>
        public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
        {
            using (SessionStateEF db = new SessionStateEF())
            {
                var session = db.ASPStateTempSessions.Find(id);
                if (session == null)
                {
                    return;
                }

                db.ASPStateTempSessions.Remove(session);
                db.SaveChanges();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 释放锁定的项,就是把锁定的Session的锁的状态清除掉
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="lockId"></param>
        public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
        {
            using (SessionStateEF db = new SessionStateEF())
            {
                var session = db.ASPStateTempSessions.Find(id);
                if (session == null)
                {
                    return;
                }

                // 把locked设置为false
                session.Locked  = false;
                session.Expires = DateTime.Now + _expiresTime;
                db.SaveChanges();
            }
        }
Esempio n. 5
0
 /// <summary>
 /// 创建未初始化的项,就是初始化Session数据
 /// </summary>
 /// <param name="context"></param>
 /// <param name="id"></param>
 /// <param name="timeout"></param>
 public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
 {
     using (SessionStateEF db = new SessionStateEF())
     {
         var session = new ASPStateTempSessions
         {
             Created   = DateTime.Now,
             Expires   = DateTime.Now.AddMinutes(timeout),
             Flags     = (int)SessionStateActions.InitializeItem,
             LockDate  = DateTime.Now,
             Locked    = false,
             SessionId = id,
             LockId    = 0,
             Timeout   = timeout
         };
         db.ASPStateTempSessions.Add(session);
         db.SaveChanges();
     }
 }