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?
views:
247answers:
2
+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
2010-02-03 01:24:49
This will not work. You can't modify a collection in a `foreach` loop.
SLaks
2010-02-03 01:42:04
@Slaks, I just ran here without major problems (just a missing .ToString()); can you please confirm that problem?
Rubens Farias
2010-02-03 01:48:44
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
2010-02-03 02:37:03
@Rubens: I just checked the source. Calling `GetEnumerator` makes a complete copy of the cache, so it isn't a correctly behaved enumerator.
SLaks
2010-02-03 02:52:44
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
2010-02-03 01:25:22