Friday, 6 July 2012

How to force clear cache in asp.net

 

Step1: Write on page load of every web page

 Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.Cache.SetNoStore();

 Step:2

i try this but it seems not working
 
            public void DisablePageCaching()
            {
                  //Used for disabling page caching
                  HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
                  HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
                  HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
                  HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                  HttpContext.Current.Response.Cache.SetNoStore();
                  HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
            }
 
            private void ClearCache()
            {
                  DictionaryEntry entry = default(DictionaryEntry);
                  foreach (DictionaryEntry entry_loopVariable in System.Web.HttpContext.Current.Cache)
                  {
                        entry = entry_loopVariable;
                        System.Web.HttpContext.Current.Cache.Remove(entry.Key.ToString());                                   
                  }
 
                  IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();
 
                  while (enumerator.MoveNext())
                  {
                        HttpContext.Current.Cache.Remove(enumerator.Key.ToString());
                  }
                  HttpContext.Current.Response.ClearHeaders();
                  HttpContext.Current.Response.Expires = 0;
                  HttpContext.Current.Response.CacheControl = "no-cache";
                  HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                  HttpContext.Current.Response.Cache.SetNoStore();
                  HttpContext.Current.Response.Buffer = true;
                  HttpContext.Current.Response.ExpiresAbsolute =DateTime.Now.Subtract(new TimeSpan(1,0,0,0));
                  HttpContext.Current.Response.AppendHeader("Pragma", "no-cache");
                  HttpContext.Current.Response.AppendHeader("", "");        
                  HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1
                  HttpContext.Current.Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1                 
            }

No comments:

Post a Comment