Asp.net| Caching database-driven Web applications, if you need to improve their performance, the best approach is to use caching capabilities. A user retrieves data from a database, possibly one of the slowest operations in a Web application. Because it involves a number of links, such as Web servers, database servers, and so on. Especially in the user more, the amount of data retrieved is relatively large, it will be to include users in all aspects of the difficulties caused. If you can cache data from a database to memory (or you can store it in another location), you do not have to access the database when you request each page. The ability to return data from memory is always faster than the newly supplied data, which can greatly provide the performance of the application.
Caching has a disadvantage that is not easily overcome, that is, the problem of data expiration. Most typically, if you cache the contents of a database table into server memory, the Web application is likely to display outdated, inaccurate data when records in the database table change. For some types of data, even if the displayed information expires, the impact is not very large. However, it is unacceptable for data that is more stringent in real time requirements, such as stock prices, auction bids, and so on.
To solve these problems, ASP.net 1.x has provided some good caching features, such as page output caching, partial caching, page data caching, and more. While these caching features address the problem of data caching, there are still large drawbacks that developers must weigh in between performance and data expiration, and the problem of data expiration is always haunting developers. For example, if the data in a database table changes, the cache may not be updated for a specified amount of time, and must wait for the cache to expire, which can cause users some trouble. Ideally, any updates in the database table can be immediately reflected in the cached data, and ASP.net 2.0 overcomes the above deficiencies and solves the problem. The caching function of ASP.net 2.0 is extended on the basis of ASP.net 1.x.
ASP.net 2.0 supports the following types of caching
• Page Output caching
Page output caching is the simplest caching mechanism that saves the entire ASP.net page content in server memory. When the user requests the page, the system outputs the relevant data from memory until the cached data expires. In this process, the cached content is sent directly to the user without having to go through the page process lifecycle again. Typically, page output caching is especially useful for pages that contain content that does not need to be modified frequently, but that requires a lot of processing to compile. It is important to note that the page output cache saves the entire contents of the page in memory and is used to complete the client request.
• Page Partial caching
As the name suggests, a partial cache of pages stores part of the page in memory in response to user requests, while other parts of the page are dynamic content. The implementation of page partial caching includes two ways: control caching and replace-behind caching. The former is also known as fragment caching, which allows you to cache portions of the page's output by allowing information that needs to be cached to be contained within a user control, and then marking the user control as cacheable. This approach caches specific content on the page without caching the entire page, so you'll need to re-create the entire page each time. For example, if you want to create a page that displays a large number of dynamic content (such as stock information), some of which are static content (such as weekly summaries), you can place the static part in the user control and allow caching of the content. The cache replacement is exactly the opposite of the control cache. This way the entire page is cached, but the sections in the page are dynamic. For example, if you want to create a page that is static for a specified period of time, you can set the entire page to be cached. If you add a Label control that displays a user name to a page, the contents of the label remain unchanged for each page refresh and for each user, always displaying the name of the user who requested the page before caching the page. After using the cache replacement mechanism, you can configure the page to be cached and to mark individual portions of the page as not cacheable. In this case, you can add a label control to the non-cached part so that the controls are dynamically created for each user and for each page request.
• Application Data caching
Application data caching provides a programmatic way to store arbitrary data in memory through key/value pairs. Using application caching is similar to using application state. However, unlike application state, the data in the application data cache is volatile, meaning that the data is not stored in memory throughout the application life cycle. The advantage of the application data cache is that the asp.net manages the cache, which removes items from the cache when items expire, is invalid, or is out of memory, and can configure application caching to notify the application when an item is removed.
• Cache dependencies
ASP.net 1.x has already provided some caching dependencies based on time, files, directories, and so on. Although these features can handle some common problems, they cannot solve the problem of data expiration. For this reason, asp.net 2.0 has added SQL data cache dependency functionality. The core of this feature is the SqlCacheDependency class. Different versions of SQL Server, which have varying degrees of support for SQL data cache dependencies, have a significant difference in how to use them. In addition, ASP.net 2.0 also supports custom cache dependencies with CacheDependency classes, as well as aggregation cache dependencies with aggregatecachedependency classes as the core.
ASP.net 2.0 includes a number of new features that help with caching configuration. For example, allow Web.config files to be used to create cache settings. With the appropriate settings in the Web.config file and referencing these settings in a single page, caching settings can be applied to multiple pages at the same time. Also, the cache settings add more options for customizing caching performance.
The above is a brief introduction to the caching capabilities provided by ASP.net 2.0, which increase the throughput of the request response to improve application performance. In fact, some of these caching features are inherited from ASP.net 1.x and are enhanced and others are new. Enhancements include a more powerful page-partial caching model, enhanced cache configuration, and improved output-caching directives. New features include Web.config cache configuration support, custom cache dependencies, aggregation cache dependencies, SQL data cache dependencies, and cache replacement.
There are two other concepts that need to be understood before you can formally begin to introduce the various caching features. One is cache cleanup and the other is cache expiration.
Cache cleanup means removing cached data from memory. This can be caused by the following 3 reasons: First, the cached item data expires. Each expired cache entry data must be deleted, or the server will not have enough memory to affect other application execution. The second is that the cache dependencies change. Dependencies have a close relationship with data cache entries. Depending on your application settings, the data cache is likely to be purged if the dependencies change. Third, the cache cleanup process begins because the server is low on memory. Items are removed if they are not accessed for a period of time, or if they are marked as low-priority when added to the cache.
The above describes the concept and cause of cache cleanup, which involves a problem with data expiration. In asp.net 2.0, when you add an item to the cache, you can set two types of data expiration. One is called a tunable expiration, which specifies how long an item has expired since the last time it was accessed. For example, you can set an item to expire 20 minutes after it was last accessed from the cache. The other is an absolute expiration, which specifies that an item expires at a set time, regardless of the frequency of access. For example, you can set an item to expire at 6 o'clock in the afternoon, or after 4 hours.
The caching features of ASP.net 2.0 have the following advantages:
• Support for broader and more flexible development features
ASP.net 2.0 contains a number of new cache controls and APIs. For example, custom cache dependencies, substitution controls, page output caching APIs, and more, these features can significantly improve the developer's control over caching capabilities.
• Enhanced Manageability
With the configuration and management features provided by ASP.net 2.0, caching functions can be more easily managed.
• Delivers higher performance and scalability
ASP.net 2.0 provides new features, such as SQL data cache dependencies, that will help developers create high-performance, scalable Web applications.
In addition, the caching function also has its own shortcomings. For example, the displayed content may not be the most recent and accurate, so you must set up an appropriate caching policy. For example, caching increases the complexity of the system and makes it difficult to test and debug, so it is recommended that you develop and test your application without caching, and then enable caching options during the performance tuning phase.
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.