I have a MVC site that I am developing that is multi-tenant application. I have set up the cache to varybyheader="host". Now I'd like to invalidate the cache only by hostname.
The RemoveOutputCacheItem only takes absolute virtual paths and isn't allowing a custom host name (there by being a non-virtual path).
Any help on how to achieve this?
Thanks.
Update
Here is how I can get the internal cache keys
var runtimeType = typeof(HttpRuntime);
var ci = runtimeType.GetProperty(
"CacheInternal",
BindingFlags.NonPublic | BindingFlags.Static);
var cache = ci.GetValue(ci, new object[0]);
var cachesInfo = cache.GetType().GetField(
"_caches",
BindingFlags.NonPublic | BindingFlags.Instance);
var cacheEntries = cachesInfo.GetValue(cache);
var outputCacheEntries = new List<object>();
foreach (Object singleCache in cacheEntries as Array)
{
var singleCacheInfo =
singleCache.GetType().GetField("_entries",
BindingFlags.NonPublic | BindingFlags.Instance);
var entries = singleCacheInfo.GetValue(singleCache);
foreach (DictionaryEntry cacheEntry in entries as Hashtable)
{
var cacheEntryInfo = cacheEntry.Value.GetType().GetField("_value",
BindingFlags.NonPublic | BindingFlags.Instance);
var value = cacheEntryInfo.GetValue(cacheEntry.Value);
if (value.GetType().Name == "CachedRawResponse")
{
var key = (string)cacheEntry.Value.GetType().BaseType.GetField("_key", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(cacheEntry.Value);
key = key.Substring(key.IndexOf("/"));
outputCacheEntries.Add(key);
}
}
}
var keys = new StringBuilder();
foreach (string key in outputCacheEntries)
{
if (key.Contains(Request.Url.Host))
{
keys.Append(key + " ");
HttpResponse.RemoveOutputCacheItem(key);
}
}
That RemoveOutputCacheItem doesn't work with this key.
They key is generated like this: /HQNnoneV+n+FCNhostVHOSTNAME.comDE
Even a direct call RemoveOutputCache("/HOSTNAME.com") doesn't work either (with vary by custom).
Update #2
So read through the reference source code (http://referencesource.microsoft.com/#System.Web/HttpResponse.cs,3222f830c91ccb06) and it appears that it should attempt to create the custom key. So I should be able to RemoveOutputCache("/") and it should create the custom key for me, but this also appears to not be working as expected, it still appears to clear all keys.