2009-04-21 8 views
8

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

私はあなたのためにこれを管理するソフトウェアがあることを知っています(Microsoft Velocity for one)。私はまた別の州のサーバーなどを持っているための他のオプションがあることを知っている。しかし、私たちが望むもののために、彼らはすべて過度のように思える。サーバー間で特定のキャッシュ項目をクリアするための単純なメカニズムは、今私たちが必要としているものです。

ありがとうございます。

+0

私のソリューションはあなたのためにうれしいです。あなたが悩んでいる場合は私に教えてください! – wweicker

答えて

1

。私たちのキャッシュクリアメカニズムは、Webコンフィグレーションの設定をチェックして他のサーバが存在するかどうかを調べ、それらのサーバ上でWebサービスを非同期に呼び出します。

私たちは、私たちが望むものを簡単にクリアするために、特定の命名法を使ってデータを保存します。したがって、削除するアイテムのプレフィックスまたはポストフィックスのいずれかを、ユーザー固有のもの(たとえば、アイテムの名前にuseridが追加されているもの)またはアプリケーション固有のもの(たとえば、アイテムのプレフィックスがアプリケーション名)。ここで

はあなたのノードのいずれか一方に呼び出されます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 

あなたは上記のコードは、このヘルパークラスを使用して他のサーバ(複数可)を呼び出すことがわかります。

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

5

オブジェクトにキャッシュ依存性を定義しないと、両方のサーバーで確認できます。 SQLまたはFileキャッシュの依存関係を使用できます。私たちは、単純なWebサービスのアプローチを使用し

Link to Caching Msdn Page

関連する問題