多租户应用程序的输出缓存,因主机名和文化而异

发布于 2024-08-17 03:14:00 字数 522 浏览 7 评论 0原文

我在 ASP.NET MVC 中有一个多租户应用程序。将提供服务的应用程序实例仅是主机名的函数(我想类似于stackexchange)。

应用程序的每个实例可能具有不同的区域性设置(甚至是“自动”,以读取浏览器的语言并尝试使用它),并将相应地本地化

在这种情况下,我想对我的某些操作进行一些输出缓存。所以,我的问题是:

  1. 如果输出完全依赖于主机名,多租户 ASP.NET MVC 应用程序实现输出缓存的可能性是什么? (即忽略本地化要求)?

  2. 与 (1) 相同,但现在考虑到输出也取决于区域性设置

  3. 与 (2) 相同,但考虑到输出可能会随传递给操作的参数而变化?

在本例中,我考虑所有站点都从单个 IIS 网站运行。

I have a multi-tenant application in ASP.NET MVC. The instance of the application that will be served is function of the hostname alone (something along the lines of stackexchange, I suppose).

Each instance of the application might have a different culture setting (even "auto", to read the browser's language and try to use it), and will be localized accordingly.

In this situation, I'd like to do some output caching on some of my actions. So, my questions are:

  1. What are the possibilities to achieve output caching of a multi-tenant ASP.NET MVC application, if the output depends exclusively on the hostname (ie, ignoring the localization requirement)?

  2. Same as (1), but now considering that the output depends on the culture settings as well?

  3. Same as (2), but considering that the output might vary with parameters that were passed to the action?

In this case, I'm considering that all the sites run from a single IIS website.

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

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

发布评论

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

评论(2

左秋 2024-08-24 03:14:00

我刚刚想出了如何实现这一目标。

只需使用 VaryByHeader 属性,并将其设置为 "host"。这样做的可能性有很多。

方法 1

使用 OutputCacheAttribute 传递所有需要的配置元素,包括 VaryByHeader

public class HomeController : Controller
{  
    [OutputCache(Duration = 3600, VaryByParam = "none", VaryByHeader = "host")]
    public ActionResult Index() { /* ... */ }
}

方法 2。

或者您可以将其设置为 Web.config 上的配置文件:

<?xml version="1.0"?>
<configuration>
  <!-- ... -->
  <system.web>
    <!-- ... -->
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear/>
          <add name="Multitenant" 
               enabled="true"
               duration="3600"
               varyByHeader="host"
               varyByParam="none"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
</configuration>

然后使用它:

public class HomeController : Controller
{  
    [OutputCache(CacheProfile = "Multitenant")]
    public ActionResult Index() { /* ... */ }
}

方法 3。

或者您可以子类化 OutputCacheAttribute 并使用它:

public sealed class MultitenantOutputCacheAttribute : OutputCacheAttribute
{
    public MultitenantOutputCacheAttribute()
    {
        VaryByHeader = "host";
        VaryByParam = "none";
        Duration = 3600;
    }
}

然后使用它:

public class HomeController : Controller
{  
    [MultitenantOutputCache]
    public ActionResult Index() { /* ... */ }
}

I've just figured out how to achieve this.

Simply use the VaryByHeader property, set to "host". There are many possibilities to do so.

Method 1

Use the OutputCacheAttribute passing all the needed configuration elements, including VaryByHeader:

public class HomeController : Controller
{  
    [OutputCache(Duration = 3600, VaryByParam = "none", VaryByHeader = "host")]
    public ActionResult Index() { /* ... */ }
}

Method 2.

Or you could set it to a profile on the Web.config:

<?xml version="1.0"?>
<configuration>
  <!-- ... -->
  <system.web>
    <!-- ... -->
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear/>
          <add name="Multitenant" 
               enabled="true"
               duration="3600"
               varyByHeader="host"
               varyByParam="none"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
</configuration>

Then use it:

public class HomeController : Controller
{  
    [OutputCache(CacheProfile = "Multitenant")]
    public ActionResult Index() { /* ... */ }
}

Method 3.

Or you can subclass the OutputCacheAttribute and use it:

public sealed class MultitenantOutputCacheAttribute : OutputCacheAttribute
{
    public MultitenantOutputCacheAttribute()
    {
        VaryByHeader = "host";
        VaryByParam = "none";
        Duration = 3600;
    }
}

Then use it:

public class HomeController : Controller
{  
    [MultitenantOutputCache]
    public ActionResult Index() { /* ... */ }
}
反目相谮 2024-08-24 03:14:00

如果人们登陆此页面并正在寻找 asp.net 2.x 中的等效内容。该属性将如下所示:

[ResponseCache(Duration = 30, Location = ResponseCacheLocation.Any, VaryByHeader = "host", VaryByQueryKeys = new string[] { "*" })]

您将需要添加中间件。您需要 此 nuget 包 和此代码:

public void ConfigureServices(IServiceCollection services)
{
    //stuff before...

    services.AddResponseCaching();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    //stuff after...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //stuff before...

    app.UseResponseCaching();

    //stuff after...
}

更多详细信息请参见此处

In case people land on this page and are looking for the equivalent in asp.net 2.x. The attribute will look like this:

[ResponseCache(Duration = 30, Location = ResponseCacheLocation.Any, VaryByHeader = "host", VaryByQueryKeys = new string[] { "*" })]

And you will need the middleware added. You need this nuget package and this code:

public void ConfigureServices(IServiceCollection services)
{
    //stuff before...

    services.AddResponseCaching();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    //stuff after...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //stuff before...

    app.UseResponseCaching();

    //stuff after...
}

More details here

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