1

I have a typical requirement over here.

I have a web application developed in asp.net 4.0. In this application I want to add a button to one of the web form, which will perform the following task :

  1. It should clear the server side cache, by this I mean it should clear the cache of the same IIS server in which the application is hosted in.

Now, I am aware of that we can achieve it manually by deleting the files present over here,

%systemroot%\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
%systemroot%\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

And we need to shut down the IIS before deleting the temporary files and restart it again after that. In order to avoid any dependencies on the cached files.

But I want to know how can I achieve it in the button click event of the web form. Please help me out with this requirement.

Bibhu
  • 4,053
  • 4
  • 33
  • 63
  • what problem is this solving for you? Usually clearing a server side cache is more application logic (data structure, etc.) and it what you at describing here. – Joe Jul 30 '12 at 11:09

3 Answers3

2

I currently use the following in my application:

    public static void ClearAspNetCache(HttpContext context) {
        foreach (DictionaryEntry entry in context.Cache) {
            context.Cache.Remove((string)entry.Key);
        }
    }

Here's a source reference which might be quite useful:

http://aspadvice.com/blogs/ssmith/archive/2005/11/14/Extending-the-ASPNET-Cache-Object-Cache-Clear.aspx#33197

Ryan
  • 503
  • 3
  • 14
1

Have a try with this if you want to remove all the items from the cache :

IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();    
while (enumerator.MoveNext())
{    
    HttpContext.Current.Cache.Remove(enumerator.Key);    
}
Giorgio Minardi
  • 2,765
  • 1
  • 15
  • 11
  • It will clear everything is cached for THAT application (you can have multiple websites on IIS and their cache will not be affected). Temporary files inside the asp.net folder are fully compilated files of your app, they are not related with the cache (they are created every time a page is hit). Here's some info: http://stackoverflow.com/questions/450831/what-is-the-temporary-asp-net-files-folder-for – Giorgio Minardi Jul 30 '12 at 11:21
  • Just want to know from you, that, what would be the effect if we delete the temporary file's in the button click ? – Bibhu Jul 30 '12 at 11:24
  • You won't get any beneficial effect by doing it. Worst, you site will be slower because it will recompile and recreate those files when there's a new page request. – Giorgio Minardi Jul 30 '12 at 11:26
  • that means there is a possibility of denided acess to the file to delete it if some user is accessing the page. Doesn't it ? Resulting an exception. – Bibhu Jul 30 '12 at 11:28
1

Removing items from HttpContext.Current.Cache will NOT clear your IIS cache. It's server side cache (managed by ASP.NET Runtime), not web server cache.

Check out this link. http://blogs.msdn.com/b/dougste/archive/2008/08/11/clearing-out-temporary-asp-net-files.aspx

berliner
  • 1,887
  • 3
  • 15
  • 23