以编程方式一次回收两台服务器中的应用程序的任何方法?
Any way to recycle application in two servers at once programmatically?
我已使用此语法回收我的应用程序。
HttpRuntime.UnloadAppDomain();
您可能会问为什么我需要回收我的应用程序,它是为了清除缓存,并且此语法被编程为仅在应用程序中执行的更新期间工作,这种情况很少见。
所以,我的问题是,是否有任何方法可以通过此语法实际回收另一台服务器?
它是一个 webfarm,因此,只有当前服务器的缓存被上面的语法清除。
我之前问过类似的问题:
我无法应用上一个问题中给出的建议的原因是我的组织不同意实施第三方工具。
请指教..
您不必使用第三方提供商来实现您自己的 OutputCacheProvider
,我在回答您之前的问题时提供的链接 只是建议分布式缓存,因为您在问关于为您的网络场设置 one 缓存。如果您很高兴拥有每个服务器的缓存并且只想使一个条目无效,您仍然可以实现自己的缓存提供程序并且只是有一些方法可以使网络场中所有服务器上的缓存无效。
考虑这样的事情:
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)
我现在还没有测试过这段代码,但我之前已经实现了类似的东西,你可能明白我的意思了吗?
我已使用此语法回收我的应用程序。
HttpRuntime.UnloadAppDomain();
您可能会问为什么我需要回收我的应用程序,它是为了清除缓存,并且此语法被编程为仅在应用程序中执行的更新期间工作,这种情况很少见。
所以,我的问题是,是否有任何方法可以通过此语法实际回收另一台服务器? 它是一个 webfarm,因此,只有当前服务器的缓存被上面的语法清除。
我之前问过类似的问题:
我无法应用上一个问题中给出的建议的原因是我的组织不同意实施第三方工具。
请指教..
您不必使用第三方提供商来实现您自己的 OutputCacheProvider
,我在回答您之前的问题时提供的链接
考虑这样的事情:
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)
我现在还没有测试过这段代码,但我之前已经实现了类似的东西,你可能明白我的意思了吗?