"Anatomy PetShop" Series IV: PetShop asp.net cache

Source: Internet
Author: User

If the microcomputer hardware system has enough knowledge, then we to the cache this noun must be familiar. In the CPU as well as the motherboard's chip, this kind of name is introduced the high speed buffer memory (cache) the technology. Because cache access speed is faster than memory, so the introduction of cache can effectively solve the CPU and memory speed mismatch problem. The hardware system can use cache to store the CPU access probability of those data, when the CPU needs to access this data can be read directly from the cache, without access to the relatively slow access to memory, thus increasing the efficiency of the CPU. The software design borrows from the mechanism of introducing caching in hardware design to improve the performance of the whole system, especially for a database-driven Web application, the use of caching is indispensable, after all, the database query is probably the most frequently invoked but one of the slowest operations throughout the Web site, We cannot be dragged by its old legs to slow our march. Caching mechanism is the accelerator that solves this flaw.

4.1 ASP. NET Cache Overview

As. NET Framework, the main product of Web application development, ASP. NET fully considered the caching mechanism. By some means, the system needs the data objects, Web pages stored in memory, so that when the Web site needs to obtain this data, do not need to go through the cumbersome database connection, query and complex logic operations, you can be "within reach", such as "Tannangquwu" as easy and fast, Thus improving the performance of the entire web system.

Asp. NET provides two basic caching mechanisms to provide caching capabilities. One is application caching, which allows developers to put data or report business objects generated by a program into the cache. Another caching mechanism is page output caching, which allows you to get a page that is stored in the cache without having to deal with the page in a cumbersome way.

Application caching its implementation is prosaic, simply by ASP.net to manage the cached space in memory. The application data object that is placed in the cache is stored as a key/value pair, which makes it easier for users to determine whether the item exists in the cache based on the key value when accessing the data item in the cache.

The data objects placed in the cache are limited in their lifecycle, and cannot be guaranteed to remain valid even throughout the lifecycle of the application. Asp. NET can manage the application cache, such as removing data items when they are invalid, outdated, or out of memory. In addition, the caller can also use the CacheItemRemovedCallback delegate to define a callback method that notifies the user when a data item is removed.

In the. Net framework, application caching is implemented through the System.Web.Caching.Cache class. It is a sealed class and cannot be inherited. For each application domain, you create an instance of the cache class that is consistent with the life cycle of the application domain. We can use the Add or Insert method to add data items to the application cache as follows:

Cache["First"] = "First Item";
Cache.Insert("Second", "Second Item");

We can also add dependencies to the application cache so that when a dependency changes, the data item can be removed from the cache:

string[] dependencies = {"Second"};
Cache.Insert("Third", "Third Item",
new System.Web.Caching.CacheDependency(null, dependencies));

This corresponds to the removal of the data item in the cache. As mentioned earlier, ASP.net can automatically manage the removal of items in the cache, but we can also explicitly remove the relevant data items in the way that code is written:

Cache.Remove("First");

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.