Subscribe

RSS Feed (xml)

Varying Output Caching by Parameter(s)

There are two techniques for varying by parameters in pages; partial page caching only


supports the


@OutputCache directive.


<%@OutputCache Duration="60" VaryByParam="productId" %>


[content specific to the product id value]


or


<script runat="server">


' Note, this only works for pages and not user controls


Sub Page_Load(object As Sender, e As EventArgs)


Response.Cache.SetExpires(DateTime.Now.AddSeconds(100))


Response.Cache.SetCacheability(HttpCacheability.Public)


Response.Cache.SetValidUntilExpires(true)


Response.Cache.VaryByParams("productId") = True


End Sub


</script>


[content specific to the product id value]


Dynamic pages usually accept parameters and potentially display different output determined


by these parameters. A common example is a page used to display product information,


such as products.aspx?productId=10.


The page is passed a single parameter, the product ID, and the content is retrieved for


that particular product. Output caching allows you to cache the results of a dynamically


generated page so you don't need to execute the page on each request.Varying the output


cache by parameters allows you to cache multiple views of the page based on parameters


that affect the output, such as a products.aspx page that accepts a productId


parameter.


Multiple parameters can be specified using the @OutputCache directive by using a


semicolon to separate items. For example, if you were generating output based on the


productId and the salesId, you could specify the following:


<%@OutputCache Duration="60" VaryByParam="productId;salesId" %>


Using the @OutputCache directive, it is also possible to vary all parameters.This isn't a


recommended approach, but if you're not sure how many parameters you depend upon,


using VaryByParam = "*" will vary by all parameters. Conversely, using VaryByParam =


"none" will vary by no parameters.

No comments:

Archives

Variety in the Web World