的OutputCache服务器 [英] OutputCache on server

查看:184
本文介绍了的OutputCache服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义的OutputCache属性,对我的控制器动作前,使服务器能够快速地给出相同的回应不同的用户。卜它缓存在全部页。我指的是母版页也被缓存如果我缓存,返回一个查看一个动作()。因此,在主页面顶部的用户帐户信息得到由每个用户共享。我想只缓存内容的网页,而不是母版页,_Layout.cshtml。我怎样才能排除?

I define OutputCache attributes on top of my controller actions, so that the server can give the same response quickly to different users. Bu it caches the whole page. I mean the master page is also cached if I have cached an action that returns a View(). So the user account information on top of the master page gets shared by every user. I want to cache only the content page, not the master page, _Layout.cshtml. How can I exclude that?

编辑:
在那里我有麻烦的部分是这样的:

The part where I'm having trouble is this:

@if(Request.IsAuthenticated) {
    <text>Hello <strong>@User.Identity.Name</strong>!</text>
    @: | 
    @Html.ActionLink("Index", "Index", "Account")
    @: |
    @Html.ActionLink("Logout", "Logout", "Account")
}
else
{
    @:|
   @Html.ActionLink("Login", "Login", "Account")
}

当我缓存控制器动作,返回的视图中也携带该用户登陆部分从缓存中,所以它给出错误的礼炮几乎每一个用户。如何动态生成这部分,即使页面缓存?

When I cache a controller action, the returned view also carries this userlogin part from the cache, so it gives wrong salutes to almost every user. How can I dynamically generate this part even if the page is cached?

推荐答案

VaryByCustom是是你想要的。

VaryByCustom is what you want.

将这个:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    return "User".Equals(custom, StringComparison.OrdinalIgnoreCase)
        ? User.Identity.Name
        : base.GetVaryByCustomString(context, custom);
}

...然后使用 [的OutputCache(VaryByCustom是=用户)] 属性。

这仍然会导致整个页面单独缓存,但一个单独的高速缓存将针对每个用户创建

This will still cause the whole page to be cached separately, but a separate cache will be created for each user.

如果您正在寻找其他选项,搜索MVC甜甜圈缓存或MVC甜甜圈洞缓存。

If you are looking for other options, search for MVC donut caching or MVC donut hole caching.

回复评论

听起来像是你想甜甜圈洞缓存。看看这个答案帮助你。

Sounds like you want donut hole caching. See if this answer helps you.

这篇关于的OutputCache服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆