Current location - Music Encyclopedia - Chinese History - How does ASP.NET count yesterday's click-through rate, today's click-through rate and historical total click-through rate?
How does ASP.NET count yesterday's click-through rate, today's click-through rate and historical total click-through rate?
Statistical click rate: including the total click rate of the website, the total click rate of the day and the click rate of each page.

Detailed logs of ASP.NET websites are usually provided by IIS, and the directory of logs can be configured in IIS Information Service Manager. However, these log files exist in the form of txt files. If you want to use them in ASP.NET, you need to write special analysis and statistical code. However, this will increase the complexity and the efficiency will not meet the requirements, because it takes a lot of time to analyze the flat file. Sometimes we don't need its detailed information, we just need to count the total number of hits per day or the number of hits per page. If each page needs to wait for a long time, this is obviously unacceptable.

Therefore, we consider writing the code of counting the total hits of the day in the masterPage, and then writing it into the database, so that the hits of each webpage can be counted; Write a code that counts the click rate of a single page in each web page, and then write it into the database. This is certainly possible, but it will affect the performance, because every time you click on a web page, you have to read the current click rate from the database, add 1 and write it back. If there is concurrency, the database will lock the data and prevent the next access until the last command ends. This is to prevent dust. If there is a lot of concurrency, it may affect performance.

Therefore, the new idea is that the statistical code in the master page and a single web page does not read the database in advance, but writes the information into the HttpAppplicationState, that is, the value in the HttpAppplicationState increases by 1 every click. Because HttpAppplicationState is stored in memory, it is much faster to operate it than to directly operate the database. Then start the thread pool timer (system. Threading.Timer) in the Application_Start function in Global.asax, and let the timer's processing function check the data in HttpAppplicationState every 5 seconds. If there is data, first read the data in the database and add data in the HttpAppplicationState, then write it back to the database, and empty the HttpAppplicationState after writing the data into the database. The advantage of this is that the handler processes the data in a new thread and does not occupy ASP's time. NET web page; Eliminate concurrent access.

Note that a handler should be added to the Application_End of Global.asax to ensure that the data in HttpApplicationState can be successfully written into the database after the application is closed and the time interval has not yet arrived, without losing the statistics of click-through rate.

In view of the total click-through rate of the day and the total click-through rate of each page, we can combine the function reconstruction of operating HttpApplicationState into one function and put it in a helper class. The function code is as follows:

public void AddValueToApplication(http application state application,string dictionaryName,string itemName)

{

If (string. IsNullOrEmpty(itemName)) returns;

Apply. lock();

Dictionary & ltstring, int & gtlist = new dictionary & ltstring, int> ();

if (application[dictionaryName]! = empty)

{

List = (dictionary & ltstring, int>) application [dictionary name];

If (list. ContainsKey (project name)

list[itemName]++;

other

List. Add(itemName, 1);

}

other

{

List. Add(itemName, 1);

}

application[dictionary name]= list;

Apply. UnLock();

}

The above function saves the click rate through Dictionary, and the parameter dictionaryName indicates the name of the item to be counted, such as "Total Click Rate of the Day" and "Single Page Click Rate"; The parameter itemName indicates the specific project name of the project to be counted. The possible value of itemname is "2010-10-19" for the click rate of the day, and "xxx.aspx" for the click rate of a single page. These pages are usually generated dynamically through ASP.NET routing.

In masterPage and every page, you only need to call this function to realize the operation of HttpapplicationState.

2. Count the number of online users: including online login users and anonymous users.

Judging the number of online people by counting the number of session ids. Some of these users are logged-in users and some are anonymous users. Judge the logged-in user and anonymous user through Page.User.IsAuthenticate

Counting the number of people online is more complicated than counting the click-through rate, but they are counted in the same way, that is, first write the data into HttpapplicationState, then register the timer handler in Global.asax, and then write the data in HttpApplicationState into the database in the timer handler.