/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <param name="timeout"></param> /// <remarks> /// checked /// </remarks> public override void CreateUninitializedItem(HttpContext context, string id, int timeout) { var model = new SessionStateList() { SessionModels = new SessionState[1] { new SessionState() { ApplicationName = applicationName, ExpireDate = DateTime.Now.AddMinutes(timeout), LockId = 0, SessionId = id, SessionText = "", IsLocked = false, Timeout = timeout, ActionFlag = SessionStateActions.InitializeItem } }, ExpireDate = DateTime.Now.AddMinutes(timeout), }; this.cache.Set(id, model, TimeSpan.FromMinutes(timeout)); }
/// <summary> /// checked /// </summary> /// <param name="context"></param> /// <param name="id"></param> /// <param name="item"></param> /// <param name="lockId"></param> /// <param name="newItem"></param> /// <remarks> /// checked /// </remarks> public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { string sessionText = new System.Func <string>(() => { var coll = (SessionStateItemCollection)item.Items; using (var output = new MemoryStream()) //using (var bin = new BinaryWriter(output)) { coll.Serialize(new BinaryWriter(output)); return(Convert.ToBase64String(output.ToArray())); } }).Invoke(); //存储Session对象模型 var model = this.cache.Get <SessionStateList>(id); if (model == null) { model = new SessionStateList() { ExpireDate = DateTime.Now.AddMinutes(item.Timeout) } } ; if (newItem || model.SessionModels == null || model.SessionModels.Count() == 0) { if (model.SessionModels != null) { //删除过期的对象 var list = (from n in model.SessionModels where n.ExpireDate > DateTime.Now select n).ToList(); model.SessionModels = list.ToArray(); } if (model.SessionModels == null) { model.SessionModels = new SessionState[0]; } var second = new[] { new SessionState() { SessionId = id, ApplicationName = applicationName, ExpireDate = DateTime.Now.AddMinutes(item.Timeout), LockId = 0, Timeout = item.Timeout, IsLocked = false, SessionText = sessionText } }; model.SessionModels = model.SessionModels.Union(second, EqualityComparer <SessionState> .Default).ToArray(); } else { if (model.SessionModels != null && lockId != null) { var newLockId = (long)lockId; foreach (var session in model.SessionModels) { if (session.LockId == newLockId) { session.SessionText = sessionText; session.IsLocked = false; session.ExpireDate = DateTime.Now.AddMinutes(item.Timeout); } } } } if ((model.ExpireDate - DateTime.Now).Minutes <= 0) { model.ExpireDate = DateTime.Now.AddMinutes(sessionCfg.Timeout.TotalMinutes); } this.cache.Set(id, model, model.ExpireDate - DateTime.Now); }