是否可以通过编程方式一次性在两台服务器中回收应用程序?

时间:2015-04-27 06:55:12

标签: asp.net vb.net caching

我已使用此语法来回收我的应用程序。

    HttpRuntime.UnloadAppDomain(); 

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

所以,我的问题是,有没有办法通过这种语法实际回收其他服务器? 它是一个webfarm,因此,只有当前服务器的缓存才会被上面的语法清除。

之前我曾问过类似的问题: Is there any way to clear cache from server farm?

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

请告知..

1 个答案:

答案 0 :(得分:1)

您不必使用第三方提供商来实现您自己的OutputCacheProvider,我在上一个问题Is there any way to clear cache from server farm?的答案中提供的链接只是建议分布式缓存,因为您在询问关于为您的Web场提供一个缓存。如果您足够高兴拥有每个服务器缓存并且只想使条目无效,您仍然可以实现自己的缓存提供程序,并且只需要在Web场中的所有服务器上使缓存无效。

考虑这样的事情:

Public Class MyOutputCacheProvider
Inherits OutputCacheProvider

Private Shared ReadOnly _cache As ObjectCache = MemoryCache.Default
Private ReadOnly _cacheDependencyFile As String = "\\networklocation\myfile.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)

我现在还没有对这段代码进行过测试,但我之前已经实现了类似的功能,你可能会看到我的内容正确吗?

相关问题