开发者

Is there a way I can implement this specific kind of output cache for ASP.NET?

开发者 https://www.devze.com 2023-01-20 03:55 出处:网络
I want to run a custom logic, before the page life-cycle, to decide what version of a cached page I want to serve to the user.

I want to run a custom logic, before the page life-cycle, to decide what version of a cached page I want to serve to the user.

Example:

If the user is not logged, then I go to a cache dictiona开发者_运维问答ry, catch a version A of the page and serve to the user. Otherwise, if it's logged, then I'll see if I already cached a version of the page specific to that user. If a particular cached version doesn't exist yet, I'll let the life-cycle to complete and then I'll save it.

What I want is to manage different versions of a page and to determine whether a version or another should be served.


You should be able to use VaryByCustom for this, and just let ASP.NET worry about pulling the correct version of the page from the cache etc.

In the page itself...

<%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="LoggedInUser" %>

And in your Global.asax file...

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "LoggedInUser")
    {
        if (UserIsLoggedIn())
        {
            return "LoggedInUser:" + GetUserNameOrSomeOtherUniquePerUserString();
        }
        else
        {
            return "LoggedInUser:NONE";
        }
    }
    return base.GetVaryByCustomString(context, custom);
}
0

精彩评论

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

关注公众号