ASP.NET中實現簡單訪問統計計數器

來源:互聯網
上載者:User

為完成網站訪問計數器可是傷透腦筋,今天完成了第一個版本,記錄資訊如下:
1、記錄每天的訪問使用者數
2、記錄每天的點擊次數

就為實現這兩個簡易功能,也讓我著實頭痛,暫時沒有太多考慮效能,只是實現。
簡單說明一下:
由於為了精確實現計數器,那麼要麼簡單使用Application.Lock鎖定線程,要麼自己寫多線程的程式,他們都會造成系統效能的嚴重影響,因此,在鎖定線程期間做的事情越少越好,因此不能頻繁的往資料庫寫使用者訪問的計數統計資訊,我使用了間隔一段時間寫一次的方法,可以減少往資料庫記錄資料的次數,下面的代碼在Application_EndRequest中檢查使用者上次記錄資料的時間到現在是否超過了設定的時間間隔,如果是,那麼再次記錄,這樣就極大的減少了寫資料庫的次數,對效能的影響和資料庫伺服器的影響也小一些。
如果伺服器停止,那麼最後一次時間間隔內的使用者訪問可能不會被記錄,因此在Application_End事件中進行記錄。

往資料庫中傳遞的參數為:當前日期(yyyy-MM-dd格式),計數類別:使用者數/頁面點擊數,計數。在預存程序中首先以日期參數為主鍵更新資料,如果更新失敗(當天還沒有記錄過),那麼插入新資料。資料庫中該表有三個欄位:TodayDate(Date),TodayVisitCNT(number),TodayClickCNT(number)。TodayDate為主鍵。

Option Explicit On
Option Strict On

Imports System
Imports System.Web
Imports System.Web.SessionState
Imports System.Runtime.Remoting

Imports System.IO
Imports Microsoft.VisualBasic

Namespace MyWeb
    Public Class Global
        Inherits System.Web.HttpApplication

        ' 儲存訪問計數
        Private Shared _visitCount As New Hashtable
        ' 記錄最後一次更新計數資訊的時間
        Private Shared _lastUpdate As DateTime

#Region " 組件設計器產生的程式碼 "

        Public Sub New()
            MyBase.New()

            '該調用是組件設計器所必需的。
            InitializeComponent()

            '在 InitializeComponent() 調用之後添加任何初始化

        End Sub

        '組件設計器所必需的
        Private components As System.ComponentModel.IContainer

        '注意:以下過程是組件設計器所必需的
        '可以使用組件設計器修改此過程。
        '不要使用代碼編輯器修改它。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            components = New System.ComponentModel.Container
        End Sub

#End Region

#Region "系統事件"

        Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
           ' 系統啟動時間定為最後一次更新時間
            _lastUpdate = DateTime.Now
        End Sub

        Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' 當前日期訪問使用者數加1
            Application.Lock()
            Dim Key As String = Now.ToString("yyyy-MM-dd") & "_USERCOUNT"
            _visitCount(Key) = CInt(_visitCount(Key)) + 1
            Application.UnLock()
      End Sub

        Sub Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs)
            ' 使用者日期頁面請求次數增加1,到一定訪問量以後記錄到資料庫
            Application.Lock()

            Dim Key As String = Now.ToString("yyyy-MM-dd") & "_PAGECOUNT"
            Dim count As Integer

            count = CInt(_visitCount(Key))
            count += 1
            _visitCount(Key) = count

            ' 如果上次記錄時間超過了指定的時間間隔,那麼再次記錄計數器
            If DateTime.Now.Subtract(_lastUpdate).TotalSeconds > 60 Then
                ' 1、往資料庫記錄所有訪問資訊
                Dim item As String
                Dim mDate As Date
                Dim mType As String
                Dim mCount As Integer

                For Each item In _visitCount.Keys
                    mCount = CInt(_visitCount(item))
                    mDate = CDate(Split(item, "_")(0))
                    mType = CStr(Split(item, "_")(1))
                  ' 往資料庫寫資料
                    With New BusinessFacade.PagesSystem
                        .VisitCountLog(mDate, mType, mCount)
                    End With
                Next

                ' 2、將計數器歸零
                _visitCount.Clear()
                ' 將記錄時間更新
                _lastUpdate = DateTime.Now
            End If

            Application.UnLock()

        End Sub

        Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
            ' 在應用程式結束時激發

            ' 1、往資料庫記錄所有訪問資訊
            Dim item As String
            Dim mDate As Date
            Dim mType As String
            Dim mCount As Integer

            For Each item In _visitCount.Keys
                mCount = CInt(_visitCount(item))
                mDate = CDate(Split(item, "_")(0))
                mType = CStr(Split(item, "_")(1))
                With New BusinessFacade.PagesSystem
                    .VisitCountLog(mDate, mType, mCount)
                End With
            Next
            ' 2、將計數器歸零
            _visitCount.Clear()

        End Sub

#End Region

    End Class
End Namespace

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.