多租户应用程序的输出缓存,因主机名和区域性而异 [英] Output cache for multi-tenant application, varying by hostname and culture

查看:78
本文介绍了多租户应用程序的输出缓存,因主机名和区域性而异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC中有一个多租户应用程序.将要提供服务的应用程序实例仅是 hostname 的功能(我想应该是stackexchange的东西).

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. 如果输出完全取决于主机名(即,忽略本地化要求)?

  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)?

与(1)一样,但是现在考虑到输出也取决于区域性设置?

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

与(2)一样,但是考虑到输出可能随传递给操作的参数而有所不同?

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

在这种情况下,我正在考虑所有站点都从单个IIS网站运行.

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

推荐答案

我刚刚想出了实现方法.

I've just figured out how to achieve this.

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

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

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

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() { /* ... */ }
}

方法2.

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

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>

然后使用它:

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

方法3.

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

Method 3.

Or you can subclass the OutputCacheAttribute and use it:

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

然后使用它:

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

这篇关于多租户应用程序的输出缓存,因主机名和区域性而异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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