Show or clear cache in ASP.NET

Having been building a few websites in EKTRON CMS, we have had a few development issues with objects being cached and therefore updates to the website have not been shown. To solve this issue I have created a ASP.Net clear cache function that allows you to view the cache and also clear it. This can be applied to any .Net website (c#):

     private void LoadCache(bool clearCache)
     {
            IDictionaryEnumerator ide = HttpContext.Current.Cache.GetEnumerator();
            ide.Reset();
            StringBuilder html = new StringBuilder();
            while (ide.MoveNext())
            {
                    if (clearCache)
                    {
                            HttpContext.Current.Cache.Remove(ide.Key.ToString());
                            html.Append("Cleared: ");
                    }
                    html.Append(ide.Key.ToString()).AppendLine("<br />");
            }
            CacheOutput.Text = html.ToString();
      }

       protected void ViewClick(object sender, EventArgs e)
       {
               LoadCache(false);
       }

       protected void ClearClick(object sender, EventArgs e)
       {
               LoadCache(true);
       }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>