开发者

How to use page OutputCache and VaryByCustom to take into account multiple criteria in ASP.NET 2.0+?

开发者 https://www.devze.com 2023-03-08 05:27 出处:网络
We have a site that is multi-lingual and has some personalization after the user\'s login (e.g. headers show different links, the user\'s username, etc.), but we would like to cache particular public

We have a site that is multi-lingual and has some personalization after the user's login (e.g. headers show different links, the user's username, etc.), but we would like to cache particular public facing pages that are the versions of the page before the user logs in (e.g. for people just browsing the site).

Is there a way to take the fo开发者_开发知识库llowing multiple criteria into account when using VaryByCustom in an ASP.NET 2.0 site (yes, we will eventually upgrade, but not now) and how would you hook a page up to pass multiple criteria into the method?

  1. A custom ASP.NET membership profile property called Profile.Preferences.Language (we will inspect the URL scheme and set the Language property to a valid value before VaryByCustom).
  2. The user's browser
  3. Any querystrings/params

Those three cases would allow us to cache our frequently hit public pages and keep from having to compute things/hit the DB or other caches to get the source of menu items, etc. for pages that infrequently change.

Any help would be greatly appreciated.

Thank you,


For number 2 and 3 you can use the following

<%@ OutputCache Duration="Whatever you want in seconds" VaryByParam="*" VaryByCustom="browser"%>

Sorry I can't help you for the 1st one


I would suggest overriding the GetVaryByCustomString method in the Global.asax to create your own cachekey based on the above three criteria.

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    const string KEY = "UserSpecific";
    if (arg == KEY) {
         //perform some logic to generate a unique key per querystring, brower and profile
         string cacheKey = context.Request.QueryString["someparam"] + ...
         return cacheKey;
     }

    return base.GetVaryByCustomString(context, arg);
}

And modifying the outputcache directive to utilize this new cachekey generation logic.

<%@ OutputCache Duration="300" VaryByParam="none" VaryByCustom="UserSpecific" %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号