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 sol开发者_如何学Cution. Is this possible?
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.
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.
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
精彩评论