asp.net 4 AppFabric output cache provider __.net

Source: Internet
Author: User
Tags sessions

Caching is where you save your data, making your application more accessible. Caching can be allocated to many machines according to your needs, and developers need not care about which machine the data is on. To get super fast performance, you can even sacrifice data consistency to replicate the cache to the calling client (local cache). The saved data can be anything that can be serialized. NET object.

The purpose of using caching is to make your application more performance and scalability. AppFabric supports both explicit and implicit caching usage to promote extensibility and performance. If you are a asp.net developer, implicit and explicit caching usage should be quite familiar to you. Explicit caching allows you to programmatically add, remove, and manage data items in the cache through the caching APIs provided by AppFabric. This is a very powerful tool to help you customize the caching strategies and usage patterns of your application data, but you need to understand the principles of data caching. Implicit caching uses a default caching strategy that works in common scenarios. Two examples of implicit caching include ASP.net session state and HTML page caching (that is, output caching).

The ASP.net provider model enables developers to choose from three session providers: InProc, StateServer, and SQL Server. The fourth session provider is technically feasible when using the AppFabric cache, but you should be careful not to confuse the session with the cache. Caching is used to improve performance, and sessions are used to bring applications to a certain state.

The AppFabric cache session provider for ASP.net uses its distributed cache (which may have high availability) as a repository for ASP.net sessions. This functionality is transparent and can be used without modifying existing code. With such a provider, the ASP.net session can remain available when the WEB server crashes or goes offline because the session is stored in the AppFabric cache.

It was redesigned in ASP.net 4.0 to provide a outputcacheprovider for developers to extend, but by default it still uses System.Web.Caching.Cache for caching. For the use of the output cache for ASP.net 4.0, refer to the extensible output cache in asp.net 4 of the ScottGu vs 2010 and. NET 4.0 series.

Asp. NET development team on the CodePlex released ASP.net 4 Providers for AppFabric Caching, which has two providers ASP.net session status and HTML page cache (that is, output cache): Sessions State: Microsoft.Web.DistributedCacheSessionStateStore Output Caching:Microsoft.Web.DistributedOutputCache

Currently, these two providers are not officially released, but are published in the community for testing and feedback, not directly in the production environment. However, AppFabric has brought a asp.net session state Provider (Windows Server AppFabric Caching), specific reference http://msdn.microsoft.com/ En-us/library/ee790859.aspx.

Use the following methods:

1. Added a configuration section about caching in the Web.config file of ASP.net 4.0, as follows

<caching>

<outputcache defaultprovider= "Appfabricoutputcache" >

<providers>

<add name= "Appfabricoutputcache" type= "Microsoft.Web.DistributedOutputCache, Microsoft.Web.DistributedCache" Hostname= "localhost" cacheport= "22233" cachename= "Session"/>

</providers>

</outputCache>

</caching>

2. Use Microsoft.Web.DistributedOutputCache

1 in the configuration file configuration, the custom implementation as the default output cache support, see the 1 configuration
2 Specify the name of the provider in the UserControl, and change the name to define it in Web.config, for example

<%@ control language= "C #" autoeventwireup= true "codebehind=" IamUserControl.ascx.cs "inherits=" Outputcachetest.iamusercontrol "%>

<%@ OutputCache duration= "3000" providername= "Aspnetinternalprovider" varybyparam= "None"%>

It should be noted that the name of the provider can only be specified in the UserControl, which is not allowed in the page declaration. The default in page is to use the defaultprovider configured in web.config, but we can use different OutputCacheProvider implementations for different pages by using the method described in 3.

Similarly, if I add [OutputCache] attributes to any of the action methods of a ASP.net MVC controller, the content is also cached and saved by using my Scottoutputcache provider:

3 You can dynamically specify which output cache provider to use on a per-request basis by overwriting the Getoutputcacheprovidername () method in the application's Global.asax file. In the following, I show that for my application in the "home.aspx" page I want to use the built-in, With the memory of the ASP.net output cache provider, and then for all other requests, I want to use the provider registered in the Web.config file: rewrite Getoutputcacheprovidername in Global.asax file ( HttpContext context) method, which returns a different implementation name according to the following example

ASP.net 4.0 new features of the use of custom OutputCache Provider

VS2010] asp.net 4.0 new features: Custom output cache Provider

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.