public void ClearExpiredData() { //算出总在线人数(包括会员和游客) int totalOnline = m_OnlineMembers.Count + m_OnlineGuests.Count; //现在是史上最高在线,记录之 if (VarsManager.Stat.MaxOnlineCount < totalOnline) VarsManager.UpdateMaxOnline(totalOnline, DateTimeUtil.Now); UserBO userbo = UserBO.Instance; //============================================ //开始处理会员在线列表 //============================================ //超时的时间,在这个时间后都没有活动过的,属于已经超时 DateTime expiresDateTime = DateTimeUtil.Now.AddMinutes(0 - OnlineSettings.OverTime); List<List<UserOnlineInfo>> allOnlineInfos = new List<List<UserOnlineInfo>>(); List<UserOnlineInfo> onlineInfos = new List<UserOnlineInfo>(); //循环所有的在线会员 foreach (OnlineMember member in m_OnlineMembers) { //收集超时的会员,以备后续更新这些用户的在线时长,并同时从列表中移除 if (member.UpdateDate < expiresDateTime) { #region 收集要更新的在线时长信息 //收集应该被移除的userID、在线时间和最后更新的时间 UserOnlineInfo info = new UserOnlineInfo(); info.UserID = member.UserID; //在线时间只记创建时间和最后更新时间的时间长度 info.OnlineMinutes = (int)(member.UpdateDate - member.CreateDate).TotalMinutes; info.UpdateDate = member.UpdateDate; onlineInfos.Add(info); if (onlineInfos.Count >= 300) { allOnlineInfos.Add(onlineInfos); onlineInfos = new List<UserOnlineInfo>(); } #endregion //从在线列表中移除 RemoveOnlineMember(member.UserID); } } //将最后一次循环收集到的在线时长信息加入总列表 if (onlineInfos.Count > 0) allOnlineInfos.Add(onlineInfos); //============================================ //开始处理游客在线列表 //============================================ //更新一下超时的时间,因为之前的循环操作已经花掉一些时间,不更新将造成较大延迟 expiresDateTime = DateTimeUtil.Now.AddMinutes(0 - OnlineSettings.OverTime); //lock (m_GuestIps_Locker) //{ foreach (OnlineGuest guest in m_OnlineGuests) { if (guest.UpdateDate < expiresDateTime) { //OnlineGuestsInIp tempOnlineGuestIp; //if (m_OnlineGuestsInIps.TryGetValue(guest.IP, out tempOnlineGuestIp)) //{ // tempOnlineGuestIp.GuestIds.Remove(guest.GuestID); // if (tempOnlineGuestIp.GuestIds.Count == 0) // m_OnlineGuestsInIps.Remove(guest.IP); //} //移除 OnlineGuest RemoveOnlineGuest(guest.GuestID); } } //} //===================================== //开始对数据库的操作,主要是更新用户的在线时长 try { foreach (List<UserOnlineInfo> infos in allOnlineInfos) { userbo.UpdateOnlineTime300(infos); } userbo.ClearMostActiveUsersCache(new ActiveUserType[] { ActiveUserType.DayOnlineTime, ActiveUserType.WeekOnlineTime }); } catch (Exception ex) { LogHelper.CreateErrorLog(ex); } }
public void ClearExpiredData() { //算出总在线人数(包括会员和游客) int totalOnline = m_OnlineMembers.Count + m_OnlineGuests.Count; //现在是史上最高在线,记录之 if (VarsManager.Stat.MaxOnlineCount < totalOnline) { VarsManager.UpdateMaxOnline(totalOnline, DateTimeUtil.Now); } UserBO userbo = UserBO.Instance; //============================================ //开始处理会员在线列表 //============================================ //超时的时间,在这个时间后都没有活动过的,属于已经超时 DateTime expiresDateTime = DateTimeUtil.Now.AddMinutes(0 - OnlineSettings.OverTime); List <List <UserOnlineInfo> > allOnlineInfos = new List <List <UserOnlineInfo> >(); List <UserOnlineInfo> onlineInfos = new List <UserOnlineInfo>(); //循环所有的在线会员 foreach (OnlineMember member in m_OnlineMembers) { //收集超时的会员,以备后续更新这些用户的在线时长,并同时从列表中移除 if (member.UpdateDate < expiresDateTime) { #region 收集要更新的在线时长信息 //收集应该被移除的userID、在线时间和最后更新的时间 UserOnlineInfo info = new UserOnlineInfo(); info.UserID = member.UserID; //在线时间只记创建时间和最后更新时间的时间长度 info.OnlineMinutes = (int)(member.UpdateDate - member.CreateDate).TotalMinutes; info.UpdateDate = member.UpdateDate; onlineInfos.Add(info); if (onlineInfos.Count >= 300) { allOnlineInfos.Add(onlineInfos); onlineInfos = new List <UserOnlineInfo>(); } #endregion //从在线列表中移除 RemoveOnlineMember(member.UserID); } } //将最后一次循环收集到的在线时长信息加入总列表 if (onlineInfos.Count > 0) { allOnlineInfos.Add(onlineInfos); } //============================================ //开始处理游客在线列表 //============================================ //更新一下超时的时间,因为之前的循环操作已经花掉一些时间,不更新将造成较大延迟 expiresDateTime = DateTimeUtil.Now.AddMinutes(0 - OnlineSettings.OverTime); //lock (m_GuestIps_Locker) //{ foreach (OnlineGuest guest in m_OnlineGuests) { if (guest.UpdateDate < expiresDateTime) { //OnlineGuestsInIp tempOnlineGuestIp; //if (m_OnlineGuestsInIps.TryGetValue(guest.IP, out tempOnlineGuestIp)) //{ // tempOnlineGuestIp.GuestIds.Remove(guest.GuestID); // if (tempOnlineGuestIp.GuestIds.Count == 0) // m_OnlineGuestsInIps.Remove(guest.IP); //} //移除 OnlineGuest RemoveOnlineGuest(guest.GuestID); } } //} //===================================== //开始对数据库的操作,主要是更新用户的在线时长 try { foreach (List <UserOnlineInfo> infos in allOnlineInfos) { userbo.UpdateOnlineTime300(infos); } userbo.ClearMostActiveUsersCache(new ActiveUserType[] { ActiveUserType.DayOnlineTime, ActiveUserType.WeekOnlineTime }); } catch (Exception ex) { LogHelper.CreateErrorLog(ex); } }