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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.
Note that the
OutputCacheAttributestill 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.When you call a Controller Action that has an
OutputCacheAttribute, a cache key is generated based on your request; for instance, if you have someVaryByParamdesignations, cache keys can differ for each user. Then the response your Action returns is stored in the cache under that key.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:
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:
Notice the inclusion of
VaryByCustom = "Host", which is then seen by your overriden OutputCacheAttribute’sGetVaryByCustomString()method and thus included in the cache key that is used.