![]() _varyByParamsSplitCache = varyByParamsSplit įoreach (var varyByParam in varyByParamsSplit) Public int SlidingExpiration, StringSplitOptions.RemoveEmptyEntries) / The comma separated parameters to vary the caching by. Private static readonly Memor圜ache _cache = new Memor圜ache("ActionResultCacheAttribute") Private static readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim() Private static readonly Dictionary _varyByParamsSplitCache = new Dictionary() Public class ActionResultCacheAttribute : ActionFilterAttribute / Caches the result of an action method. ![]() This allows for things like SessionState or ViewData to vary per cached request! It’s not quite as efficient as the true OutputCacheAttribute, but my custom ActionResultCacheAttribute is an excellent tradeoff between performance and dynamic data: /// By executing within the MVC pipeline, this custom caching attribute will not interrupt or short-circuit the MVC pipeline. This attribute can cache the ActionResult instead of the raw HTML of the page, and in doing so will allow you to cache all of the work that is done to generate the ActionResult (be it ViewResult or otherwise). It’s scary stuff, nonetheless.Ī great way to balance the benefits of output caching with the dynamic content and features that the modern ASP.NET MVC web application offers is to create a custom caching attribute. I think that most of us have seen this kind of (often humorous) caching error before. And if David fills out your empty form and presses submit, only to cache the resulting input validation error page, then everybody will see David’s completed form when they have errors too – maybe even including sensitive data like his username, password, or even his credit card information. When David accesses the logged in page for the first time and caches it, everybody else who logs in will be called David on the page. This means that if your view has dynamic content that comes from session or ViewData, such as displaying the currently logged in user’s name in the top bar, or the current time of day, or the resulting view of an invalid form post which tells your user to correct their input errors, you’ll quickly discover the error of your ways when you try to cache that page. ![]() ASP.NET CORE PRIVATE CACHE CODEBecause it caches the entire page as raw HTML, it circumvents a large part of the MVC pipeline and thus also skips the code that runs to generate the page. The OutputCacheAttribute is a really powerful way to improve performance in your MVC application, but isn’t always the most practical. This is the essence of caching: trading memory for performance. This cached view page is then available to the application for all subsequent requests (or until the item expires out of the cache), which can retrieve it from the memory rather than redoing the work to re-create the result again. For example, if your action method renders a view, then the view page will be cached in memory. By adding this attribute to a controller or action method, the output of the method(s) will be stored in memory. If you’re working on an application built using ASP.NET MVC, you’re hopefully aware of the OutputCacheAttribute attribute which can be used to statically cache your dynamic web pages. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |