开发者

Cache page but count hits

I have an aspx page which counts every visit and creates a cooki开发者_JAVA百科e. But if I use OutputCache page counts only the first visitor who requested the page. How can I prevent this bug?

Page directive:

<%@ OutputCache Duration="1200" VaryByParam="mode;page;sid;tid" %>

Codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    //Load single post data

    #region Hit Counter
    //hit counter lasts during session
    if (Session["LastHit" + postId] == null)
    {
        cmmnd.CommandText = "UPDATE Posts SET Hits=Hits+1 WHERE PostID=@PostID;";
        cmmnd.ExecuteNonQuery();
        Session["LastHit" + postId] = 1;
    }
    #endregion
}


This isn't a bug, but by design. The page is not re-processed if it is in the cache and "the cached output is still valid". You can run code during this validation phase and this could help you perform the task of counting the visits and adding it to a cookie. The following might be of help:

  • Caching ASP.NET Pages
  • How to: Check the Validity of a Cached Page


What is the point of this count? Marketting information? Use a lightweight analytics counter from script or an image in the page, not mixed in with generating the page itself. The bug is that you're adding extra work to the busier requests, instead of giving the extra work its own request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜