Asp. NET cache concept and its application analysis

Source: Internet
Author: User
Tags microsoft sql server 2005 set time

Asp. NET cache concept and its application analysis

Asp. What is a net cache? Asp. What does the net cache look like? This article provides you with a detailed description of the ASP.

Asp. What is the net cache concept? Typically, applications can improve performance by storing data that is frequently accessed, as well as those that require a lot of processing time to be created in memory. For example, if your application uses complex logic to process large amounts of data, and then returns the data as a report that is frequently accessed by users, avoid recreating the report each time the user requests the data to improve efficiency. Similarly, if your application contains a page that handles complex data but does not require frequent updates, the server re-creates the page every time it is requested, making it inefficient.

In these cases, after understanding the ASP. NET cache concept, to help you improve the performance of your application, the ASP. NET cache uses two basic caching mechanisms to provide caching capabilities. The first mechanism is application caching, which allows you to cache generated data, such as datasets or custom report business objects. The second mechanism is the page output cache, which saves the page processing output and reuses the saved output when the user requests the page again, rather than processing the page again.

Asp. NET Cache Application

Application 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 cache is volatile-that is, the data is not stored in memory throughout the application life cycle. The advantage of using application caching is that ASP. NET manages the cache, which removes items from the cache when items expire, are invalid, or are out of memory. You can also configure the application cache to notify the application when an item is removed. For more information, see Caching Application Data.

The mode of using the application cache is to determine whether the item exists in the cache when an item is accessed and, if so, if it exists. If the item does not exist, you can recreate the item and put it back in the cache. This mode ensures that the most up-to-date data is always available in the cache.

Asp. NET Cache App page output

The page output cache stores the contents of the Processed ASP. NET page in memory. This mechanism allows ASP. NET to send a page response to the client without having to go through the page processing life cycle again. Page output caching is particularly useful for pages that do not change frequently, but that require a lot of processing to create. For example, if you create a Web page with large traffic to display data that does not require frequent updates, the page output cache can greatly improve the performance of the page. You can configure the page cache for each page individually, or you can create a cache profile in the Web. config file. With a cache configuration file, you can use these settings on multiple pages by defining only one cache setting.

The page output cache provides two kinds of page cache models: full-page caching and partial-page caching. Full page caching allows the entire contents of a page to be saved in memory and used to complete client requests. Partial-page caching allows portions of the page to be cached, while others are dynamic content.

Partial-page caching can work in two ways: control caching and post-cache substitution. Control caching is also sometimes referred to as fragment caching, which allows information to be contained within a user control and then marked as cacheable to cache portions of the page output. This way you can cache specific content in a page and not cache the entire page, so you need to recreate the entire page each time. For example, if you want to create a page that displays a lot of dynamic content, such as stock information, some of which are static content (such as a weekly summary), you can place the static part in the user control and allow caching of the content.

Post-cache substitution is just the opposite of the control cache. This way, the entire page is cached, but the segments in the page are dynamic. For example, if you want to create a page that is static for a specified time period, you can set the entire page to be cached. If you add a Label control to a page that displays a user name, the contents of the label remain the same for each page refresh and for each user, and the name of the user who requested the page is always displayed. However, using the post-cache substitution mechanism, you can configure the page to be cached, but the individual parts of the page are marked as non-cacheable. In this case, you can add a Label control to the non-cacheable section, which dynamically creates the controls for each user and each page request.

Cache pages based on request parameters

In addition to a single version of a cached page, the ASP. NET page output cache also provides functionality to create multiple versions of a page that differ depending on the request parameters.

Automatically remove data

ASP. NET can remove data from the cache for one of the following reasons:

Since there is not enough memory on the server, a process called "cleanup" begins.

Because the item in the cache has expired.

Because the item's dependency has changed.

To help manage cache entries, ASP. NET notifies the application when an item is removed from the cache.

Clean

Cleanup is the process of removing items from the cache when memory is low. Items are removed if they are not accessed for a period of time or are flagged as low priority when added to the cache. ASP. NET uses the CacheItemPriority object to determine which items to clean first.

Expired

In addition to scavenging, when a cache entry expires, ASP. NET will automatically remove these items from the cache. When you add an item to the cache, you can set its expiration time as described in the following table.

Asp. NET cache expiration type

Adjustable expiration

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 from the last time it was accessed in the cache.

Absolute expiration

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:00PM, or expire after four hours.

Dependent items

You can configure the lifetime of an item in the cache to depend on other application elements, such as a file or database. When the element to which the cache entry depends changes, ASP. NET removes the item from the cache. For example, if your site displays a report that is created by an application through an XML file, you can place the report in the cache and configure it to depend on the XML file. When the XML file changes, ASP. NET removes the report from the cache. When the code requests the report, the code first determines whether the report is in the cache, and if not, the code re-creates the report. Therefore, the latest version of the report is always available.

For an ASP. NET cache app, it supports the following described dependencies:

Key dependencies

Items in the application cache are stored in key/value pairs. A key dependency allows an item to depend on the key of another item in the application cache. If the original item is removed, the item with the key dependency is also removed. For example, you can add a cache entry named ReportsValid, and then cache several reports that depend on the ReportsValid key. When the ReportsValid item is removed, all cached reports that depend on it are also removed from the cache.

File dependencies

The items in the cache depend on the external file. If the file is modified or deleted, the cache entry is also removed.

SQL dependencies

The items in the cache depend on changes to tables in Microsoft SQL Server 2005, SQL Server 2000, or SQL Server 7.0 databases. For SQL Server 2005, items in the cache can depend on a row in the table.

Aggregation dependencies

Items in the AggregateCacheDependency class cache are dependent on multiple elements by using them. If any dependencies change, the entry is removed from the cache.

Custom dependencies

You can configure the items in the cache with dependencies that you create with your own code. For example, you can create a custom Web service cache dependency that removes data from the cache when the calling Web service gets a specific value.

Asp. NET cache application Item removal notification

You can receive notifications when items are removed from the application cache. For example, if you have an item that requires a lot of processing time to create, you receive a notification when the item is removed from the cache so that it can be replaced immediately. This way, the next time the item is requested, the user does not have to wait for the item to be processed.

Asp. NET caching concepts and information about the ASP. I'd like to introduce you to this, and hopefully you'll get an idea of what the ASP is and the ASP.

(ext.) ASP and its application analysis

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.