Extensions to the Standard Caching Engine in ASP.NET 4

Original author: Scott Gu
  • Transfer
imageThis is the fourteenth article in a series of materials on the upcoming VS 2010 and .NET 4. Today's article will tell us about the improvements to the caching mechanism that were made in ASP.NET 4. Innovations can be used in all ASP.NET 4 applications, including applications developed on ASP .NET Web Forms and ASP.NET MVC.

Caching mechanism today


ASP.NET 1.0 introduced the concept of caching results, which allowed developers to capture and store generated pages, controls, controllers, and HTTP responses in the cache memory. During all further web requests, ASP.NET can deliver content faster by returning the finished data from the cache without launching a page or controller to generate a result. This can significantly increase the performance of your applications, especially in cases where your data is stored in a database (after caching, you will no longer need to perform database queries 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 language settings of the user. This allows you to cache the mobile version of the page separately from the desktop browser version, as well as varying the cache data based on the user's preferences for reading 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 will regenerate the result and cache it again. You can also force ASP.

Prior to ASP.NET 4, there was one limitation in the caching mechanism, which was that the cache could not expand itself, and 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 native cache providers. Cache providers can use various 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 storage and common cache engines (memcached or velocity).

You can create your cache provider by adding a class that inherits from the new System.Web.Caching.OutputCacheProvider class, which appeared in ASP.NET 4. In the declared class, you need to overload 4 public methods that are responsible for the implementation of adding / removing / changing / return cached content (a key is used to find any cache element). You can configure ASP.NET 4 to use your own cache provider by registering it in web.config in a new subsections :

image

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 by specifying the “defaultProvider” attribute in the element:

And now, when I add the OutputCache directive to the header of the .aspx file, the contents of the page will be cached and saved using my ScottOutputCache provider:

image

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

image

Which cache provider will be used


Before that, I configured ASP.NET so that by default it always used my SampleCache provider, wherever caching was used in the application.

In addition, developers can set up a dynamic cache provider for each request. This is convenient for scenarios where you want to have a richer set of cache semantics. For example, you want to cache the Home or Top 10 page on the website, which uses the ASP.NET in-memory provider built-in, which is super-fast by storing data in RAM, and cache less requested pages on disk.

You can dynamically determine which cache provider to use for each request by overriding the GetOutputCacheProviderName () method in the Global.asax file. Below, I indicate for “Home.aspx” to use the built-in in-memory ASP.NET cache provider, regardless of the installed cache provider in the web.config file:

image

This feature allows you to switch between providers depending on your needs and allows you to implement powerful scripts .

Common Cache Providers


We will provide examples that demonstrate how to implement a cache provider that uses the storage of cached data in the file system. We also provide examples that show how to integrate caching with the new Windows Server AppFabric Caching Service, known as Velocity. AppFabric Caching Service will be a free, fully supported cache system from Microsoft. In ASP.NET 4, it will be easy to use the extension mechanism for memcached, the popular open source cache system.

You can familiarize yourself with the principles of creating providers by watching Stefan Shakow’s video with PDC - ASP.NET 4 Core Runtime . And also get acquainted with AppFabric Cache service at PDC 2009 .

Summary


Adding 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 application performance and response, and reduce the amount of resources required for the server.

Also popular now: