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

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

问题描述

我在 ASP.NET MVC 中有一个 多租户应用程序.将提供服务的应用程序实例是 主机名 单独的函数(我想是类似 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. 如果输出完全依赖于主机名(即,多租户 ASP.NET MVC 应用程序的实现输出缓存的可能性有哪些?忽略本地化要求)?

  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 并使用它:

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

然后使用它:

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

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

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