What is the best way to implement storage for a hit counter in ASP.NET?

asp.net

Solution

which is the best way to save this information

There are three ways that can help to not affect performance of showing the page, and care only about what data you goin to save and keep your way.

First way

You can use a handler that you call from the page as image, only to write this statistic counter. In this handler you do not use session and for that reason you not even block other pages. And also this is not called from most of the spiders, so you write only real users.

You place the call on the page this way:

<img src="keepstats.ashx?Page=CurrentPage.aspx" height="1" width="1" alt="" >

and the handler is

// 1x1 transparent GIF
private readonly byte[] GifData = {
    0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
    0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
    0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
    0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
    0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
    0x02, 0x44, 0x01, 0x00, 0x3b
};

public void ProcessRequest (HttpContext context) 
{
    // save here your stat counter

    // send the image
    context.Response.ContentType = "image/gif";
    context.Response.Buffer = false;
    context.Response.OutputStream.Write(GifData, 0, GifData.Length);
}

Second way

To call this function not from code behind but from inside the page at the end like that. The dis-advanced of this case is that the session is locking the page and the page must totally load to allow the next request to proceeds. But is the most easy to made.

<body>
... all the page here...


..bottom of the page..
<%
  // you send this part of the page and user all ready see it
  Response.Flush();
  // now you call the function that calculate the statistics
  // the page still show that is loading, but the user all ready see it
  CallTheStatisticSaveFunction();
%>
</body>

third way

I forget one more way, to create a thread to make the work in parallel and in-dependable from the page.

Last word

In my code I use all of the above tricks, one for statistics, and the second for actions that must be done at the page but may take some more time. I use the thread for actions that must be done before I start render the page, with some tricks that if the thread not end I render what I have.

I will avoid session for knowing if the user all ready see the page for this reason:

- If the user not use cookie the session is not work, and all spiders not use session

- If the user start see a lot of pages the session data will be grow.

The `Application_End` is called only ones, when you close iis, or stop your application. The point that is global and called every time is the `Application_BeginRequest` but there you going to affect performance because is called for every asp.net element and you need to check if its a page, if something else, and this at the first point of the page, you going to have delay there. We search here how to write the statistics at the end, after user see something.

Problem

I am creating a hit counter which serves to keep track of how many people accessed a particular page. The table which holds this information contains the page_id, ip_address, and timestamp (when the IP address accessed the page). The "issue" I'm having is which is the best way to save this information. If I store it every time a user accesses the page it affects the website performance as well as problems may rise in the saving process. I was hoping of maybe using the `Application_End` in the `Global.asax` file to add the records in the counter table. But how can I save such records? Do I make use of an `Application` variable? If yes, how?

Original source