<%@OutputCache Duration="600" VaryByParam="*" %>
然而,如果有人发布了评论,我希望清除缓存,以便刷新页面并显示评论。
在ASP.Net C#中,我该如何做到这一点?
<%@OutputCache Duration="600" VaryByParam="*" %>
然而,如果有人发布了评论,我希望清除缓存,以便刷新页面并显示评论。
在ASP.Net C#中,我该如何做到这一点?
我找到了我要寻找的答案:
HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
如果您知道要清除缓存的页面,上述方法都是可以的。在我的情况下(ASP.NET MVC),我从各个地方引用了相同的数据。因此,在我执行[保存]操作时,我希望在整个网站上清除缓存。以下是适用于我情况的解决方法:http://aspalliance.com/668
这是在OnActionExecuting过滤器的上下文中完成的。也可以通过覆盖BaseController中的OnActionExecuting来轻松实现。
HttpContextBase httpContext = filterContext.HttpContext;
httpContext.Response.AddCacheItemDependency("Pages");
准备工作:
protected void Application_Start()
{
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
}
小调整: 我有一个帮助器,它会添加“闪存消息”(错误消息、成功消息-“此项已成功保存”等)。为了避免闪存消息在每个后续GET请求中都显示出来,我必须在写入闪存消息后使其无效。
清除缓存:
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
希望这能有所帮助。OutputCacheAttribute.ChildActionCache = new MemoryCache("NewRandomStringNameToClearTheCache");
来清除整个“子操作缓存”(适用于所有子操作 - 似乎没有更好的方法,除非使用库)。http://dotnet.dzone.com/articles/programmatically-clearing-0 - user2173353 public class Page : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
try
{
string cacheKey = "cacheKey";
object cache = HttpContext.Current.Cache[cacheKey];
if (cache == null)
{
HttpContext.Current.Cache[cacheKey] = DateTime.UtcNow.ToString();
}
Response.AddCacheItemDependency(cacheKey);
}
catch (Exception ex)
{
throw new SystemException(ex.Message);
}
base.OnLoad(e);
}
}
// Clear All OutPutCache Method
public void ClearAllOutPutCache()
{
string cacheKey = "cacheKey";
HttpContext.Cache.Remove(cacheKey);
}
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
并在退出按钮点击时:
Session.Abandon();
Session.Clear();
//add dependency
string key = "post.aspx?id=" + PostID.ToString();
Cache[key] = new object();
Response.AddCacheItemDependency(key);
当有人添加评论时...
Cache.Remove(key);
我猜这个方法即使使用 VaryByParam * 也能生效,因为所有请求都会与同一个缓存依赖项相关联。
嗯。您可以在OutputCache项上指定VaryByCustom属性。该值作为参数传递给您可以在global.asax中实现的GetVaryByCustomString方法。此方法返回的值用作缓存项的索引 - 如果例如返回页面上的评论数量,则每次添加评论时都会缓存一个新页面。
但是,这样做的警告是,它实际上并没有清除缓存。如果博客条目得到大量评论使用,使用此方法可能会导致缓存爆炸。
或者,您可以将页面的不可更改部分(导航、广告、实际博客条目)实现为用户控件,并在每个用户控件上实现部分页面缓存。
HttpRuntime.Close()
.. 我尝试了所有的方法,这是唯一一个对我有效的方法