public void ReleaseIdleDBUserInfos(int ticksSlot) { long nowTicks = DateTime.Now.Ticks / 10000L; List <string> idleUserIDList = new List <string>(); lock (this.DictUserInfos) { foreach (MyWeakReference weakRef in this.DictUserInfos.Values) { if (weakRef.IsAlive) { DBUserInfo dbUserInfo = weakRef.Target as DBUserInfo; if (null != dbUserInfo) { if (nowTicks - dbUserInfo.LastReferenceTicks >= (long)ticksSlot) { idleUserIDList.Add(dbUserInfo.UserID); } } } } } for (int i = 0; i < idleUserIDList.Count; i++) { this.RemoveDBUserInfo(idleUserIDList[i]); LogManager.WriteLog(LogTypes.Info, string.Format("释放空闲的用户数据: {0}", idleUserIDList[i]), null, true); } }
public DBUserInfo FindDBUserInfo(string userID) { DBUserInfo dbUserInfo = null; MyWeakReference weakRef = null; lock (this.DictUserInfos) { if (this.DictUserInfos.Count > 0) { if (this.DictUserInfos.TryGetValue(userID, out weakRef)) { if (weakRef.IsAlive) { dbUserInfo = (weakRef.Target as DBUserInfo); } } } } if (null != dbUserInfo) { lock (dbUserInfo) { dbUserInfo.LastReferenceTicks = DateTime.Now.Ticks / 10000L; } } return(dbUserInfo); }
public DBUserInfo GetDBUserInfo(string userID) { DBUserInfo dbUserInfo = this._DBUserMgr.FindDBUserInfo(userID); if (null == dbUserInfo) { dbUserInfo = new DBUserInfo(); MySQLConnection conn = this._DBConns.PopDBConnection(); try { if (!dbUserInfo.Query(conn, userID)) { return(null); } } catch (Exception ex) { LogManager.WriteException(ex.ToString()); } finally { this._DBConns.PushDBConnection(conn); } dbUserInfo = this._DBUserMgr.AddDBUserInfo(dbUserInfo); } return(dbUserInfo); }
/// <summary> /// 获取指定的用户信息 /// </summary> /// <param name="userID"></param> /// <returns></returns> public DBUserInfo GetDBUserInfo(string userID) { DBUserInfo dbUserInfo = _DBUserMgr.FindDBUserInfo(userID); if (null == dbUserInfo) { dbUserInfo = new DBUserInfo(); MySQLConnection conn = _DBConns.PopDBConnection(); try { if (!dbUserInfo.Query(conn, userID)) { return(null); } } finally { _DBConns.PushDBConnection(conn); } //放入缓存 _DBUserMgr.AddDBUserInfo(dbUserInfo); } return(dbUserInfo); }
/// <summary> /// 添加用户信息到字典中 /// </summary> /// <param name="dbUserInfo"></param> public void AddDBUserInfo(DBUserInfo dbUserInfo) { MyWeakReference weakRef = null; lock (DictUserInfos) { if (DictUserInfos.TryGetValue(dbUserInfo.UserID, out weakRef)) { weakRef.Target = dbUserInfo; } else { DictUserInfos.Add(dbUserInfo.UserID, new MyWeakReference(dbUserInfo)); } } }
public DBUserInfo AddDBUserInfo(DBUserInfo dbUserInfo) { MyWeakReference weakRef = null; lock (this.DictUserInfos) { if (this.DictUserInfos.TryGetValue(dbUserInfo.UserID, out weakRef)) { DBUserInfo old = weakRef.Target as DBUserInfo; if (null != old) { return(old); } weakRef.Target = dbUserInfo; } else { this.DictUserInfos.Add(dbUserInfo.UserID, new MyWeakReference(dbUserInfo)); } } return(dbUserInfo); }