【发布时间】:2015-03-25 23:28:34
【问题描述】:
我有一个正在开发的 MVC 站点,它是多租户应用程序。我已将缓存设置为 varybyheader="host"。现在我想仅通过主机名使缓存无效。
RemoveOutputCacheItem 只接受绝对虚拟路径,不允许自定义主机名(存在非虚拟路径)。
关于如何实现这一点的任何帮助?
谢谢。
更新
这是获取内部缓存键的方法
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);
}
}
RemoveOutputCacheItem 不适用于此键。
他们的密钥是这样生成的:/HQNnoneV+n+FCNhostVHOSTNAME.comDE
即使是直接调用 RemoveOutputCache("/HOSTNAME.com") 也不起作用(因自定义而异)。
更新 #2
所以通读参考源代码 (http://referencesource.microsoft.com/#System.Web/HttpResponse.cs,3222f830c91ccb06),它似乎应该尝试创建自定义密钥。所以我应该能够 RemoveOutputCache("/") 并且它应该为我创建自定义键,但这似乎也没有按预期工作,它似乎仍然清除了所有键。
【问题讨论】:
标签: c# asp.net-mvc model-view-controller