C# 清除 ASP.NET 中的页面缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing Page Cache in ASP.NET
提问by GateKiller
For my blog I am wanting to use the Output Cache to save a cached version of a perticular post for around 10 minutes, and thats fine...
对于我的博客,我想使用输出缓存将特定帖子的缓存版本保存大约 10 分钟,这很好...
<%@OutputCache Duration="600" VaryByParam="*" %>
However, if someone posts a comment, I want to clear the cache so that the page is refreshed and the comment can be seen.
但是,如果有人发表评论,我想清除缓存,以便刷新页面并可以看到评论。
How do I do this in ASP.Net C#?
我如何在 ASP.Net C# 中做到这一点?
采纳答案by GateKiller
I've found the answer I was looking for:
我找到了我正在寻找的答案:
HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
回答by John Christensen
Hmm. You can specify a VaryByCustom attribute on the OutputCache item. The value of this is passed as a parameter to the GetVaryByCustomString method that you can implement in global.asax. The value returned by this method is used as an index into the cached items - if you return the number of comments on the page, for instance, each time a comment is added a new page will be cached.
唔。您可以在 OutputCache 项上指定 VaryByCustom 属性。此值作为参数传递给您可以在 global.asax 中实现的 GetVaryByCustomString 方法。此方法返回的值用作缓存项的索引 - 例如,如果您返回页面上的评论数量,则每次添加评论时都会缓存一个新页面。
The caveat to this is that this does not actually clear the cache. If a blog entry gets heavy comment usage, your cache could explode in size with this method.
需要注意的是,这实际上并没有清除缓存。如果博客条目获得大量评论使用,则使用此方法您的缓存可能会爆炸。
Alternatively, you could implement the non-changeable bits of the page (the navigation, ads, the actual blog entry) as user controls and implement partial page caching on each of those user controls.
或者,您可以将页面的不可更改部分(导航、广告、实际博客条目)实现为用户控件,并在每个用户控件上实现部分页面缓存。
回答by palmsey
If you change "*" to just the parameters the cache should vary on (PostID?) you can do something like this:
如果您将“*”更改为缓存应该改变的参数(PostID?),您可以执行以下操作:
//add dependency
string key = "post.aspx?id=" + PostID.ToString();
Cache[key] = new object();
Response.AddCacheItemDependency(key);
and when someone adds a comment...
当有人添加评论时......
Cache.Remove(key);
I guess this would work even with VaryByParam *, since all requests would be tied to the same cache dependency.
我想这甚至可以与 VaryByParam * 一起使用,因为所有请求都将绑定到相同的缓存依赖项。
回答by GateKiller
Using Response.AddCacheItemDependency to clear all outputcaches.
使用 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);
}
This is also can be used in ASP.NET MVC's OutputCachedPage.
这也可以在 ASP.NET MVC 的 OutputCachedPage 中使用。
回答by Brian Scott
why not use the sqlcachedependency on the posts table?
为什么不在posts 表上使用sqlcachedependency?
This way your not implementing custom cache clearing code and simply refreshing the cache as the content changes in the db?
这样您就不会实现自定义缓存清除代码,而是在数据库中的内容发生变化时简单地刷新缓存?
回答by Kevin
The above are fine if you know what pages you want to clear the cache for. In my instance (ASP.NET MVC) I referenced the same data from all over. Therefore, when I did a [save] I wanted to clear cache site wide. This is what worked for me: http://aspalliance.com/668
如果您知道要清除缓存的页面,则上述内容很好。在我的实例 (ASP.NET MVC) 中,我引用了来自各地的相同数据。因此,当我进行 [保存] 时,我想清除整个站点的缓存。这对我有用:http: //aspalliance.com/668
This is done in the context of an OnActionExecuting filter. It could just as easily be done by overriding OnActionExecuting in a BaseController or something.
这是在 OnActionExecuting 过滤器的上下文中完成的。它可以通过在 BaseController 或其他东西中覆盖 OnActionExecuting 来轻松完成。
HttpContextBase httpContext = filterContext.HttpContext;
httpContext.Response.AddCacheItemDependency("Pages");
Setup:
设置:
protected void Application_Start()
{
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
}
Minor Tweak: I have a helper which adds "flash messages" (Error messages, success messages - "This item has been successfully saved", etc). In order to avoid the flash message from showing up on every subsequent GET, I had to invalidate after writing the flash message.
小调整:我有一个助手,它添加了“闪现消息”(错误消息、成功消息 - “此项目已成功保存”等)。为了避免闪现消息出现在每个后续的 GET 中,我不得不在写入闪现消息后失效。
Clearing Cache:
清除缓存:
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
Hope this helps.
希望这可以帮助。
回答by Julien
HttpRuntime.Close()
.. I try all method and this is the only that work for me
HttpRuntime.Close()
..我尝试了所有方法,这是唯一对我有用的方法
回答by Mohd Adil
On the master page load event, please write the following:
在母版页加载事件上,请编写以下内容:
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
and in the logout button click:
并在注销按钮中单击:
Session.Abandon();
Session.Clear();