对于我的博客,我想使用输出缓存来保存一个缓存版本的永久帖子大约10分钟,这是很好的…
<%@OutputCache Duration="600" VaryByParam="*" %>
但是,如果有人发表了评论,我想要清除缓存,这样页面就会刷新,评论就可以看到了。
如何在ASP.Net C#中执行此操作?
发布于 2008-08-14 20:04:36
我找到了我想要的答案:
HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
发布于 2010-05-21 02:24:32
如果您知道要为哪些页清除缓存,则上述方法很好用。在我的实例(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中都显示flash消息,我必须在写入flash消息后使其失效。
清除缓存:
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
希望这能有所帮助。
发布于 2009-01-06 13:55:03
使用Response.AddCacheItemDependency清除所有输出缓存。
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);
}
这也可以在ASP.NET MVC的OutputCachedPage中使用。
https://stackoverflow.com/questions/11585
复制相似问题