Live Chat Icon For mobile
Live Chat Icon

ASP.NET FAQ

Find answers for the most frequently asked questions
Expand All Collapse All

Try the following:


<%@ OutputCache Duration='10' VaryByParam='*' %> 

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 many querystring parameters you’re using.
Note: ‘*’ is not recommended – it is best to use a list of params that your page truly varies by.

Permalink

You can attach a cache dependency to the response that is unique per query param, then invalidate the dependency for that particular param:


// add cache item dependency on response 
string cacheKey = 'xxx.aspx?' + queryParam; 
Cache[cacheKey] = new object(); 
Response.AddCacheItemDependency(cacheKey); 

// invalidate the dependency 
string cacheKey = 'xxx.aspx?' + queryParam; 
Cache.Remove(cacheKey); 
Permalink

Let’s say you set your output cache directive as follows:


<%@ OutputCache Duration='86400' VaryByParam='RegionID' VaryByCustom='ProductID' %>

VaryByCustom – is a string that your application has to interpret in the GetVaryByCustomString override in the gloabal.asax file.

Now, to avoid caching pages for certain ProductIDs you can set the cacheability to private or nocache from your page load for those pages as follows:

VB.NET


Response.Cache.SetCacheability(HttpCacheability.Private) 

C#


Response.Cache.SetCacheability(HttpCacheability.Private) ;
Permalink

Yes. In your page:


<%@ OutputCache Duration='60' VaryByParams='abc;xyz' VaryByCustom='browsermajorversion' %> 

In your global.asax file:

VB.NET


Public Overrides Function GetVaryByCustomString(context As HttpContext, custom As String) As String
	If custom.ToLower() = 'browsermajorversion' Then
		Dim browser As HttpBrowserCapabilities = context.Request.Browser
			Return browser.Browser + ' ' + browser.MajorVersion
	Else
		Return MyBase.GetVaryByCustomString(context, custom)
	End If
End Function ’GetVaryByCustomString

C#


public override string GetVaryByCustomString(HttpContext context, string custom)
{ 
	if (custom.ToLower() == 'browsermajorversion') { 
		HttpBrowserCapabilities browser = context.Request.Browser; 
		return browser.Browser + ' ' + browser.MajorVersion; 
	} 
	else 
	{ 
	return base.GetVaryByCustomString(context, custom); 
	} 
} 
Permalink

You can attach a cache dependency to the response that is unique per query param, then invalidate the dependency for the particular param:

VB.NET


’add cache item dependency on response 
Dim cacheKey As String = 'webform1.aspx?' + queryParam
Cache(cacheKey) = New Object() ’
Response.AddCacheItemDependency(cacheKey)

’ invalidate the dependency 
Dim cacheKey As String = 'webform1.aspx?' + queryParam
Cache.Remove(cacheKey)

C#


// add cache item dependency on response 
string cacheKey = 'webform1.aspx?' + queryParam; 
Cache[cacheKey] = new object(); 
Response.AddCacheItemDependency(cacheKey); 

// invalidate the dependency 
string cacheKey = 'webform1.aspx?' + queryParam; 
Cache.Remove(cacheKey); 
Permalink

VB.NET


Dim enumerator As IDictionaryEnumerator = Cache.GetEnumerator()
While enumerator.MoveNext()
	Dim key As String = CType(CType(enumerator.Current, DictionaryEntry).Key, String)
	If key.StartsWith('cachedata_') Then
	                ’ Print it:
                	Response.Write(('Deleting key ' + key + '
'))
	                ’ Remove it:
                	Cache.Remove(key)
	End If
End While

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);
	}
}
Permalink

VB.NET


Dim objItem As DictionaryEntry
For Each objItem In  Cache
   	Response.Write(('Key :' + objItem.Key.ToString() + '
'))
   	Response.Write((' Value :' + objItem.Value.ToString() + '
'))
Next 

C#


foreach(DictionaryEntry objItem in Cache) 
{ 
	Response.Write ('Key :' + objItem.Key.ToString () + '
');
	Response.Write(' Value :' + objItem.Value.ToString ()+ '
' ) ;
}
Permalink

You could add a dependency to the output cached control, and change the dependency to evict the control. Here’s code to do that:

VB.NET


If TypeOf Parent Is System.Web.UI.BasePartialCachingControl Then
            	Cache('dependent') = 'dependent'
            	Dim dep As New CacheDependency(Nothing, New String() {'dependent'}) ’
            	CType(Parent, System.Web.UI.BasePartialCachingControl).Dependency = dep
End If

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; 
} 
Permalink

Share with

Share on twitter
Share on facebook
Share on linkedin

Couldn't find the FAQs you're looking for?

Please submit your question and answer.