![]() |
![]() |
Hawkfield: web development & web designYour partner in IT - Gent Belgium
VaryByCustom Output CachingOutput caching is a very nice feature of ASP.NET to significantly reduce the load on your webservers and increase the response time to your users. What ASP.NET does, is save the HTML output of a certain page into memory to be served multiple times without re-running the same page over and over again. The Output Caching is easily enabled on a page per page base with the following tag: <%@ OutputCache Duration="#seconds" VaryByParam="*" %> Duration is the amount of seconds after which ASP.NET should refresh the page. Multiple caches of the same page can exist in memory. For example, when you adres a page with the following url: MyPage.aspx?Language=UK, or going to that same page with the url: MyPage.aspx?Language=FR will generally output different data. The VaryByParam is used for this. Enter "none" if you just want 1 page in the cache, no matter what parameters the users use to visit it. Use for example "Language" to have (in this case) two instances of the page in memory (one with the parameter FR and one with UK). Use "*" to vary by any parameter that is given to the page. This is all very basic output caching, in this article we will go into more detail on the VaryByCustom attribute. VaryByCustom, you can add your own tag, for example VaryByCustom="CookieUser" if you would like to output a different page depending on the User stored in the cookie. To make this work, you will need to override GetVaryByCustomString in your Global.asax. Use this as an example on how to do so: /* context will contain an instace of the current Http Context, containing for example the Request object or the Cookie object (which we will use in this example) */ /* arg will be the parameter you specified in your page under the VaryByCustom attribute. For example when using this: VaryByCustom="CookieUser" the arg would contain 'CookieUser" */ Public override string GetVaryByCustomString(HttpContext context, string arg) { // We compare the arg to what we would like to do if(arg.ToLower() == "cookieuser") { HttpCookie cookie = context.Request.Cookies["User"]; //We have no cookie ? if(cookie != null) //Return what is contained inside the cookie return cookie.Value; } return base.GetVaryByCustomString(context, arg); } If you go to any page having this tag: <%@ OutputCache Duration="60" VaryByParam="none" VaryByCustom="CookieUser" %> You will get a different page for any different value you have stored in the cookie. If you would like to experiment with the Output Caching, the easiest way is to add (and display) the time on your page. You can easily check if you did get a new, or a cached page then. One remark though, from my personal experiance, I haven't been able to vary by a Session variable. Some claim the session is not yet available when ASP.NET checks it's cache to see if a certain page is in there, that could be, I have no idea. I was usually able to somve this issue by storing the variable inside a cookie, and using that value instead of the session variable. |
||||||