8

負荷分散された 2 つのサーバーで実行される Web サイトがあります。ASP.Net キャッシュを使用して、使用率の高いデータをキャッシュすることでパフォーマンスを向上させます。しかし、時々そのデータが変更されます。その場合、負荷分散されたサーバーの両方で関連するキャッシュ項目をクリアする必要があります。これを行う方法について、簡単に実装できる提案を誰かが持っていますか?

これを管理するソフトウェアが世の中にあることは知っています (Microsoft Velocity)。また、別の状態サーバーなどを使用するための他のオプションがあることも知っています。今のところ、サーバー全体で特定のキャッシュ アイテムをクリアするための単純なメカニズムだけが必要です。

提案をありがとう。

4

2 に答える 2

5

両方のサーバーが参照できるオブジェクトにキャッシュ依存関係を定義しないのはなぜですか? SQLまたはファイルキャッシュの依存関係を使用できます。

キャッシュ MSDN ページへのリンク

于 2009-04-21T21:37:15.537 に答える
1

シンプルな Web サービス アプローチを使用します。当社のキャッシュ クリア メカニズムは、Web 構成設定をチェックして、他のサーバーが存在するかどうかを確認し、それらのサーバー上の Web サービスを非同期的に呼び出します。

必要なものを簡単に明確にするために、特定の命名規則を使用してデータを保存します。そのため、削除するアイテムのプレフィックスまたはポストフィックスを渡します。これは、ユーザー固有 (例: ユーザー ID がアイテムの名前に追加される) またはアプリケーション固有 (例: アイテムのプレフィックスがアプリケーションである) である場合があるためです。名前)。

いずれかのノードで呼び出される ClearItem ルーチンの VB の例を次に示します。

Public Shared Sub ClearItem(ByVal strPrefix As String, ByVal strPostfix As String)

    If WebConfig.Caching_Enabled() Then

        ' Exit if no criteria specified '
        If IsNothing(strPrefix) AndAlso IsNothing(strPostfix) Then
            Exit Sub
        End If

        ' At the very least we need a Postfix '
        If Not IsNothing(strPostfix) AndAlso Not strPostfix.Length.Equals(0) Then
            _ClearItem(strPrefix, strPostfix)
        End If

        If WebConfig.Caching_WebFarmEnabled() Then
            ' Now clear the cache across the rest of the server farm '
            _ClearItem_WebFarm(strPrefix, strPostfix)
        End If

    End If

End Sub

Private Shared Sub _ClearItem_WebFarm(ByVal strPrefix As String, ByVal strPostfix As String)

    If WebConfig.Caching_WebFarmEnabled() Then

        ' Use a web service on each server in the farm to clear the '
        ' requested item from the Cache '

        ' Determine which servers need to remove cache items '
        Dim arrServers As String()
        arrServers = Split(WebConfig.Caching_WebFarmServers(), "|")

        Dim strServer As String ' Holds which server we are currently contacting ' 

        ' Loop through all the servers and call their web services '
        For Each strServer In arrServers

            Dim WS As New WebServiceAsyncCall
            WS.StartCallBack(strServer, strPrefix, strPostfix)

        Next

    End If

End Sub

Private Shared Sub _ClearItem(ByVal strPrefix As String, ByVal strPostfix As String)

    If WebConfig.Caching_Enabled() Then

        ' Determine how we are comparing keys '
        Dim blnPrefix, blnPostfix As Boolean

        If strPrefix.Length.Equals(0) Then
            blnPrefix = False
        Else
            blnPrefix = True
        End If

        If strPostfix.Length.Equals(0) Then
            blnPostfix = False
        Else
            blnPostfix = True
        End If

        ' Reference the Cache collection '
        Dim objCache As System.Web.Caching.Cache = HttpContext.Current.Cache

        ' Exit if the cache is empty '
        If objCache.Count.Equals(0) Then
            Exit Sub
        End If

        ' Clear out the cache for all items matching the input(s) (on this local server) '
        Dim objCacheEnum As IEnumerator = objCache.GetEnumerator()
        Dim objCacheItem As Object
        Dim objCurrentKey As System.Collections.DictionaryEntry
        Dim strCurrentKey As String

        ' Enumerate through the cache '
        While objCacheEnum.MoveNext()

            objCurrentKey = CType(objCacheEnum.Current, DictionaryEntry)
            strCurrentKey = objCurrentKey.Key.ToString()

            ' How are we comparing the key? '
            If blnPrefix AndAlso Not (blnPostfix) Then ' Only by PREFIX '

                If strCurrentKey.StartsWith(strPrefix) Then
                    ' Remove it from the cache '
                    objCacheItem = objCache.Remove(strCurrentKey) ' Returns a reference to the item '
                    objCacheItem = Nothing ' Need to explicitly nuke this because the objCache.Remove() above doesn t destroy '
                End If

            ElseIf Not (blnPrefix) AndAlso blnPostfix Then ' Only by POSTFIX '

                If strCurrentKey.EndsWith(strPostfix) Then
                    ' Remove it from the cache '
                    objCacheItem = objCache.Remove(strCurrentKey) ' Returns a reference to the item '
                    objCacheItem = Nothing ' Need to explicitly nuke this because the objCache.Remove() above doesn t destroy '
                End If

            ElseIf blnPrefix AndAlso blnPostfix Then ' By both PREFIX and POSTFIX'

                If strCurrentKey.StartsWith(strPrefix) AndAlso strCurrentKey.EndsWith(strPostfix) Then
                    ' Remove it from the cache '
                    objCacheItem = objCache.Remove(strCurrentKey) ' Returns a reference to the item '
                    objCacheItem = Nothing ' Need to explicitly nuke this because the objCache.Remove() above doesn t destroy '
                End If

            Else
                ' Not comparing prefix OR postfix? Why bother continuing then! '
                Exit Sub
            End If

        End While

    End If

End Sub

上記のコードは、次のヘルパー クラスを使用して他のサーバーを呼び出していることがわかります。

Private Class WebServiceAsyncCall

    Public Sub StartCallBack(ByVal strService As String, ByVal strPrefix As String, ByVal strPostfix As String)

        ActiveWebServiceCounter += 1

        Dim clearCacheProxy As New CacheClearService.CacheClear ' This is the web service which of course will exist on the other node as well '
        clearCacheProxy.Url = strService

        AddHandler clearCacheProxy.ClearItemCompleted, AddressOf DoneCallBack

        clearCacheProxy.ClearItemAsync(strPrefix, strPostfix)

    End Sub

    Public Sub DoneCallBack(ByVal sender As Object, ByVal e As CacheClearService.ClearItemCompletedEventArgs)

        ActiveWebServiceCounter -= 1

        If e.Result.Length > 0 Then ' Something failed '
            ' Log the error '
        End If

    End Sub

End Class

リモート サーバー上の Web サービスは、_ClearItem が呼び出したのと同じコードを呼び出します。

于 2009-04-21T21:44:30.493 に答える