Original: Building a back-end management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (40)-Accurate online headcount statistics-"filter +cache"
Series Catalogue
The last discussion did not have any results, I browsed a lot of articles and individual system reference! Decide to do it with the cache, which may be a bit hard to accept but it works very well with the MVC filter!
Because of the previous filter, we used the OnActionExecuting method to determine the permissions.
Now, after the method is executed, we use onactionexecuted to monitor the user's actions and refresh the user's online list.
First download the http://files.cnblogs.com/ymnets/OnlineUser.7z class library, the code is clear, and annotated
This class library includes the operation of the online user list of additions and deletions, you can download the next look and put in
Can open research its code!
New Class Onlinehttpmodule in App.admin
usingApp.Core.OnlineStat;usingApp.Models.Sys;usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Net.Http;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingsystem.web;usingSystem.Web.Routing;namespaceapp.admin{ Public classOnlinehttpmodule {//Cache Key Public Static ReadOnly stringG_onlineuserrecordercachekey ="__onlineuserrecorder"; #regionIHttpHandler Members Public Static voidProcessRequest () {//Get online User loggerOnlineuserrecorder recorder = Httpcontext.current.cache[g_onlineuserrecordercachekey] asOnlineuserrecorder; if(Recorder = =NULL) { //Create a logger factoryOnlineuserrecorderfactory factory =Newonlineuserrecorderfactory (); //Set user timeout timeFactory. Usertimeoutminute =2; //Statistical time intervalFactory. Statisticeventinterval = -; //Creating LoggersRecorder =Factory. Create (); //Cache RecorderHttpContext.Current.Cache.Insert (G_onlineuserrecordercachekey, Recorder); } onlineuser User=NewOnlineuser (); Accountmodel Model= (Accountmodel) httpcontext.current.session[" Account"];//Note that the session name is the same as the name of the login saved//User nameUser. UserName =convert.tostring (model. ID); //SessionIDUser. SessionID =HttpContext.Current.Session.SessionID; //IP AddressUser. ClientIP =HttpContext.Current.Request.UserHostAddress; //Last activity TimeUser. Activetime =DateTime.Now; //Last Request AddressUser. Requesturl =HttpContext.Current.Request.RawUrl; //Save user InformationRecorder. Persist (user); } #endregion }}
This class is called when the user logs on and is called in the filter, and the calling code
Filter:
public class Supportfilterattribute:actionfilterattribute { public string actionname {get ; set private string area; // update the online user list after the method is executed public override void onactionexecuted (ActionExecutedContext filtercontext) {onlinehttpmodule.processrequest (); }......................................
Settings at login time:
New Accountmodel (); = user. Id; = user. Truename; string. IsNullOrEmpty (user. Photo)? " /images/photo.jpg " : User. Photo; session["account") = account ; // Online User Statistics Onlinehttpmodule.processrequest ();
The call is very simple and the implementation is very simple!
Now look if you get the online list:
// bind online user list ilist<onlineuser> userlist = Recorder. Getuserlist (); foreach (var in userlist) { sb. AppendFormat (onlineuser.username+"<br>"); }
Onlinehttpmodule can freely set the statistics interval (in seconds), and the user times out time, which is very accurate statistics of the user 2 minutes without action is considered offline!
I used IE and chome to test 2 users, but also close the browser test users, accuracy is good! Abandoned an inaccurate primitive old method.
Build a backend management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (40)-Accurate online Demographics-"Filter +cache"