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?
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?
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.
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.
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.