Output Caching
Output Caching
17.1 How can I remove the cache of a page (cached with VaryByParam)
for certain request params in code?
You can attach a cache dependency to the response that is unique per query param, then invalidate the
VaryByCustom - is a string that your application has to interpret in the GetVaryByCustomString override in
Now, to avoid caching pages for certain ProductIDs you can set the cacheability to private or nocache from
VB.NET
Response.Cache.SetCacheability(HttpCacheability.Private)
C#
Response.Cache.SetCacheability(HttpCacheability.Private) ;
Since the pages are cached asp.net doesn't have to regenerate the html for every request.
or
17.6 How to prevent client Cache? I want every client request get sent to
the server even if it's behind a proxy server and for any browser setting.
or
VB.NET
Response.Cache.SetCacheability(HttpCacheability.NoCache)
C#
Response.Cache.SetCacheability(HttpCacheability.NoCache);
If the PDF files are on the disk, you should just let the IIS handle them. The IIS static file handler performs
You can attach a cache dependency to the response that is unique per query param, then invalidate the
VB.NET
C#
You could add a dependency to the output cached control, and change the dependency to evict the control.
VB.NET
C#
if (Parent is System.Web.UI.BasePartialCachingControl)
{
Cache["dependent"] = "dependent";
CacheDependency dep = new CacheDependency(null, new string[] "dependent");
((System.Web.UI.BasePartialCachingControl)Parent).Dependency = dep;
}
VB.NET
C#
17.12 Why do I get the error message "The type or namespace name
'CacheDependency' could not be found (are you missing a using directive
or an assembly reference?) "?
refer to it as System.Web.Caching.CacheDependency
17.13 Is there a way that I can clear/expire a page cache from another
page/class?
No, not easily unless you use the output cache APIs on Response.Cache and take advantage of the
Then, from the other page or class you could invalidate the common key which would enforce a dependency
eviction (since the key page1 depended on changed). Refer How to remove OutputCache by param?
VB.NET
somevar=System.Web.HttpContext.Current.Cache(" ")
C#
somevar=System.Web.HttpContext.Current.Cache(" ");
This should result in any changes to querystring parameters causing a new version of the page to be cached.
Keep in mind that this can significantly increase the amount of memory used for caching, depending on how
Note: "*" is not recommended - it is best to use a list of params that your page truly varies by.
Use HttpRuntime.Cache
VB.NET
C#
foreach(DictionaryEntry objItem in Cache)
{
Response.Write ("Key :" + objItem.Key.ToString () + "
");
Response.Write(" Value :" + objItem.Value.ToString ()+ "
");
}
VB.NET
C#
Try using "HttpContext.Current.Cache" instead of just "Cache" in your code. The compiler probably wasn't
VB.NET
C#
IDictionaryEnumerator enumerator = Cache.GetEnumerator();
while(enumerator.MoveNext())
{
String key = (String) ((DictionaryEntry) enumerator.Current).Key;
if(key.StartsWith("cachedata_"))
{
// Print it:
Response.Write("Deleting key " + key + "
");
// Remove it:
Cache.Remove(key);
}
}