Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ASP.NET cache types

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the ASP.NET cache types". In the daily operation, I believe many people have doubts about the ASP.NET cache types. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the ASP.NET cache types?" Next, please follow the editor to study!

The easiest and most effective way to improve the performance of ASP.NET applications is to use the built-in caching engine. Although you can build your own cache, because the caching engine already provides so much functionality, you don't have to bother at all. To a large extent, ASP.NET developers can directly package the functions of the cache engine into their own data representation and access classes in Web applications.

ASP.NET caching engine type

The full-page output cache caches the rendered HTML content of the entire page when a page is requested. Subsequent requests will directly fetch the cache copy.

Partial caching refers to caching a portion of HTML content, which is similar to the output of a Web user control. It is called such a name because we generally say, "submit a portion of the HTML to a page."

Data caching is concerned with caching individual variables or data items. It works at a lower level than either of the above two cache types.

Full page output cache

The full-page output cache is the simplest type of cache, requiring only the addition of a preprocessing instruction OutputCache for the page to be cached. With this cache, you don't have to reprocess a page's Init, Load,PreRender,Render, and Unload events. If those events were to access a back-end system like a database, the time savings would be considerable. ASP.NET caches several variants of a page and associates each page with subsequent requests.

Partial caching

Partial caching allows only partial HTML to be cached, which is efficient because parts of a page that change frequently can be merged with those that change infrequently, while still retrieving static parts from the cache. One example is to apply a partial cache to a Web user control that needs to call a XML Web service. This is highly efficient because it avoids overly tight coupling between your site and Web services, while significantly improving performance.

To use partial caching, you can also place an OutputCache preprocessing instruction at the top of the HTML page. But this time, we're going to put it in the ASCX page of the Web user control. Note that when using partial caching, the Location and VaryByHeader properties are no longer supported, but support for the VaryByControl property has been added.

Using the VaryByControl property, you can specify one or more properties of a user control with a semicolon-separated list. A cached version can be created for each combination of property values. For example, suppose your user control reveals a custom State property that controls which elements of the user control to display. Use the following preprocessing directives to cache a specific version of the control for each value of State:

But when caching Web user controls, keep in mind that the ASP.NET runtime directly replaces the actual control with the cached HTML, ignoring any control processing that normally occurs. This implies that code executed in a web page cannot programmatically manipulate a cached user control or any of its properties. In other words, the Web user control must be fully autonomous and able to initialize itself with its Load and Init events in order to cache effectively.

Partial caching can also be done declaratively by using an attribute instead of the OutputCache preprocessing instruction. In a code-behind file, the PartialCaching attribute can be placed in a class derived from UserControl so that the ASP.NET runtime can read it and cache the rendered HTML accordingly. For example, the following declaration from a code-behind class caches the Web user control for five minutes based on the id value in the query string.

Data caching

One of the cache types supported by the ASP.NET caching engine is data caching. By definition, its working level is lower than full-page output cache and partial cache. If several web pages use the same data (such as a product list), but want to display the data in different ways, consider using this cache. Of course, data caching has a performance advantage because it reduces the number of calls to the back-end database.

To add an item to the cache, use the Cache property of the Page or UserControl class, because both classes end up deriving from the Control class. The Cache property reveals the System.Web.Caching.Cache object, which allows you to store data as a combination of keys and values. Using this property, developers can write code to populate an item and put it in the cache; if the item already exists, it is taken out of the cache directly. As shown in the following C# code.

DataTable dt = null; if (this.Cache ["Products"] = null) {/ / Go get the data from the database this.Cache.Insert ("Products", dt, null, _ DateTime.Now.AddHours (6), TimeSpan.Zero);} else {dt = this.Cache ["Products"] As DataTable;}

The above example first checks whether the item with the Products key is in the cache. If not, retrieve an ADO.NET DataTable from the back-end database and use the Insert method to put it in the cache. This example uses an overloaded version of Insert, which allows you to specify an absolute expiration time (6 hours) for cached objects instead of specifying a periodic expiration. Instead, if you already have this item in the cache, take it back and use an As expression to cast it back to a DataTable.

When caching the data retrieved by ADO.NET, note that either the DataTable object or the entire DataSet object can be cached as in the previous example, because both objects are completely disconnected from any data source and do not maintain a database connection. Caching data readers, such as SqlDataReader, seems better because they are used only once (they are "forward-only" readers) and will always occupy a database connection when open.

The powerful flexibility and functionality of the ASP.NET caching engine make it one of the most important features when creating high-performance ASP.NET applications.

At this point, the study on "what are the types of ASP.NET caches" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report