Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Clear Cache

If you cache pages in a HttpHandler with

_context.Response.Cache.SetCacheability(HttpCacheability.Public);
_context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(180));

is it possible to clear a certain page from the cache?

like image 565
Lieven Cardoen Avatar asked Jul 30 '26 15:07

Lieven Cardoen


2 Answers

is it possible to Clear a certain page from the Cache?

Yes:

HttpResponse.RemoveOutputCacheItem("/pages/default.aspx");

You can also use Cache dependencies to remove pages from the Cache:

this.Response.AddCacheItemDependency("key");

After making that call, if you modify Cache["key"], it will cause the page to be removed from the Cache.

In case it might help, I cover caching in detail in my book: Ultra-Fast ASP.NET.

like image 168
RickNZ Avatar answered Aug 02 '26 07:08

RickNZ


more simple one...

public static void ClearCache()
{
    Cache cache = HttpRuntime.Cache;

    IDictionaryEnumerator dictionaryEnumerators = cache.GetEnumerator();

    foreach (string key in (IEnumerable<string>) dictionaryEnumerators.Key)
    {
        cache.Remove(key);
    }

}
like image 41
Mortaza Kamal Nourestani Avatar answered Aug 02 '26 09:08

Mortaza Kamal Nourestani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!