在负载平衡的服务器选择清除缓存(ASP.Net) [英] Selective Cache clearing across load balanced servers (ASP.Net)

查看:162
本文介绍了在负载平衡的服务器选择清除缓存(ASP.Net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有两个负载均衡的服务器上运行一个网站。我们使用ASP.Net缓存来帮助提高性能通过高速缓存使用率很高的数据。但偶尔的数据更改。如果是这样,我们需要明确双方的负载均衡的服务器相关的缓存项。有没有人有一些容易实现的如何可以做到这一点建议吗?

We have a website that runs on two load balanced servers. We use the ASP.Net caching to help improve performance by caching high usage data. BUT, occasionally that data changes. When it does, we need to clear the relevant cache items on BOTH the load balanced servers. Does anyone have some easy to implement suggestions for how this can be done?

我知道有软件都来为您进行管理(微软速度一)。我也知道,还有其他的选择有不同的状态,服务器等。但是,对于我们想要的东西他们似乎有点小题大做。只是一个简单的机制,清理出特定的缓存项跨服务器是我们所需要的了。

I know that there is software out there to manage this for you (Microsoft Velocity for one). I also know that there are other options for having separate state servers etc. However, for what we want they all seem like overkill. Just a simple mechanism for clearing out specific cache items across servers is all we need for now.

感谢您的任何建议。

推荐答案

我们用一个简单的Web服务的方法。我们缓存明确的机制,检查网络配置设置,看是否有其他服务器存在,并呼吁那些服务器(S)上异步Web服务。

We use a simple web service approach. Our cache clear mechanism checks a web config setting to see if any other servers exist and calls the web service on those server(s) asynchronously.

我们存储与特定的命名convetions数据,可以很容易地清除我们想要的。因此,我们通过在任一个preFIX或后缀为要删除的项目,因为有时可能是用户特定的(例如,用户标识被附加到物品的名称)或专用(例如,在preFIX该项目是应用程序名)。

We store data with specific naming convetions to make it easy to clear what we want. So we pass in either a prefix or postfix for the item to be removed as sometimes it might be user specific (ex. the userid is appended to the name of the item) or application specific (ex. the prefix of the item is the application name).

下面是会叫上你的节点的任一ClearItem程序的VB例如:

Here is a VB example of the ClearItem routine that would be called on either one of your nodes:

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

您可以看到,code以上使用这种辅助类调用其他的服务器(S):

You can see that the code above calls other server(s) by using this helper class:

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服务,然后调用同一个code调用该_ClearItem。

The web service on the remote server then calls the same code that the _ClearItem called.

这篇关于在负载平衡的服务器选择清除缓存(ASP.Net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆