:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  
 

Resources

Finding Correct Content Managemet System
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Workflow Managemet Systems
Workflow management is a crucial component in organizing a variety of business processes so that they benefit the business as a whole and increase profitability...

Using the Power of Content Management Systems
With page editors that resemble a word processor program, adding content with a CMS interface is simple and fun. Most CMS software also allows you to change the location of your content pages and links easily, while the back end processes takes care of updating the links throughout your site...

Content Management Systems (CMS): What They Are And Why We Love Them
In the past, individuals who took interest in having and operating their own websites were burdened with the task of learning HTML, DHTML, and other web-based technologies such as JavaScript and CSS. The only alternative to this was, unfortunately, to pocket the expenses and costs required to pay a web developer to build and maintain it for them...

Outsourcing
Post your project for outsourcing and get bids from qualified programmers, designers, interpreters, copywriters.


 

Code Walkthroughs

Datagrid Formatting the Data
We are able to format the content of the datagrid cell by one of two simple methods, dependant upon whether the column is a bound column or whether it is a template column. In our example we shall format the column to have to digits after the decimal point , followed by a...

Datagrid Highlight a Row With Click Through
It is relatively easy to add alternating colours to the rows in your datagrid. However, when we move the mouse over the rows we may want to highlight this row, and possibly to add the option of a click through based on the row selected...

Add a Delete Button to a Datagrid
To add a delete button to a datagrid follows a similar process to adding an edit button. In the datagrid header...

Add an Edit Button to a Datagrid
The datagrid has a predefined editColumn for handling the editing of a datagrid. Adding this simple column definition to a datagrid adds a powerful feature. When a row is not in edit mode the column item shows the word...

Making a Datagrid Row Editable
Two of the most popular methods of editing a datagrid in asp.net are to either select the row and take the user off to a different presentation of the data, or to change the formatting of the row presented in the database with appropriate edit text boxes, checkboxes and...

Adding Tooltips to Datagrid Rows
Adding tooltips to datagrid rows is easy, assuming that you have already created the code for adding row highlighting. In this article I shall assume that you have already read the article entitled Datagrid Highlight a Row With Click Through...

Binding a Datagrid to an Access Database
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Adding Data to a DropDownList
The aim of this article is to answer the question 'How do I add items to a DropDownList?' Initially as part of the declaration for the DropDownList we can also define a number of items, much in the same way as in classic ASP...

Getting Current Date Time
In classic ASP we had now() which would return the current date and time. For asp.net this no longer exists. So what should we use...

Test if File Exists
Sometimes, in order to reduce our chance of error, when working with the filesystem in ASP.NET, we need to determine wether a file exists before performing an action on it. The following short piece of code will enable us to test whether a file exists...

Using Javascript with ASP.NET Form Elements
Adding simple pieces of Javascript to an Asp.net page can be acheived by adding to the attributes of the particular imagebutton or linkbutton. if its normal ASP.Net Button then you can...

Regular Expressions
In the table below we list the characters used in .Net regular expressions, together with their meaning, But first...

Authentication in Asp.net
Forms authentication in ASP.Net is far more easier and safe than Asp 3. It is possible to place a web.config file in any directory of a web site.Therefore, we are able to make most of a web site public, whilst providing authentication on, say, one directory...

Discussion Forums

General ASP.NET

.Net Programming

cSharp Home

Sql Server Home

Javascript / Client Side Development

IT Jobs

Ajax Programming

Ruby on Rails Development

Perl Programming

C Programming Language

C++ Programming

Python Programming Language

Laptop Suggestions?

TCL Scripting

Fortran Programming

Scheme Programming

17. Output Caching

FAQ Home
   17.1 How can I remove the cache of a page (cached with VaryByParam) for certain request params in code?
   17.2 How can I set a page/control to be cached only for certain parameters?
   17.3 What is Output Caching?
   17.4 I want to Cache based on HTTP header .How can I do that?
   17.5 How to make a page expire immediately in such a way that a warning message "This page has expired."appears?
   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.
   17.7 Is there any way to cache by browser a page or User Control?
   17.8 Is caching PDF Files a good or bad idea?
   17.9 How to remove Output Cache by param?
   17.10 How can I programatically invalidate the outputcache from a usercontrol?
   17.11 Is it possible to cache a page by Browser Version and/or some params?
   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?) "?
   17.13 Is there a way that I can clear/expire a page cache from another page/class?
   17.14 How to access the Cache from a compiled class?
   17.15 How can I cache the page by the complete querystring?
   17.16 How do I access the cache in the Global.asax?
   17.17 How to display items stored in a local application Cache?
   17.18 Is there a Cache.RemoveAll()? How can I clear / remove the total cache?
   17.19 Why do I get the error message "Exception Details: System.Web.HttpException: Cache is not available "?
   17.20 How to remove the Cache starting with "cachedata_"?

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



17.2 How can I set a page/control to be cached only for certain parameters?


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



17.3 What is Output Caching?


Output caching lets you cache the output of static aspx pages or portions of pages to improve performance. Since the pages are cached asp.net doesn't have to regenerate the html for every request.

For more information see:

 


17.4 I want to Cache based on HTTP header .How can I do that?



<%@OutputCache ... VaryByHeader="UserAgent" %>


or


<%@ OutputCache ... VaryByHeader="Accept-Language" %>



17.5 How to make a page expire immediately in such a way that a warning message "This page has expired."appears?


Try the following:


<%@ OutputCache location="none" %>



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.


You can use tag


<%@ OutputCache Duration="0" Location="None" VaryByParam="none" %>


or
VB.NET


Response.Cache.SetCacheability(HttpCacheability.NoCache)


C#


Response.Cache.SetCacheability(HttpCacheability.NoCache);



17.7 Is there any way to cache by browser a page or User Control?


Try VaryByCustom="browser" in the user control/page.


17.8 Is caching PDF Files a good or bad idea?


If the PDF files are on the disk, you should just let the IIS handle them. The IIS static file handler performs caching and is much faster than the ASP.NET cache.


17.9 How to remove Output Cache by param?


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



17.10 How can I programatically invalidate the outputcache from a usercontrol?


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



17.11 Is it possible to cache a page by Browser Version and/or some params?


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



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?) "?


Use namespace System.Web.Caching or
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 Response.AddCacheDependency() to make the page dependent on a common key.
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?


17.14 How to access the Cache from a compiled class?


VB.NET


somevar=System.Web.HttpContext.Current.Cache(" ")


C#


somevar=System.Web.HttpContext.Current.Cache(" ");



17.15 How can I cache the page by the complete querystring?


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.


17.16 How do I access the cache in the Global.asax?


Use HttpRuntime.Cache


17.17 How to display items stored in a local application Cache?


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 ()+ "
" ) ;
}



17.18 Is there a Cache.RemoveAll()? How can I clear / remove the total cache?