您应该能够创建自定义OutputCacheProvider。
在您的 global.asax 中:
public override string GetOutputCacheProviderName(HttpContext context)
{
bool isInSession = true; // Determine here.
if (isInSession)
{
return "CustomProvider";
}
// Or by page:
if (context.Request.Path.EndsWith("MyPage.aspx"))
{
return "SomeOtherProvider";
}
return base.GetOutputCacheProviderName(context);
}
然后创建你的提供者:
public class SessionBasedCacheProvider : OutputCacheProvider
{
public override object Get(string key)
{
return null; // Do not cache.
}
public override object Add(string key, object entry, DateTime utcExpiry)
{
// Basically let it "fall through" since we don't want any caching.
return entry;
}
public override void Set(string key, object entry, DateTime utcExpiry)
{
// Basically let it "fall through" since we don't want any caching.
}
public override void Remove(string key)
{
// Basically let it "fall through" since we don't want any caching.
}
}
通过从Set 返回null,您将不会缓存这些项目。
标识缓存中指定条目的键值,或
如果指定的条目不在缓存中,则为 null。
并将您的提供者添加到配置中:
<system.web>
<caching>
<outputCache defaultProvider="AspNetInternalProvider">
<providers>
<clear/>
<add name="CustomProvider" type="YourNamespace.SessionBasedCacheProvider, YourNamespace, Version=1.0.0.0, Culture=neutral"/>
</providers>
</outputCache>
</caching>
</web>
要启用它,您应该可以使用它。只需将其设置为 ASPX 页面的顶部即可。请注意 Location 属性,具体取决于您希望它缓存的位置。
<%@ OutputCache Duration="60" VaryByParam="None" Location="Server" %>
https://msdn.microsoft.com/en-us/library/hdxfb6cy(v=vs.85).aspx
https://msdn.microsoft.com/en-us/magazine/gg650661.aspx
您也可以始终使用相同的 CacheProvider,并让它决定是否应该缓存该项目。
public class CustomOutputCacheProvider : OutputCacheProvider
{
public override object Add(string key, object entry, DateTime utcExpiry)
{
// Determine if in session.
bool isInSession = true;
if (isInSession)
{
return null;
}
// Do the same custom caching as you did in your
// CustomMemoryCache object
var result = HttpContext.Current.Cache.Get(key);
if (result != null)
{
return result;
}
HttpContext.Current.Cache.Add(key, entry, null, utcExpiry,
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
return entry;
}
public override object Get(string key)
{
return HttpContext.Current.Cache.Get(key);
}
public override void Remove(string key)
{
HttpContext.Current.Cache.Remove(key);
}
public override void Set(string key, object entry, DateTime utcExpiry)
{
HttpContext.Current.Cache.Add(key, entry, null, utcExpiry,
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
}
}
来自http://www.haneycodes.net/custom-output-caching-with-mvc3-and-net-4-0-done-right/的代码
注意,我没有用 Session 测试过这个,但理论上它应该可以工作。但是我建议您在发布之前对其进行真正的测试。缓存总是比人们想象的要困难得多... :(
更新:由于 Session 不可用,您应该可以使用 cookie。
protected void Session_Start(object sender, EventArgs e)
{
Response.Cookies.Add(new HttpCookie("SessionCookie", "some_value"));
}
public override string GetOutputCacheProviderName(HttpContext context)
{
bool isInSession = true; // Determine here.
if (context.Request.Cookies["SessionCookie"] != null)
{
// Use your CustomProvider
}
if (isInSession)
{
return "CustomProvider";
}
// Or by page:
if (context.Request.Path.EndsWith("MyPage.aspx"))
{
return "SomeOtherProvider";
}
return base.GetOutputCacheProviderName(context);
}
记得删除 cookie,或以某种方式处理它。