Question about settings of IIS7. How I can do one cache for each of bindings? For example, I have one site and three bindings for the site. I need to create three different caches for each of three bindings.
关于IIS7设置的问题。如何为每个绑定执行一个缓存?例如,我有一个站点和三个站点绑定。我需要为三个绑定分别创建三个不同的缓存。
2 个解决方案
#1
0
usr's answer is correct if you are manually reading from and writing to the cache. You have less direct control over the cache key with the OutputCacheAttribute, however.
如果您正在手动地从缓存中读取和写入,usr的答案是正确的。但是,使用OutputCacheAttribute可以更少地直接控制缓存键。
Note that the OutputCacheAttribute
still relies on cache keys in its implementation. In ASP.NET, each item that is cached is assigned a key through which it is looked up.
注意,OutputCacheAttribute在实现中仍然依赖于缓存键。在ASP。NET,缓存的每个条目都被分配一个键,通过这个键查找。
When you call a Controller Action that has an OutputCacheAttribute
, a cache key is generated based on your request; for instance, if you have some VaryByParam
designations, cache keys can differ for each user. Then the response your Action returns is stored in the cache under that key.
当您调用具有OutputCacheAttribute的控制器操作时,会根据您的请求生成缓存键;例如,如果您有一些VaryByParam指定,缓存键可以根据每个用户而有所不同。然后,操作返回的响应将存储在该键下的缓存中。
When the next request comes in, the cache key is generated and we check in the cache to see if there is already something cached under that key. If so, we just return that; otherwise, we continue with the Action.
当下一个请求进入时,将生成缓存键,我们将检查缓存,看看是否已经在该键下缓存了一些内容。如果是,我们就返回;否则,我们继续行动。
We can have a different cache for each binding by including the host name in the cache key. If you're using OutputCacheAttribute, you can override it to allow varying the cache by host:
我们可以为每个绑定提供不同的缓存,包括在缓存键中包含主机名。如果您正在使用OutputCacheAttribute,您可以重写它以允许按主机更改缓存:
public override string GetVaryByCustomString(HttpContext context, string customVary)
{
if(customVary == "Host")
{
return context.Request.Url.Host;
}
// other behaviors here if necessary
return "";
}
This will allow the cache key to be dynamically modified to include the host name through which the site is accessed. This means that if you have three different bindings, you will have three different cache keys (assuming no other varying parameters).
这将允许动态修改缓存键,以包含访问站点的主机名。这意味着如果您有三个不同的绑定,您将有三个不同的缓存键(假设没有其他变化的参数)。
Here's how to modify your Controller Action:
以下是如何修改你的控制器动作:
[MyOutputCache(VaryByParam = "None", VaryByCustom = "Host", Duration = 7200)]
public ActionResult Index()
{
// ...
return View();
}
Notice the inclusion of VaryByCustom = "Host"
, which is then seen by your overriden OutputCacheAttribute's GetVaryByCustomString()
method and thus included in the cache key that is used.
注意,包含VaryByCustom = "Host",然后由overriden OutputCacheAttribute的GetVaryByCustomString()方法查看,因此包含在使用的缓存键中。
#2
1
Use Request.Host
as part of the cache key.
使用请求。主机作为缓存键的一部分。
#1
0
usr's answer is correct if you are manually reading from and writing to the cache. You have less direct control over the cache key with the OutputCacheAttribute, however.
如果您正在手动地从缓存中读取和写入,usr的答案是正确的。但是,使用OutputCacheAttribute可以更少地直接控制缓存键。
Note that the OutputCacheAttribute
still relies on cache keys in its implementation. In ASP.NET, each item that is cached is assigned a key through which it is looked up.
注意,OutputCacheAttribute在实现中仍然依赖于缓存键。在ASP。NET,缓存的每个条目都被分配一个键,通过这个键查找。
When you call a Controller Action that has an OutputCacheAttribute
, a cache key is generated based on your request; for instance, if you have some VaryByParam
designations, cache keys can differ for each user. Then the response your Action returns is stored in the cache under that key.
当您调用具有OutputCacheAttribute的控制器操作时,会根据您的请求生成缓存键;例如,如果您有一些VaryByParam指定,缓存键可以根据每个用户而有所不同。然后,操作返回的响应将存储在该键下的缓存中。
When the next request comes in, the cache key is generated and we check in the cache to see if there is already something cached under that key. If so, we just return that; otherwise, we continue with the Action.
当下一个请求进入时,将生成缓存键,我们将检查缓存,看看是否已经在该键下缓存了一些内容。如果是,我们就返回;否则,我们继续行动。
We can have a different cache for each binding by including the host name in the cache key. If you're using OutputCacheAttribute, you can override it to allow varying the cache by host:
我们可以为每个绑定提供不同的缓存,包括在缓存键中包含主机名。如果您正在使用OutputCacheAttribute,您可以重写它以允许按主机更改缓存:
public override string GetVaryByCustomString(HttpContext context, string customVary)
{
if(customVary == "Host")
{
return context.Request.Url.Host;
}
// other behaviors here if necessary
return "";
}
This will allow the cache key to be dynamically modified to include the host name through which the site is accessed. This means that if you have three different bindings, you will have three different cache keys (assuming no other varying parameters).
这将允许动态修改缓存键,以包含访问站点的主机名。这意味着如果您有三个不同的绑定,您将有三个不同的缓存键(假设没有其他变化的参数)。
Here's how to modify your Controller Action:
以下是如何修改你的控制器动作:
[MyOutputCache(VaryByParam = "None", VaryByCustom = "Host", Duration = 7200)]
public ActionResult Index()
{
// ...
return View();
}
Notice the inclusion of VaryByCustom = "Host"
, which is then seen by your overriden OutputCacheAttribute's GetVaryByCustomString()
method and thus included in the cache key that is used.
注意,包含VaryByCustom = "Host",然后由overriden OutputCacheAttribute的GetVaryByCustomString()方法查看,因此包含在使用的缓存键中。
#2
1
Use Request.Host
as part of the cache key.
使用请求。主机作为缓存键的一部分。