
This is the fourteenth article in a
series of articles about the emerging VS 2010 and .NET 4. Today's article will tell us about the improvements in the caching mechanism that made in ASP.NET 4. Innovations can be used in all ASP.NET 4 applications, including applications developed in ASP .NET Web Forms and ASP.NET MVC.
Caching mechanism today
ASP.NET 1.0 introduced the concept of results caching, which allowed developers to capture and save the generated pages, controls, controllers and HTTP responses in the cache memory. During all further web requests, ASP.NET can deliver the content faster by returning the finished data from the cache without starting the page or controller to generate the result. This can significantly increase the performance of your applications, especially in cases where your data is stored in a database (after caching, you no longer need to perform queries to the database every time you access the page).
The caching mechanism in ASP.NET is flexible enough to store heterogeneous data, depending on the GET / POST parameters passed to the page or controller, for example: Browse.aspx? Category = Beverages and Browse.aspx? Category = Meat - different cache data. It also allows you to cache different versions of the results based on the type of browser and the user's language settings. This allows you to cache the mobile version of the page separately from the version for the desktop browser, as well as the variation of data in the cache, based on the user's preferences to read the page in English or French. You can configure ASP.NET to cache individual parts for a specific period of time. For example, for 1 minute, after which the query regenerates the result and caches it again. You can also force ASP.NET to dynamically invalidate a portion of the cache on an external event, for example, if the data on which the cache is based has changed in the database.
Prior to ASP.NET 4, there was one limitation in the caching mechanism, which was that the cache could not expand itself, and the cached content should always be stored in memory.
')
Extensibility of the caching mechanism in ASP.NET 4.
ASP.NET 4 adds extensibility to the caching mechanism, which allows developers to configure one or more proprietary cache providers. Cache providers can use different data storage mechanisms. This allows you to create cache providers that store data in the cache using any storage mechanism, including local or remote disks, databases, cloud storages, and common cache engines (memcached or velocity).
You can create your cache provider by adding a class that inherits from the new class System.Web.Caching.OutputCacheProvider, which appeared in ASP.NET 4. In the declared class, you need to overload 4 public methods that are responsible for the implementation of add / delete / change / return the cached content (a key is used to find any cache item). You can configure ASP.NET 4 to use its own cache provider by registering it with web.config in the new <outputCache> <providers> subsection:

Above, I added a new cache provider, which was called “SampleCache”, it is implemented using the “ScottOutputCache” class in the OutputCacheSample.dll assembly. I also asked ASP.NET to use “SampleCache” as the default caching mechanism, specifying the “defaultProvider” attribute in the <outputCache> element:
And now, when I add the OutputCache directive to the header of the .aspx file, the page content will be cached and saved using my ScottOutputCache provider:

Moreover, if I add the [OutputCache] attribute to any action method in the ASP.NET MVC controller, the content will also be cached and saved using the ScottOutputCache provider:

Which cache provider will be used
Before that, I configured ASP.NET so that by default it always uses my “SampleCache” provider, wherever application caching is used.
In addition, developers can customize the dynamic selection of the cache provider for each request. This is useful for scenarios where you want a richer set of cache semantics. For example, you want to cache the page “Home” or “Top 10” on the site, which uses the in-memory provider that is built into ASP.NET, which is super-fast by storing data in RAM, and caching less-requested pages to disk.
You can dynamically determine which cache provider to use for each request by overloading the GetOutputCacheProviderName () method in the Global.asax file. Below, I indicate for "Home.aspx" to use the ASP.NET in-memory cache provider, regardless of the cache provider set in the web.config file:

This feature allows you to switch between providers depending on the need and allows you to implement powerful scripts.
Common Cache Providers
We will provide examples that demonstrate how to implement a cache provider that uses cached data storage in the file system. We will also provide examples that show how to integrate caching with the new Windows Server AppFabric Caching Service, known as “Velocity”. The AppFabric caching service will be a free, fully supported cache system from Microsoft. ASP.NET 4 makes it easy to use the memcached extension mechanism, a popular open source cache system.
You can familiarize yourself with the principles of creating providers by viewing the video of Stefan Shakou with PDC -
ASP.NET 4 Core Runtime . And also will get acquainted with AppFabric Cache service on
PDC 2009 .
Results
The addition of the ability to extend the caching mechanism in ASP.NET 4 allows developers to implement more aggressive and smarter caching strategies for websites and applications. This can significantly improve the performance and responsiveness of the application, and reduce the amount of resources required for the server.