How to implement VaryByCustom caching?

情到浓时终转凉″ 提交于 2019-12-04 23:19:06

You don't need to call SetVaryByCustom in MVC. You could use the OutputCache attribute. Checkout the following blog post.

If you want to have different cache for different hosts, you can use:

VaryByHeader="host"

Because, that would make it use the value of header "host" in the request to vary the cache. You can add this in the OutputCache directive on your controllers/actions, or you can specify it globally in your web.config probably.

a host header will always be present if you use host-bindings, which seems to be the case for you.

GetVaryByCustomString(...) is called by the caching layer per request and you have an opportunity to inspect the request and the passed-in argument to decide how to "categorize" this request. So if you set the VaryByCustom property/attribute to "host", you would then write code inside GetVaryByCustomString function which returns the host (as in your example, above). If the caching layer finds that it has already cached the argument "host" with the value you've returned then it will return the cached response, otherwise it executes the request and adds it to the cache.

Based on your edit, add VaryByCustom="host" to your FundScreener output cache profile.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!