2009-04-21 11 views
8

हमारे पास एक वेबसाइट है जो दो लोड संतुलित सर्वरों पर चलती है। हम उच्च उपयोग डेटा कैशिंग द्वारा प्रदर्शन को बेहतर बनाने में सहायता के लिए एएसपी.Net कैशिंग का उपयोग करते हैं। लेकिन, कभी-कभी डेटा बदलता है। जब ऐसा होता है, तो हमें लोड संतुलित सर्वर दोनों पर प्रासंगिक कैश आइटम साफ़ करने की आवश्यकता होती है। क्या किसी के पास सुझावों को लागू करने के लिए कुछ आसान है कि यह कैसे किया जा सकता है?लोड संतुलित सर्वरों (एएसपी.Net) में चुनिंदा कैश समाशोधन

मुझे पता है कि आपके लिए यह प्रबंधित करने के लिए सॉफ़्टवेयर है (माइक्रोसॉफ्ट वेग एक के लिए)। मुझे यह भी पता है कि अलग-अलग राज्य सर्वर आदि के लिए अन्य विकल्प हैं। हालांकि, हम जो चाहते हैं उसके लिए वे सभी ओवरकिल की तरह लगते हैं। सर्वर्स में विशिष्ट कैश आइटम साफ़ करने के लिए बस एक सरल तंत्र है जिसे हमें अभी चाहिए।

किसी भी सुझाव के लिए धन्यवाद।

+0

मुझे खुशी है कि मेरा समाधान आपके लिए काम करेगा। यदि आप किसी भी स्नैग में भागते हैं तो मुझे बताएं! – wweicker

उत्तर

1

हम एक सरल वेब सेवा दृष्टिकोण का उपयोग करें। हमारे कैश स्पष्ट तंत्र यह देखने के लिए एक वेब कॉन्फ़िगरेशन सेटिंग जांचता है कि कोई अन्य सर्वर मौजूद है या नहीं, और उन सर्वरों पर वेब सेवा को असीमित रूप से कॉल करता है।

हम विशिष्ट नामकरण रूपांतरणों के साथ डेटा स्टोर करते हैं ताकि हम जो चाहते हैं उसे साफ़ करना आसान हो। इसलिए हम आइटम को निकालने के लिए या तो उपसर्ग या पोस्टफिक्स में पास करते हैं क्योंकि कभी-कभी यह उपयोगकर्ता विशिष्ट हो सकता है (उदा। उपयोगकर्ता आईडी आइटम के नाम पर जोड़ा जाता है) या एप्लिकेशन विशिष्ट (उदा। आइटम का उपसर्ग एप्लिकेशन है नाम)।

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 

आप देख सकते हैं कि कोड के ऊपर इस सहायक वर्ग का उपयोग करके अन्य सर्वर (रों) कहता है:

यहाँ ClearItem दिनचर्या है कि आपके नोड्स में से किसी एक पर कहा जा सकता है की एक वीबी उदाहरण है:

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 

दूरस्थ सर्वर पर वेब सेवा तो एक ही कोड है कि _ClearItem कहा जाता है कहते हैं।

5

किसी ऑब्जेक्ट पर कैश निर्भरता को परिभाषित क्यों नहीं करते हैं, दोनों सर्वर देख सकते हैं? आप SQL या File कैश निर्भरता का उपयोग कर सकते हैं।

Link to Caching Msdn Page

संबंधित मुद्दे