【问题标题】:How to implement VaryByCustom caching?如何实现 VaryByCustom 缓存?
【发布时间】:2011-07-15 21:30:05
【问题描述】:

我正在尝试实现根据主机缓存某些页面的功能。这是因为我可以拥有具有相同参数的页面的多个版本,并且请求方面的唯一区别是被请求的主机。

因此,例如这两个 URL 将请求同一个页面,但它们的样式不同:

http://www.a.com/something/specific

http://www.b.com/something/specific

我正在浏览此处列出的示例:

http://msdn.microsoft.com/en-us/library/5ecf4420%28v=VS.90%29.aspx

但这对我来说没有意义。

我已将此添加到我的 global.asax:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg == "host")
    {
        return "host=" + context.Request.Url.Host;
    }

    return base.GetVaryByCustomString(context, arg);
}

示例说明“要以编程方式设置自定义字符串,请调用 SetVaryByCustom 方法并将自定义字符串传递给它以使用”,代码类似于以下内容:

Response.Cache.SetVaryByCustom("host");

问题是我不知道该怎么办。我已将上一行添加到 MvcApplication_EndRequest,因为它似乎有道理,但我认为这是不对的,因为当我在 GetVaryByCustomString 中设置断点时,它们永远不会被命中。

有人可以告诉我我在这里缺少什么吗?或者如果我需要以不同的方式执行此操作?

编辑: RE Darin 在下面的回答,我已经用以下方式装饰了我的动作:

[CustomOutputCache(CacheProfile = "FundScreener")] // or similar depending on the action

其中CustomOutputCacheAttribute 定义为:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CustomOutputCacheAttribute: OutputCacheAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        AddLabelFilesDependency(filterContext);
        base.OnResultExecuted(filterContext);
    }

    private static void AddLabelFilesDependency(ControllerContext filterContext)
    {
        IConfigurationManager configurationManager = ObjectFactory.TryGetInstance<IConfigurationManager>();
        if (configurationManager == null 
            || filterContext == null 
            || filterContext.RequestContext == null
            || filterContext.RequestContext.HttpContext == null
            || filterContext.RequestContext.HttpContext.Response == null
            )
        {
            return;
        }
        string[] files = Directory.GetFiles(configurationManager.LabelsDirectoryPath, "*.xml");
        foreach(var file in files)
        {
            filterContext.RequestContext.HttpContext.Response.AddFileDependency(file);
        }
    }
}

配置文件定义为:

<add name="FundScreener"
     location="Server"
     enabled="true"
     varyByParam="*"
     duration="1200"
     sqlDependency="mmftms:offline.ScreenerData"/>

我需要改变这个吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc caching varybyparam


    【解决方案1】:

    你不需要在 MVC 中调用SetVaryByCustom。您可以使用 OutputCache 属性。查看following blog post

    【讨论】:

    • ...实际上,当我进一步阅读时,我可以看到该示例包括varybycustom="IsLoggedIn"。这可能是我需要的! -- 谢谢
    • 两个 URL(实际帖子中的一个和 cmets 中的一个)都不起作用。
    • 链接已损坏。
    • 链接到 URL 已损坏。答案中应该包含信息。
    【解决方案2】:

    如果你想为不同的主机设置不同的缓存,你可以使用:

    VaryByHeader="主机"

    因为,这将使它使用请求中标头“host”的值来改变缓存。您可以在控制器/操作的 OutputCache 指令中添加它,或者您可以在 web.config 中全局指定它。

    如果您使用主机绑定,主机标头将始终存在,您似乎就是这种情况。

    【讨论】:

      【解决方案3】:

      GetVaryByCustomString(...) 由每个请求的缓存层调用,您有机会检查请求和传入的参数来决定如何“分类”这个请求。因此,如果您将VaryByCustom 属性/属性设置为“主机”,那么您将在返回主机的GetVaryByCustomString 函数中编写代码(如上面的示例)。如果缓存层发现它已经用你返回的值缓存了参数“host”,那么它将返回缓存的响应,否则它执行请求并将其添加到缓存中。

      【讨论】:

        【解决方案4】:

        根据您的编辑,将VaryByCustom="host" 添加到您的 FundScreener 输出缓存配置文件中。

        【讨论】:

          猜你喜欢
          • 2012-10-14
          • 1970-01-01
          • 2013-10-12
          • 1970-01-01
          • 1970-01-01
          • 2019-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多