views:

247

answers:

2

I would like to reset/clear an item in the Cache, but without resetting the application or writing a specialized page just for this. ie, a non-programmatic solution. Is this possible?

+1  A: 

Short answer: No.

ASP.NET Cache doesn't have a administration interface to manage it. You'll need to recycle your application pool, or to to create a simple page to do Delete Items from the Cache in ASP.NET.

EDIT: Inspired on Mick answer, you could to have a page like this (RemoveCache.aspx):

<%@ Page Language="C#" %>
<script runat="server">

    void Page_Load(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(Request.QueryString["name"]))
        {
            foreach (DictionaryEntry item in Cache)
            {
                Cache.Remove(item.Key.ToString());
                Response.Write(item.Key.ToString() + " removed<br />");
            }
        }
        else
        {
            Cache.Remove(Request.QueryString["name"]);
        }
    }

</script>

If you call RemoveCache.aspx all your cache will be removed; running RemoveCache.aspx?name=Products, just Products cache entry will be removed.

Rubens Farias
This will not work. You can't modify a collection in a `foreach` loop.
SLaks
@Slaks, I just ran here without major problems (just a missing .ToString()); can you please confirm that problem?
Rubens Farias
OK, thanks fellas. But writing new code requires resetting the AppDomain. I'm not scared to write code, just wanted to do it without resetting the app!
Bryan
@Rubens: I just checked the source. Calling `GetEnumerator` makes a complete copy of the cache, so it isn't a correctly behaved enumerator.
SLaks
A: 

Only have VB.NET code to hand, but why not loop through the cache removing items?

For Each de As DictionaryEntry In HttpContext.Current.Cache

HttpContext.Current.Cache.Remove(DirectCast(de.Key, String))

Next

Regards

Mick Walker
This will not work. You can't modify a collection in a `For Each` loop.
SLaks