|
| 1 | +# ASP.NET MVC Extensible Donut Caching # |
| 2 | + |
| 3 | +ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code allows you to cache all of your page apart from one or more Html.Actions which can be executed every request. Perfect for user specific content. |
| 4 | + |
| 5 | +## Download ## |
| 6 | + |
| 7 | +The best way to add donut caching to your MVC project is to use the NuGet package. From within Visual Studio, select *Tools | Library Package Manager* and then choose either Package Manager Console or Manage NuGet Packages. Via the console, just type **install-package MvcDonutCaching** and hit return. From the GUI, just search for **MvcDonutCaching** and click the install button. |
| 8 | + |
| 9 | +## Usage ## |
| 10 | + |
| 11 | +The package adds several overloads to the built-in Html.Action HTML helper. The extra parameter in each overload is named *excludeFromParentCache*. Set this to true for any action that should not be cached, or should have a different cache duration from the rest of the page. |
| 12 | + |
| 13 | +```csharp |
| 14 | +@Html.Action("Login", "Account", true) |
| 15 | +``` |
| 16 | + |
| 17 | +The package also include a DonutOutputCacheAttribute to be used in place of the built-in OutputCacheAttribute. This attribute is typically placed on every controller action that needs be be cached. |
| 18 | + |
| 19 | +You can either specify a fixed duration: |
| 20 | + |
| 21 | +```csharp |
| 22 | +[DonutOutputCache(Duration = "300")] |
| 23 | +public ActionResult Index() |
| 24 | +{ |
| 25 | + return View(); |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Or, use a cache profile: |
| 30 | + |
| 31 | +```csharp |
| 32 | +[DonutOutputCache(CacheProfile = "FiveMins")] |
| 33 | +public ActionResult Index() |
| 34 | +{ |
| 35 | + return View(); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +If you are using cache profiles, be sure to configure the profiles in the web.config. Add the following within the system.web element: |
| 40 | + |
| 41 | +```xml |
| 42 | +<caching> |
| 43 | + <outputCacheSettings> |
| 44 | + <outputCacheProfiles> |
| 45 | + <add name="FiveMins" duration="300" varyByParam="*" /> |
| 46 | + </outputCacheProfiles> |
| 47 | + </outputCacheSettings> |
| 48 | +</caching> |
| 49 | +``` |
| 50 | + |
| 51 | +You can also configure the output cache to use a custom provider: |
| 52 | + |
| 53 | +```xml |
| 54 | +<caching> |
| 55 | + <outputCache defaultProvider="DistributedCacheProvider"> |
| 56 | + <providers> |
| 57 | + <add name="DistributedCacheProvider" type="DevTrends.Example.DistributedCacheProvider" /> |
| 58 | + </providers> |
| 59 | + </outputCache> |
| 60 | +</caching> |
| 61 | +``` |
| 62 | + |
| 63 | +Note, that a custom provider is not included with this project but you can write one fairly easily by subclassing *System.Web.Caching.OutputCacheProvider*. A number of implementations are also available on the web. |
| 64 | + |
| 65 | +## More Information ## |
| 66 | + |
| 67 | +A comprehensive guide to MVC Extensible Donut Caching is now available on the [DevTrends Blog](http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3). |
0 commit comments