How can I programmatically clear cache?

你说的曾经没有我的故事 提交于 2019-11-27 03:35:01

问题


In my application (ASP.NET + c#) I need to clear the cache before a user enters an aspx page.

Does anybody have any idea how I can programmatically clear the cache on an aspx page, or in the code behind (c#)?


回答1:


Write following code in the page load event:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now);
    Response.Cache.SetNoServerCaching();
    Response.Cache.SetNoStore();
}



回答2:


You can remove a page from the output cache as follows:

HttpResponse.RemoveOutputCacheItem("MyPage.aspx");

This won't remove it from any client-side cache, so if you want to use this technique you will probably want to disable client-side cache, e.g. by using the following directive in your aspx page:

<%@ OutputCache Location="Server" ...



回答3:


Unless there's some javascript way to clear the cache (which would be awful), you can't.

Your best bet is to ensure the page doesn't get cached at all, by doing as Sukhi suggests - or setting up a no-cache cache profile and using the OutputCache directive.



来源:https://stackoverflow.com/questions/4757733/how-can-i-programmatically-clear-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!