以编程方式一次回收两台服务器中的应用程序的任意方法



我用这种语法来回收我的应用程序。

    HttpRuntime.UnloadAppDomain(); 

您可能会问为什么我需要回收我的应用程序,它是为了清除缓存,并且此语法被编程为仅在应用程序中执行的更新期间工作,这非常罕见。

所以,我的问题是,有没有办法通过这种语法实际回收其他服务器?它是一个 webfyard,因此,只有来自当前服务器的缓存才能使用上述语法清除。

我之前问过一个类似的问题:有没有办法从服务器场中清除缓存?

我无法应用上一个问题中给出的建议的原因是因为我的组织不同意实施第三方工具。

请指教..

您不必使用第三方提供商来实现自己的OutputCacheProvider ,我在回答您之前的问题时提供的链接 有没有办法从服务器场中清除缓存? 只是建议分布式缓存,因为您询问是否为您的 Web 场提供一个缓存。如果您足够高兴拥有每服务器缓存,并且只想使条目无效,您仍然可以实现自己的缓存提供程序,并且只需通过某种方法使 Web 场中所有服务器上的缓存失效。

考虑这样的事情:

Public Class MyOutputCacheProvider
Inherits OutputCacheProvider
Private Shared ReadOnly _cache As ObjectCache = MemoryCache.Default
Private ReadOnly _cacheDependencyFile As String = "\networklocationmyfile.txt"
Private Shared _lastUpdated As DateTime
Public Sub New()
    'Get value for LastWriteTime
    _lastUpdated = File.GetLastWriteTime(_cacheDependencyFile)
End Sub
Public Overrides Function [Get](key As String) As Object
    'If file has been updated try to remove the item from cache and return null
    If _lastUpdated <> File.GetLastWriteTime(_cacheDependencyFile) Then
        Remove(key)
        Return Nothing
    End If
    'return item from cache
    Return _cache.Get(key)
End Function

Public Overrides Function Add(key As String, entry As Object, utcExpiry As DateTime) As Object
    'If the item is already in cache no need to add it
    If _cache.Contains(key) Then
        Return entry
    End If
    'add item to cache
    _cache.Add(New CacheItem(key, entry), New CacheItemPolicy() With {.AbsoluteExpiration = utcExpiry})
    Return entry
End Function

Public Overrides Sub [Set](key As String, entry As Object, utcExpiry As DateTime)
    'If key does not exist in the cache, value and key are used to insert as a new cache entry. 
    'If an item with a key that matches item exists, the cache entry is updated or overwritten by using value
    _cache.Set(key, entry, utcExpiry)
End Sub
Public Overrides Sub Remove(key As String)
    'if item exists in cache - remove it
    If _cache.Contains(key) Then
        _cache.Remove(key)
    End If
End Sub End Class

因此,基本上您将使用网络共享上的文件或其他东西来使缓存无效。当您想强制应用程序使缓存失效时,您只需以某种方式更新文件:

        'Take an action that will affect the write time.
    File.SetLastWriteTime(_cacheDependencyFile, Now)

我现在还没有测试过这段代码,但我以前已经实现了类似的东西,你可能会看到我得到了什么?

最新更新