views:

1005

answers:

2

In which situations should we use Query-Strings (VaryByParam) to achieve Caching in ASP.NET?

Can anyone give an example of a real-life situation in case of an web-application?

+3  A: 
Products.aspx?productID=12345

Obviously you don't want the output cache of Products.aspx to have the data for the first product that got requested.

In detail

If I hit Products.aspx?productID=12345 the page will be processed for me, look up the info for product 12345, and cache the results. Then you hit Products.aspx?productID=54321, the page will not be processed for you but products.aspx will be retrieved from the output cache and you'll see the info for 12345. Not good. VaryByParam resolves this.

Rex M
You're correct, of course. Maybe I read the question from another angle, but my question would be do you actually want to cache this page at all? You'd possibly have as many pages in cache as you do products, and by my count that could be as many as 12,345! ;)
Kurt Schindler
@Kurt of course that is a call you need to make based on a number of factors. If your product pages are extremely db-intensive; you have enough repeat traffic to product pages to justify caching them, and you have 32gb of memory on your web front-ends, why not cache 100k product pages at 25k each - that's less than 3gb of memory.
Rex M
A: 

The most classic example may be: A product catalog

It may have a URL structure like so:

http://www.myshop.com/Catalog/ViewAllProducts.aspx

http://www.myshop.com/Catalog/ViewAllProducts.aspx?PageNum=123&PageSize=50

http://www.myshop.com/Catalog/ViewProduct.aspx?ProductID=12345

The first URL is the complete list of all products. There's no querystring here, but the second URL is the same page but with pagination. The Page Number (PageNum) and the number of items per page (PageSize) are both querystrings, so assuming the user couldn't re-order the product list, two different users requesting the:

http://www.myshop.com/Catalog/ViewAllProducts.aspx?PageNum=123&PageSize=50

URL would be getting the same information. The first request could cache this data so that the second request at a later time doesn't need to go back to the database to get the product items (and the details related to them: price, description etc.) in order to display them.

The third URL is a single product detail screen with the Product ID (which could be a unique identifier from the database) as it's single querystring parameter. Multiple requests for this will almost always need to return the same data (unless the price or description or some other element of that specific product changes on a frequent basis). Caching based upon the "ProductID" querystring (and it's variations in value for different products) will greatly reduce load of the database. Ensuring the cache duration isn't too long will ensure that the cache is expired in a reasonable time should the actual back-end database data change.

CraigTP