Is it possible to clear the ASP.NET application Cache without resetting the AppDomain?
Solution 1
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.
Solution 2
Dim CacheKeys As New List(Of String)
For Each CacheItem As DictionaryEntry In Cache
CacheKeys.Add(CacheItem.Key)
Next
For Each Key As String In CacheKeys
Cache.Remove(Key)
Next
This appears to go through the cache and clear the items but it does not clear the Response cache. If you login to a webpage using FormsAuthentication, goto a webpage, logout, then just type that URL into the address bar it return you to that page. When you click on anything it return you to the login page because you are not authenticated but the page still displays from the cache even though the cache is cleared when the logout button is clicked. Setting the Cachability to NoCache does not work because all GridViews that use DataBound event show as expired when using the back button. I am still looking for a proper cache clearing solution for this case.
Solution 3
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
Bryan
Updated on June 05, 2022Comments
-
Bryan almost 2 years
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?
-
SLaks over 14 yearsThis will not work. You can't modify a collection in a
For Each
loop. -
SLaks over 14 yearsThis will not work. You can't modify a collection in a
foreach
loop. -
Rubens Farias over 14 years@Slaks, I just ran here without major problems (just a missing .ToString()); can you please confirm that problem?
-
Bryan over 14 yearsOK, 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!
-
SLaks over 14 yearsI just checked the source. Calling
GetEnumerator
makes a complete copy of the cache, so it isn't a correctly behaved enumerator. -
Ankur-m about 11 yearsThis will throw an exception if the Cache Collection is modified. You may like to check this link - stackoverflow.com/questions/6212676/…