在这种情况下我该如何使用outputcache?

发布于 2024-10-02 18:26:01 字数 142 浏览 1 评论 0原文

在我的应用程序中,我会记住用户在会话中选择的语言。 问题是,如果我使用输出缓存,那么更改语言功能将不起作用,因为当我根据 Session["lang"] 值从数据库检索时,它会缓存结果。

那么我是否有另一种方法来使用缓存功能?或者我怎样才能减少响应时间?

In my application, I will remember user's language choice in session.
The problem is if I use output cache, then the change language function will not work, because it caches the result when I retrieve from database according to the Session["lang"] value.

So if I have another method to use cache function ? Or how can I decrease the response time ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2024-10-09 18:26:01

输出缓存基础结构的一部分是 VaryBy 机制,它是一种指示 ASP.NET 保持同一页面的并行缓存因某些数据(如查询字符串)而变化的方法。在这种情况下,VaryByCustom 机制可能是最容易实现的。 这是一篇简短的文章,其中有一个很好的示例。

首先,缓存属性:

[OutputCache(CacheProfile = "CachedPage")]
public ActionResult Index()
{
   return View();
}

缓存配置文件:

<caching>
    <outputcachesettings>             
        <outputcacheprofiles> 
            <add varybycustom="Language"
                 varybyparam="*"
                 duration="86400"
                 name="CachedPage" />
        </outputcacheprofiles> 
    </outputcachesettings> 
</caching>

最后,global.asax.cs 中的自定义逻辑:

public override string GetVaryByCustomString(
    HttpContext context,
    string arg)
{
    if (arg == "Language")
    {
         return Session["lang"].ToString();
    }
    else
    {
        return base.GetVaryByCustomString(context, arg);
    }
}

现在,对于 Session["lang"] 返回的每个可能的唯一值,ASP .NET 将保留在该参数下执行的页面的缓存副本。

Part of the Output Caching infrastructure is the VaryBy mechanism, which is a way to instruct ASP.NET to keep parallel caches of the same page varied by some piece of data, like a querystring. In this case, the VaryByCustom mechanism may be the simplest to implement. Here's a short article with a good example.

First, the caching attribute:

[OutputCache(CacheProfile = "CachedPage")]
public ActionResult Index()
{
   return View();
}

The cache profile:

<caching>
    <outputcachesettings>             
        <outputcacheprofiles> 
            <add varybycustom="Language"
                 varybyparam="*"
                 duration="86400"
                 name="CachedPage" />
        </outputcacheprofiles> 
    </outputcachesettings> 
</caching>

And finally, the custom logic in global.asax.cs:

public override string GetVaryByCustomString(
    HttpContext context,
    string arg)
{
    if (arg == "Language")
    {
         return Session["lang"].ToString();
    }
    else
    {
        return base.GetVaryByCustomString(context, arg);
    }
}

Now for each possible unique value that Session["lang"] returns, ASP.NET will keep a cached copy of the page which executed under that parameter.

绝影如岚 2024-10-09 18:26:01

将此值保存在cookie中,会话在GetVaryByCustomString中不可用

Save this value in cookies, session are not available in GetVaryByCustomString

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文