Introduction to the cache technology of ASP. NET 2.0

Source: Internet
Author: User
Tags server memory stock prices
The best way to improve the performance of database-driven Web applications is to use the cache function. Retrieving data from a database may be one of the slowest operations performed in a web application. Because it involves multiple links, such as web servers and database servers. Especially when there are a large number of users and a large amount of data to be retrieved, it will cause great difficulties for all aspects including users. If you can cache data in the database to the memory (or in other places), you do not need to access the database when requesting each page. Since the data returned from the memory is always faster than the new data, the performance of the application can be greatly improved.

Cache has a disadvantage that cannot be easily overcome, that is, data expiration. The most typical case is that if the data content in the database table is cached in the server memory, when the records in the database table change, web applications may display expired and inaccurate data. For some types of data, even if the displayed information expires, the impact will not be great. However, for data with strict real-time requirements, such as stock prices and auction prices, it is unacceptable to display data that has expired slightly.

To solve the above problems, ASP. NET 1.x has provided some good cache functions, such as page output cache, partial cache, and page data cache. Although these caching functions can solve data caching problems, there are still many shortcomings. Developers must make a trade-off between performance and data expiration, the problem of data expiration has always plagued developers. For example, if the data in the database table changes, the cache may not be updated within the specified time, but it must wait until the cache expires, which may cause users some trouble. Ideally, any update in the database table can be immediately reflected in the cached data. ASP. NET 2.0 overcomes the above shortcomings and solves this problem. The cache function of ASP. NET 2.0 is extended based on ASP. NET 1.x.

ASP. NET 2.0 supports the following cache types:

· Page output Cache

Page output cache is the simplest caching mechanism, which stores the entire ASP. NET page content in the server memory. When a user requests this page, the system outputs relevant data from the memory until the cached data expires. In this process, the cached content is directly sent to the user without having to go through the page processing lifecycle again. In general, the page output cache is particularly useful for pages that contain content that does not need to be modified frequently, but require a large amount of processing before compilation. You must note that the page output cache stores all the page content in the memory and is used to complete client requests.

· Partial page Cache

As the name suggests, some pages are cached in the memory to respond to user requests, while others are dynamic. The page cache can be implemented in two ways: the control cache and the replaced cache. The former can also be called fragment caching. This method allows you to include the information to be cached in a user control, and then mark the user control as cacheable, to Cache part of the page output. This method caches specific content on the page without caching the entire page. Therefore, you must recreate the entire page each time. For example, if you want to create a page that displays a large amount of dynamic content (such as stock information), some of which are static content (such as weekly summary), you can place the static content in the user control, and allow caching of the content. The cache replacement is the opposite to the control cache. This method caches the entire page, but the sections in the page are dynamic. For example, if you want to create a page that is static within a specified period of time, you can set the entire page to cache. If you add a label control that displays the user name to the page, the label content remains unchanged for each page refresh and each user, the name of the user who requested the page before caching the page is always displayed. With the cache replacement mechanism, you can configure the page to be cached and mark some parts of the page as non-cached. In this case, you can add label controls to the non-cacheable section to dynamically create these controls for each user and each page request.

· Application Data Cache

The application data cache provides a programming method that stores any data in the memory through key/value pairs. The application cache status is similar to that of the application. However, unlike the application state, the data in the application data cache is easy to lose, that is, the data is not stored in the memory throughout the application lifecycle. The advantage of application data cache is that ASP. manage the cache. It will remove the items in the cache when the items expire, are invalid, or the memory is insufficient. You can also configure the application cache to notify the application when the items are removed.

· Cache dependency

ASP. NET 1.x provides cache dependencies Based on Time, files, directories, and other functions. Although these functions can solve some common problems, they cannot solve the problem of data expiration. Therefore, SQL data cache dependency is added to ASP. NET 2.0. The core of this function is the sqlcachedependency class. Different Versions of SQL Server have different degrees of support for SQL data cache dependencies. Therefore, the usage varies greatly. In addition, ASP. NET 2.0 supports custom cache dependencies with the cachedependency class as the core and aggregate cache dependencies with the aggregatecachedependency class as the core.

ASP. NET 2.0 includes some new features that help with cache configuration. For example, you can use the web. config file to create cache settings. Make the appropriate settings in the web. config file, and reference these settings in a single page, you can apply the cache settings to multiple pages at the same time. In addition, more options are added to the cache settings for customizing cache performance.

The above briefly introduces the cache functions provided by ASP. NET 2.0, which can improve the Request Response throughput to improve application performance. In fact, some of these cache functions inherit from ASP. NET 1.xand are enhanced, while others are newly added. Enhanced functions include more powerful page cache models, enhanced cache configurations, and improved output cache commands. New features include web. config cache configuration support, custom cache dependencies, aggregate cache dependencies, SQL data cache dependencies, and post-cache replacement.

There are two concepts to understand before we officially start introducing the various cache functions. One is cache clearing, and the other is cache expiration.

Cache cleanup refers to removing cached data from memory. It may be caused by the following three reasons: first, the cache item data has expired. Each expired cache item data must be deleted; otherwise, the server memory is insufficient, affecting the execution of other applications. Second, the cache dependency changes. Dependencies are closely related to data cache items. According to the application settings, if the dependencies change, the data cache is likely to be cleared. Third, the cache cleaning process starts due to insufficient server memory. If some items are not accessed for a period of time, or are marked as low-priority when added to the cache, these items are removed.

The above describes the concept and cause of cache clearing, which involves a problem of data expiration. In ASP. NET 2.0, you can set two data expiration types when adding items to the cache. A type is called adjustable expiration, that is, to specify how long a certain item expires after the last access. For example, you can set an item to expire 20 minutes after the last access in the cache. The other is absolute expiration, that is, specifying that an item expires at the specified time, regardless of the Access frequency. For example, you can set an item to expire at six o'clock P.M. or 4 hours later.

The cache function of ASP. NET 2.0 has the following advantages:

· Supports more extensive and flexible development features

ASP. NET 2.0 contains some new cache controls and APIs. For example, custom cache dependencies, substitution controls, and page output cache APIs can significantly improve developers' control over cache functions.

· Enhanced manageability

With the configuration and management functions provided by ASP. NET 2.0, you can manage cache functions more easily.

· Higher performance and scalability

ASP. NET 2.0 provides some new functions, such as SQL data cache dependencies, which will help developers create high-performance and scalable Web applications.

In addition, the cache function also has its own shortcomings. For example, the displayed content may not be the latest or most accurate. Therefore, you must set an appropriate Cache Policy. Another example is that caching increases system complexity and makes it difficult to test and debug. Therefore, it is recommended to develop and test applications without caching, and then enable caching options in the Performance Optimization phase.

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.