…meie igapäevast IT’d anna meile igapäev…

2011-04-05

Clear ASP.NET application cache

Filed under: Nipid,Programmeerimine — Sander @ 16:46:59
Tags:

From time to time I’ve needed a way to clear ASP.NET cache – you know, the views cached with <%@ OutputCache Duration="3600" VaryByParam="None" %>. Usually the reason has been that changes in the development server needed to be shown while the caching is still “on”.

I used to do this with a change to web.config (add extra line, undo, save – the file is changed, cache is invalidated), but I wanted something that wouldn’t require me to access the test server every time.

if (!string.IsNullOrEmpty(HttpContext.Request.Params["emptycache"]))

{

    foreach (DictionaryEntry entry in HttpContext.Cache)

    {

        HttpContext.Cache.Remove(entry.Key.ToString());

    }

}

Add that snippet to appropriate page/class (for me, HomeController’s Index() method, as I use ASP.NET MVC) – and whenever your URL has ?emptycache=1 in it, application cache is cleared. As simple as that.

1 kommentaar »

  1. Otsin ASP programmeerijat.
    Kui keegi oskab aidata: kaspar.fi@gmail.com

    kommentaar kirjutas Kaspar — 2011-09-23 @ 15:50:13 | Vasta


RSS feed for comments on this post. TrackBack URI

Lisa kommentaar

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Muuda )

Twitter picture

You are commenting using your Twitter account. Log Out / Muuda )

Facebook photo

You are commenting using your Facebook account. Log Out / Muuda )

Google+ photo

You are commenting using your Google+ account. Log Out / Muuda )

Connecting to %s

Blog at WordPress.com.