Interfaces

Project

OutputCache

NuGet packageOwin.Framework.OutputCache
GitHub sourceOwinFramework.OutputCache

Home |  Readme

Receives hints from downstream middleware about the volatility of generated output, and uses rules based configuration to decide what to cache and for how long

This middleware will improve the performance of your web site by capturing the output from downstream middleware and saving it in the ICache facility, then using the cached response when another identical request is received.

The output is only cached when dwnstream middleware indicates that it is valid to cache the response. Other Middleware (like the static file middleware) will communicate with the output cache when installed to tell it when the response can be cached.

Downstream middleware can set a category and a priority in the output cache for each request. The output cache can be configured with different cache behavours for all combinations of these two values.

Adding this middleware to the Owin pipeline

 builder.Register(ninject.Get<OwinFramework.OutputCache.OutputCacheMiddleware>())
    .As("Output cache")
    .ConfigureWith(config, "/owinFramework/middleware/outputCache");    
The assumes that you are using Ninject as your IoC container, and followed the getting started walkthrough. If this is not the case then you will need to adjust the code to work in your application.

Default Configuration

The configuration below is the configuration you will get by default if you do not provide a configuration for this middleware.

{
   "owinFramework": {
      "middleware": {
         "outputCache": {
            "documentationRootUrl": "/outputCache",
            "rules": null
         }
      }
   }
}
Note that this default configuration means that nothing will be cached. In order to have some output cached you need to add some rules.

Cache rules

The output cache middleware must have rules defined before it will cache anything. The rules define what kind of caching to apply to each type of content that can be requested from your website.

When downstream middleware indicates that caching is possible on the content that it is producing, it passes back a "Category" and "Priority" to the output cache. In the output cache rules you can match on one, both or neither of these and define the caching behavior.

Below is an example configuration that provides some caching:

{
   "owinFramework": {
      "middleware": {
         "outputCache": {
            "documentationRootUrl": "/outputCache",
            "rules": [
                {
                    "category": "UserProfile",
                    "priority": "High",
                    "cacheCategory": "Shared",
                    "serverCacheTime": "00:30:00",
                    "browserCacheTime": "00:10:00"
                },
                {
                    "category": "Product",
                    "priority": "Always",
                    "cacheCategory": "Local",
                    "serverCacheTime": "01:00:00",
                    "browserCacheTime": "00:30:00"
                },
                {
                    "category": "Product",
                    "priority": "Medium",
                    "cacheCategory": "Local",
                    "serverCacheTime": "00:10:00",
                    "browserCacheTime": "2"
                }
            ]
         }
      }
   }
}
Note that the category and priority are used to figure out which rule to apply, then the cacheCategory is passed to the ICache implementation.
Note that the serverCacheTime can be zero to disable server side caching, and browserCacheTime can be set to zero to disable caching in the browser.
If you are configuring the browserCacheTime to a large value you should include some kind of cache buster in your URL or you will have no way to force the browser to retrieve a new version of the content after a production deployment.