[ASP.NET]在线用户解决方案

上一篇 / 下一篇  2008-05-11 23:45:42 / 个人分类:转载

在线用户的统计方法有很多
其中一个就是SESSION保存在数据库,然后select tmpdb表,获取在线用户.
下面介绍一个非常有用的办法
试用Cache, Cache是服务器的方法,跟ASP的Application差不多,

下面是一个Cache在线用户的超作,
流程:建立一个DataTable 把需要的字段添加进去,
id,userid,username,place,lasttime
lasttime是最后活动的时间,这个时间是决定用户是否超时登陆,然后用一个方法来删除过时的用户

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Caching;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11.   
  12. /// <summary>  
  13. /// OnlineCache 的摘要说明  
  14. /// </summary>  
  15. public class OnlineCache  
  16. {  
  17.     public OnlineCache()  
  18.     {  
  19.           
  20.     }  
  21.   
  22.     #region 缓存在线用户  
  23.     /// <summary>  
  24.     /// 建立缓存表  
  25.     /// </summary>  
  26.     public static void BuildCacheOnlineTable()  
  27.     {  
  28.         DataTable dt = new DataTable();  
  29.         DataColumn col1 = dt.Columns.Add("ID"typeof(Int32));  
  30.         col1.AllowDBNull = false;  
  31.         col1.AutoIncrement = true;  
  32.         col1.AutoIncrementSeed = 1;  
  33.         col1.AutoIncrementStep = 1;  
  34.         col1.Unique = true;  
  35.   
  36.         DataColumn col2 = dt.Columns.Add("UserID"typeof(String));  
  37.         col2.AllowDBNull = true;  
  38.   
  39.         DataColumn col3 = dt.Columns.Add("UserName"typeof(String));  
  40.         col3.AllowDBNull = true;  
  41.   
  42.         DataColumn col4 = dt.Columns.Add("UserPlace"typeof(String));  
  43.         col4.AllowDBNull = true;  
  44.   
  45.         DataColumn col5 = dt.Columns.Add("IP"typeof(String));  
  46.         col5.AllowDBNull = true;  
  47.   
  48.         DataColumn col6 = dt.Columns.Add("LastActiveTime"typeof(DateTime));  
  49.         col6.AllowDBNull = true;  
  50.         col6.DefaultValue = DateTime.Now;  
  51.   
  52.   
  53.         HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);  
  54.     }  
  55.   
  56.   
  57.     /// <summary>  
  58.     /// 获取当前位置  
  59.     /// </summary>  
  60.     /// <returns></returns>  
  61.     public static string GetPlace()  
  62.     {  
  63.         string str = "首页";  
  64.         if (HttpContext.Current.Request.Url != null)  
  65.         {  
  66.             string url = HttpContext.Current.Request.Url.AbsolutePath.ToLower();  
  67.             if (url.IndexOf("default.aspx") >= 0)  
  68.             {  
  69.                 str = "首页";  
  70.             }  
  71.             else if (url.IndexOf("detail.aspx") >= 0)  
  72.             {  
  73.                 str = "查看详细";  
  74.             }  
  75.             else if (url.IndexOf("getpassword.aspx") >= 0)  
  76.             {  
  77.                 str = "取回密码";  
  78.             }  
  79.             else if (url.IndexOf("gettry.aspx") >= 0)  
  80.             {  
  81.                 str = "获取试用";  
  82.             }  
  83.             else if (url.IndexOf("gopay.aspx") >= 0)  
  84.             {  
  85.                 str = "付款";  
  86.             }  
  87.             else if (url.IndexOf("modpass.aspx") >= 0)  
  88.             {  
  89.                 str = "修改密码";  
  90.             }  
  91.             else if (url.IndexOf("modprofile.aspx") >= 0)  
  92.             {  
  93.                 str = "修改资料";  
  94.             }  
  95.             else if (url.IndexOf("myorder.aspx") >= 0)  
  96.             {  
  97.                 str = "我的订单";  
  98.             }  
  99.             else if (url.IndexOf("myreport.aspx") >= 0)  
  100.             {  
  101.                 str = "我的报告";  
  102.             }  
  103.             else if (url.IndexOf("register.aspx") >= 0)  
  104.             {  
  105.                 str = "注册";  
  106.             }  
  107.             else if (url.IndexOf("writereport.aspx") >= 0)  
  108.             {  
  109.                 str = "写报告";  
  110.             }  
  111.             else if (url.IndexOf("forumlist.aspx") >= 0)  
  112.             {  
  113.                 str = "论坛列表";  
  114.             }  
  115.             else if (url.IndexOf("forummail.aspx") >= 0)  
  116.             {  
  117.                 str = "论坛邮箱";  
  118.             }  
  119.             else if (url.IndexOf("forumsetsign.aspx") >= 0)  
  120.             {  
  121.                 str = "修改个人签名";  
  122.             }  
  123.             else if (url.IndexOf("forumtopic.aspx") >= 0)  
  124.             {  
  125.                 str = "浏览贴子";  
  126.             }  
  127.             else if (url.IndexOf("forum.aspx") >= 0)  
  128.             {  
  129.                 str = "论坛首页";  
  130.             }  
  131.             else if (url.IndexOf("list.aspx") >= 0)  
  132.             {  
  133.                 str = "查看类别";  
  134.             }  
  135.         }  
  136.         return str;  
  137.     }  
  138.   
  139.   
  140.     /// <summary>  
  141.     /// 统计在线人数  
  142.     /// </summary>  
  143.     /// <returns></returns>  
  144.     public static int OnlineCount()  
  145.     {  
  146.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  147.             BuildCacheOnlineTable();  
  148.   
  149.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  150.   
  151.         return dt.Rows.Count;  
  152.     }  
  153.   
  154.     /// <summary>  
  155.     /// 获取在线用户列表  
  156.     /// </summary>  
  157.     /// <returns></returns>  
  158.     public static DataTable GetOnlineUserTable()  
  159.     {  
  160.         ClearExptionUser();  
  161.         return (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  162.   
  163.     }  
  164.   
  165.     /// <summary>  
  166.     /// 检查IP是否在Cache中  
  167.     /// </summary>  
  168.     /// <param name="ip"></param>  
  169.     /// <returns></returns>  
  170.     public static bool CheckOnlineIP(string ip)  
  171.     {  
  172.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  173.             BuildCacheOnlineTable();  
  174.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  175.         DataRow[] drs = dt.Select("ip='" + ip + "'");  
  176.         bool result = false;  
  177.         if (drs.Length > 0)  
  178.             result = true;  
  179.   
  180.         return result;  
  181.     }  
  182.   
  183.     /// <summary>  
  184.     /// 用户是否在线  
  185.     /// </summary>  
  186.     /// <param name="userid"></param>  
  187.     /// <returns></returns>  
  188.     public static bool IsOnline(string userid)  
  189.     {  
  190.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  191.             BuildCacheOnlineTable();  
  192.   
  193.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  194.         DataRow[] drs = dt.Select("userid='" + userid + "'");  
  195.         bool result = false;  
  196.         if (drs.Length > 0)  
  197.             result = true;  
  198.   
  199.         return result;  
  200.     }  
  201.   
  202.     /// <summary>  
  203.     /// 删除过时用户  
  204.     /// </summary>  
  205.     public static void ClearExptionUser()  
  206.     {  
  207.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  208.             BuildCacheOnlineTable();  
  209.   
  210.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  211.         for (int i = 0; i < dt.Rows.Count; i++)  
  212.         {  
  213.             DateTime lastactive = Convert.ToDateTime(dt.Rows[i]["LastActiveTime"]);  
  214.             int diff = Convert.ToInt32(Unit.DateDiff("minute", lastactive, DateTime.Now));  
  215.             if (diff > 20)  
  216.             {  
  217.                 dt.Rows[i].Delete();  
  218.             }  
  219.         }  
  220.         HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);  
  221.     }  
  222.   
  223.     /// <summary>  
  224.     /// 更新用户活动时间  
  225.     /// </summary>  
  226.     /// <param name="userid"></param>  
  227.     public static void UpdateOnline(string userid, string place)  
  228.     {  
  229.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  230.             BuildCacheOnlineTable();  
  231.   
  232.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  233.         DataRow[] drs = dt.Select("userid='" + userid + "'");  
  234.         for (int i = 0; i < drs.Length; i++)  
  235.         {  
  236.             drs[i]["LastActiveTime"] = DateTime.Now;  
  237.             drs[i]["UserPlace"] = place;  
  238.         }  
  239.         HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);  
  240.     }  
  241.   
  242.     public static void UpdateOnlineByIP(string userid, string username, string userplace, string ip)  
  243.     {  
  244.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  245.             BuildCacheOnlineTable();  
  246.   
  247.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  248.         DataRow[] drs = dt.Select("ip='" + ip + "'");  
  249.         for (int i = 0; i < drs.Length; i++)  
  250.         {  
  251.             drs[i]["LastActiveTime"] = DateTime.Now;  
  252.             drs[i]["UserID"] = userid;  
  253.             drs[i]["UserName"] = username;  
  254.             drs[i]["UserPlace"] = userplace;  
  255.         }  
  256.         HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);  
  257.     }  
  258.   
  259.     public static void DeleteOnlineUser(string userid)  
  260.     {  
  261.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  262.             BuildCacheOnlineTable();  
  263.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  264.         DataRow[] drs = dt.Select("userid='" + userid + "'");  
  265.         for (int i = 0; i < drs.Length; i++)  
  266.         {  
  267.             dt.Rows.Remove(drs[i]);  
  268.         }  
  269.         HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);  
  270.     }  
  271.   
  272.   
  273.     public static void InsertOnlineUser(string userid, string username, string UserPlace, string IP)  
  274.     {  
  275.         if (HttpContext.Current.Cache["UserOnlineTable"] == null)  
  276.             BuildCacheOnlineTable();  
  277.   
  278.         DataTable dt = (DataTable)HttpContext.Current.Cache["UserOnlineTable"];  
  279.         DataRow workRow;  
  280.         workRow = dt.NewRow();  
  281.         workRow["UserID"] = userid;  
  282.         workRow["UserName"] = username;  
  283.         workRow["UserPlace"] = UserPlace;  
  284.         workRow["IP"] = IP;  
  285.         workRow["LastActiveTime"] = DateTime.Now;  
  286.         dt.Rows.Add(workRow);  
  287.         HttpContext.Current.Cache.Add("UserOnlineTable", dt, null, DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal, null);  
  288.         ClearExptionUser();  
  289.     }  
  290.   
  291.     #endregion  


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar