cache 解决在线用户方案

在线用户的统计方法有很多其中一个就是SESSION保存在数据库,然后select tmpdb表,获取在线用户. 下面介绍一个非常有用的办法试用Cache, Cache是服务器的方法,跟ASP的Application差不多, 下面是一个Cache在线用户的超作, 流程:建立一个DataTable 把需要的字段添加进去, id,userid,username,place,lasttime lasttime是最后活动的时间,这个时间是决定用户是否超时登陆,然后用一个方法来删除过时的用户

using System;   

  

using System.Data;   

  

using System.Configuration;   

  

using System.Web;   

  

using System.Web.Caching;   

  

using System.Web.Security;   

  

using System.Web.UI;   

  

using System.Web.UI.WebControls;   

  

using System.Web.UI.WebControls.WebParts;   

  

using System.Web.UI.HtmlControls;   

  

  

  

  1. /// <summary>   
  2.   
  3. /// OnlineCache 的摘要说明   
  4.   
  5. /// </summary>   
  6.   
  7. public   class  OnlineCache   
  8.   
  9. {   
  10.   
  11. public  OnlineCache()   
  12.   
  13. {   
  14.   
  15.   
  16.   
  17. }  
  18.  
  19.  
  20.  
  21. #region 缓存在线用户   
  22.   
  23. /// <summary>   
  24.   
  25. /// 建立缓存表   
  26.   
  27. /// </summary>   
  28.   
  29. public   static   void  BuildCacheOnlineTable()   
  30.   
  31. {   
  32.   
  33. DataTable dt =  new  DataTable();   
  34.   
  35. DataColumn col1 = dt.Columns.Add( "ID"typeof (Int32));   
  36.   
  37. col1.AllowDBNull =  false ;   
  38.   
  39. col1.AutoIncrement =  true ;   
  40.   
  41. col1.AutoIncrementSeed = 1;   
  42.   
  43. col1.AutoIncrementStep = 1;   
  44.   
  45. col1.Unique =  true ;   
  46.   
  47.   
  48.   
  49. DataColumn col2 = dt.Columns.Add( "UserID"typeof (String));   
  50.   
  51. col2.AllowDBNull =  true ;   
  52.   
  53.   
  54.   
  55. DataColumn col3 = dt.Columns.Add( "UserName"typeof (String));   
  56.   
  57. col3.AllowDBNull =  true ;   
  58.   
  59.   
  60.   
  61. DataColumn col4 = dt.Columns.Add( "UserPlace"typeof (String));   
  62.   
  63. col4.AllowDBNull =  true ;   
  64.   
  65.   
  66.   
  67. DataColumn col5 = dt.Columns.Add( "IP"typeof (String));   
  68.   
  69. col5.AllowDBNull =  true ;   
  70.   
  71.   
  72.   
  73. DataColumn col6 = dt.Columns.Add( "LastActiveTime"typeof (DateTime));   
  74.   
  75. col6.AllowDBNull =  true ;   
  76.   
  77. col6.DefaultValue = DateTime.Now;   
  78.   
  79.   
  80.   
  81.   
  82.   
  83. HttpContext.Current.Cache.Add( "UserOnlineTable" , dt,  null , DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal,  null );   
  84.   
  85. }   
  86.   
  87.   
  88.   
  89.   
  90.   
  91. /// <summary>   
  92.   
  93. /// 获取当前位置   
  94.   
  95. /// </summary>   
  96.   
  97. /// <returns></returns>   
  98.   
  99. public   static   string  GetPlace()   
  100.   
  101. {   
  102.   
  103. string  str =  "首页" ;   
  104.   
  105. if  (HttpContext.Current.Request.Url !=  null )   
  106.   
  107. {   
  108.   
  109. string  url = HttpContext.Current.Request.Url.AbsolutePath.ToLower();   
  110.   
  111. if  (url.IndexOf( "default.aspx" ) >= 0)   
  112.   
  113. {   
  114.   
  115. str =  "首页" ;   
  116.   
  117. }   
  118.   
  119. else   if  (url.IndexOf( "detail.aspx" ) >= 0)   
  120.   
  121. {   
  122.   
  123. str =  "查看详细" ;   
  124.   
  125. }   
  126.   
  127. else   if  (url.IndexOf( "getpassword.aspx" ) >= 0)   
  128.   
  129. {   
  130.   
  131. str =  "取回密码" ;   
  132.   
  133. }   
  134.   
  135. else   if  (url.IndexOf( "gettry.aspx" ) >= 0)   
  136.   
  137. {   
  138.   
  139. str =  "获取试用" ;   
  140.   
  141. }   
  142.   
  143. else   if  (url.IndexOf( "gopay.aspx" ) >= 0)   
  144.   
  145. {   
  146.   
  147. str =  "付款" ;   
  148.   
  149. }   
  150.   
  151. else   if  (url.IndexOf( "modpass.aspx" ) >= 0)   
  152.   
  153. {   
  154.   
  155. str =  "修改密码" ;   
  156.   
  157. }   
  158.   
  159. else   if  (url.IndexOf( "modprofile.aspx" ) >= 0)   
  160.   
  161. {   
  162.   
  163. str =  "修改资料" ;   
  164.   
  165. }   
  166.   
  167. else   if  (url.IndexOf( "myorder.aspx" ) >= 0)   
  168.   
  169. {   
  170.   
  171. str =  "我的订单" ;   
  172.   
  173. }   
  174.   
  175. else   if  (url.IndexOf( "myreport.aspx" ) >= 0)   
  176.   
  177. {   
  178.   
  179. str =  "我的报告" ;   
  180.   
  181. }   
  182.   
  183. else   if  (url.IndexOf( "register.aspx" ) >= 0)   
  184.   
  185. {   
  186.   
  187. str =  "注册" ;   
  188.   
  189. }   
  190.   
  191. else   if  (url.IndexOf( "writereport.aspx" ) >= 0)   
  192.   
  193. {   
  194.   
  195. str =  "写报告" ;   
  196.   
  197. }   
  198.   
  199. else   if  (url.IndexOf( "forumlist.aspx" ) >= 0)   
  200.   
  201. {   
  202.   
  203. str =  "论坛列表" ;   
  204.   
  205. }   
  206.   
  207. else   if  (url.IndexOf( "forummail.aspx" ) >= 0)   
  208.   
  209. {   
  210.   
  211. str =  "论坛邮箱" ;   
  212.   
  213. }   
  214.   
  215. else   if  (url.IndexOf( "forumsetsign.aspx" ) >= 0)   
  216.   
  217. {   
  218.   
  219. str =  "修改个人签名" ;   
  220.   
  221. }   
  222.   
  223. else   if  (url.IndexOf( "forumtopic.aspx" ) >= 0)   
  224.   
  225. {   
  226.   
  227. str =  "浏览贴子" ;   
  228.   
  229. }   
  230.   
  231. else   if  (url.IndexOf( "forum.aspx" ) >= 0)   
  232.   
  233. {   
  234.   
  235. str =  "论坛首页" ;   
  236.   
  237. }   
  238.   
  239. else   if  (url.IndexOf( "list.aspx" ) >= 0)   
  240.   
  241. {   
  242.   
  243. str =  "查看类别" ;   
  244.   
  245. }   
  246.   
  247. }   
  248.   
  249. return  str;   
  250.   
  251. }   
  252.   
  253.   
  254.   
  255.   
  256.   
  257. /// <summary>   
  258.   
  259. /// 统计在线人数   
  260.   
  261. /// </summary>   
  262.   
  263. /// <returns></returns>   
  264.   
  265. public   static   int  OnlineCount()   
  266.   
  267. {   
  268.   
  269. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  270.   
  271. BuildCacheOnlineTable();   
  272.   
  273.   
  274.   
  275. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  276.   
  277.   
  278.   
  279. return  dt.Rows.Count;   
  280.   
  281. }   
  282.   
  283.   
  284.   
  285. /// <summary>   
  286.   
  287. /// 获取在线用户列表   
  288.   
  289. /// </summary>   
  290.   
  291. /// <returns></returns>   
  292.   
  293. public   static  DataTable GetOnlineUserTable()   
  294.   
  295. {   
  296.   
  297. ClearExptionUser();   
  298.   
  299. return  (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  300.   
  301.   
  302.   
  303. }   
  304.   
  305.   
  306.   
  307. /// <summary>   
  308.   
  309. /// 检查IP是否在Cache中   
  310.   
  311. /// </summary>   
  312.   
  313. /// <param name="ip"></param>   
  314.   
  315. /// <returns></returns>   
  316.   
  317. public   static   bool  CheckOnlineIP( string  ip)   
  318.   
  319. {   
  320.   
  321. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  322.   
  323. BuildCacheOnlineTable();   
  324.   
  325. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  326.   
  327. DataRow[] drs = dt.Select( "ip='"  + ip +  "'" );   
  328.   
  329. bool  result =  false ;   
  330.   
  331. if  (drs.Length > 0)   
  332.   
  333. result =  true ;   
  334.   
  335.   
  336.   
  337. return  result;   
  338.   
  339. }   
  340.   
  341.   
  342.   
  343. /// <summary>   
  344.   
  345. /// 用户是否在线   
  346.   
  347. /// </summary>   
  348.   
  349. /// <param name="userid"></param>   
  350.   
  351. /// <returns></returns>   
  352.   
  353. public   static   bool  IsOnline( string  userid)   
  354.   
  355. {   
  356.   
  357. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  358.   
  359. BuildCacheOnlineTable();   
  360.   
  361.   
  362.   
  363. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  364.   
  365. DataRow[] drs = dt.Select( "userid='"  + userid +  "'" );   
  366.   
  367. bool  result =  false ;   
  368.   
  369. if  (drs.Length > 0)   
  370.   
  371. result =  true ;   
  372.   
  373.   
  374.   
  375. return  result;   
  376.   
  377. }   
  378.   
  379.   
  380.   
  381. /// <summary>   
  382.   
  383. /// 删除过时用户   
  384.   
  385. /// </summary>   
  386.   
  387. public   static   void  ClearExptionUser()   
  388.   
  389. {   
  390.   
  391. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  392.   
  393. BuildCacheOnlineTable();   
  394.   
  395.   
  396.   
  397. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  398.   
  399. for  ( int  i = 0; i < dt.Rows.Count; i++)   
  400.   
  401. {   
  402.   
  403. DateTime lastactive = Convert.ToDateTime(dt.Rows[i][ "LastActiveTime" ]);   
  404.   
  405. int  diff = Convert.ToInt32(Unit.DateDiff( "minute" , lastactive, DateTime.Now));   
  406.   
  407. if  (diff > 20)   
  408.   
  409. {   
  410.   
  411. dt.Rows[i].Delete();   
  412.   
  413. }   
  414.   
  415. }   
  416.   
  417. HttpContext.Current.Cache.Add( "UserOnlineTable" , dt,  null , DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal,  null );   
  418.   
  419. }   
  420.   
  421.   
  422.   
  423. /// <summary>   
  424.   
  425. /// 更新用户活动时间   
  426.   
  427. /// </summary>   
  428.   
  429. /// <param name="userid"></param>   
  430.   
  431. public   static   void  UpdateOnline( string  userid,  string  place)   
  432.   
  433. {   
  434.   
  435. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  436.   
  437. BuildCacheOnlineTable();   
  438.   
  439.   
  440.   
  441. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  442.   
  443. DataRow[] drs = dt.Select( "userid='"  + userid +  "'" );   
  444.   
  445. for  ( int  i = 0; i < drs.Length; i++)   
  446.   
  447. {   
  448.   
  449. drs[i][ "LastActiveTime" ] = DateTime.Now;   
  450.   
  451. drs[i][ "UserPlace" ] = place;   
  452.   
  453. }   
  454.   
  455. HttpContext.Current.Cache.Add( "UserOnlineTable" , dt,  null , DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal,  null );   
  456.   
  457. }   
  458.   
  459.   
  460.   
  461. public   static   void  UpdateOnlineByIP( string  userid,  string  username,  string  userplace,  string  ip)   
  462.   
  463. {   
  464.   
  465. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  466.   
  467. BuildCacheOnlineTable();   
  468.   
  469.   
  470.   
  471. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  472.   
  473. DataRow[] drs = dt.Select( "ip='"  + ip +  "'" );   
  474.   
  475. for  ( int  i = 0; i < drs.Length; i++)   
  476.   
  477. {   
  478.   
  479. drs[i][ "LastActiveTime" ] = DateTime.Now;   
  480.   
  481. drs[i][ "UserID" ] = userid;   
  482.   
  483. drs[i][ "UserName" ] = username;   
  484.   
  485. drs[i][ "UserPlace" ] = userplace;   
  486.   
  487. }   
  488.   
  489. HttpContext.Current.Cache.Add( "UserOnlineTable" , dt,  null , DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal,  null );   
  490.   
  491. }   
  492.   
  493.   
  494.   
  495. public   static   void  DeleteOnlineUser( string  userid)   
  496.   
  497. {   
  498.   
  499. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  500.   
  501. BuildCacheOnlineTable();   
  502.   
  503. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  504.   
  505. DataRow[] drs = dt.Select( "userid='"  + userid +  "'" );   
  506.   
  507. for  ( int  i = 0; i < drs.Length; i++)   
  508.   
  509. {   
  510.   
  511. dt.Rows.Remove(drs[i]);   
  512.   
  513. }   
  514.   
  515. HttpContext.Current.Cache.Add( "UserOnlineTable" , dt,  null , DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal,  null );   
  516.   
  517. }   
  518.   
  519.   
  520.   
  521.   
  522.   
  523. public   static   void  InsertOnlineUser( string  userid,  string  username,  string  UserPlace,  string  IP)   
  524.   
  525. {   
  526.   
  527. if  (HttpContext.Current.Cache[ "UserOnlineTable" ] ==  null )   
  528.   
  529. BuildCacheOnlineTable();   
  530.   
  531.   
  532.   
  533. DataTable dt = (DataTable)HttpContext.Current.Cache[ "UserOnlineTable" ];   
  534.   
  535. DataRow workRow;   
  536.   
  537. workRow = dt.NewRow();   
  538.   
  539. workRow[ "UserID" ] = userid;   
  540.   
  541. workRow[ "UserName" ] = username;   
  542.   
  543. workRow[ "UserPlace" ] = UserPlace;   
  544.   
  545. workRow[ "IP" ] = IP;   
  546.   
  547. workRow[ "LastActiveTime" ] = DateTime.Now;   
  548.   
  549. dt.Rows.Add(workRow);   
  550.   
  551. HttpContext.Current.Cache.Add( "UserOnlineTable" , dt,  null , DateTime.Now.AddYears(10), TimeSpan.Zero, CacheItemPriority.Normal,  null );   
  552.   
  553. ClearExptionUser();   
  554.   
  555. }  
  556.  
  557.  
  558.  
  559. #endregion   
  560.   
  561. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值