You don't have to use a third party provider to implement your own OutputCacheProvider
, the links I provided in the answer to your previous question Is there any way to clear cache from server farm? just suggested distributed cache because you were asking about having one cache for your web farm. If you are happy enough to have a per server cache and just want to invalidate an entry you can still implement your own cache provider and just have some way of invalidating the cache on all servers in the web farm.
Consider something like this:
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
So basically you would be using a file on a network share or something to invalidate your cache. When you want to force the application to invalidate the cache you would just have to update the file somehow:
'Take an action that will affect the write time.
File.SetLastWriteTime(_cacheDependencyFile, Now)
I haven't tested this code now but I have previously implemented something similar and you can probably see what I'm getting at right?