C # cache Introduction

Source: Internet
Author: User

The cache is mainly used to speed up Data Reading. Because there is a traffic bottleneck between the server and the application client, when reading large data volumes, cache is used to serve the client directly, which can reduce the data interaction between the client and the server, this greatly improves the program performance.

In this chapter, the namespace "System. web. beginning with Caching, we will introduce in detail the cache classes and operation methods provided by the framework. They mainly involve three technical points: simple data cache, data cache dependency, and database cache dependency, finally, we will demonstrate an instance that fully uses cache for Data Reading. The process is described in 16-1.

Figure 16-1 Description of the cache namespace

16.1 introduction to System. Web. Caching

This section describes the general introduction and composition of the cache namespace and provides an overview of System. Web. Caching.

16.1.1 System. Web. Caching Overview

System. Web. Caching is used to manage the cache namespace. Cache is to temporarily Save the server data on the client for easy reading. The parent space of the cache namespace is "System. Web". It can be seen that the cache is usually used for Web site development, including development in the B/S project. The cache design mainly takes into account that the network bandwidth may delay data submission and re-sending. If you save the data on the client, you can directly read the data from the client, in this way, the data is extracted locally and will not be affected by the network.

The System. Web. Caching namespace provides all the cache-related operation classes, including which classes will be detailed in the next section.

16.1.2 composition of classes in the System. Web. Caching namespace

The cache namespace provides three operations: cache data objects, object cache dependencies, and database cache dependencies. Any cached object uses a class Cache, but when the Cache changes, the dependencies between common objects and database objects are different.

Figure 16-2 lists the cache deployment in a three-tier structure. The two dependent classes CacheDependency and SqlCache Dependency mainly change the cache data that has changed and serve as a notification. When the data is not cached, use the Cache class to add it.

The following describes the cache classes used in the figure based on the deployment in Figure 16-2. Descriptions of these classes are shown in Table 16-1.

 

Figure 16-2 Deployment of cache in a layer-3 Structure

Table 16-1 classes in the cache namespace and their descriptions

Class Name

Description

Cache

Edit a cache object. operations include adding, deleting, and modifying a cache object.

CacheDependency

The dependency of the basic cache object. When the basic object changes, the cached content is updated.

SqlCacheDependency

The dependency of the database cache object. When the data in the database changes, the cache content is updated.

16.2 Cache Management class: Cache class

The Cache class is used to store data objects and provides methods to edit these objects. This section describes the methods contained in the Cache class and how to use these methods to Cache data objects.

16.2.1 Function Description

The Cache class is a dictionary class that stores user-required data according to certain rules. The data types are unrestricted, including strings, arrays, data tables, Dataset, and hash tables.

The advantage of using the Cache class is that when the cached data changes, the Cache class will invalidate the data and re-Add the cached data, and then notify the application to report timely updates to the Cache.

16.2.2 syntax definition

The syntax of the Cache class is defined as follows:

Public sealed class Cache: IEnumerable

Through its definition, we can find that the Cache class is a class defined by "sealed", indicating that this class is sealed and cannot be inherited. The Cache class also inherits the IEnumerable interface, which allows enumeration of data in the set.

The cache lifecycle ends with the completion of the application domain activity. That is to say, the cache will remain as long as the application domain is still active, because each application domain creates a cache instance. The instance information is obtained through the Cache attribute of the HttpContext object or the Cache attribute of the Page object.

The following code demonstrates how to add array data to the cache:

ArrayList myarray = new ArrayList (); // create an array of data

Myarray. Add ("1. Learning Garden ");

Myarray. Add ("2. Communication Forum ");

Myarray. Add ("3. Help ");

Cache. Add ("Category", myarray); // Add the array to the Cache

16.2.3 detailed explanation of the Method

Cache methods mainly provide editing operations for Cache data, such as adding, deleting, and modifying. The most common methods and their descriptions are shown in Table 16-2.

Table 16-2 main Cache methods and descriptions

Name

Description

Add

Add data to Cache object

Insert

Insert a data item to the Cache to modify an existing data Cache item.

Remove

Remove cached data items from the Cache object

Get

Obtain the specified data item from the Cache Object. Note that the returned data type is Object and type conversion is required.

GetType

Obtain the data item type from the Cache object and determine the data type for easy conversion.

GetEnumerator

Cyclically access the cached data items in the Cache object. Note that the return type is "IDictionaryEnumerator"

Tip: to modify the cache data, you only need to assign a value to the cache again.

Note that the Add method uses the following syntax:

Public Object Add (

String key,

Object value,

CacheDependency dependencies,

DateTime absoluteExpiration,

TimeSpan slidingExpiration,

CacheItemPriority priority,

CacheItemRemovedCallback onRemoveCallback

)

When the Add method is used, the preceding seven parameters are required. Their meanings are as follows:

-The "key" parameter indicates the key value of the cached data item, which must be unique.

-The "value" parameter indicates the content of the cached data, which can be of any type.

-The "dependencies" parameter indicates the cached dependency, that is, this change means that the cached content has expired. If there is no dependency, you can set this value to NULL.

-The "absoluteExpiration" parameter indicates the expiration time of the cache. The cache provided by. NET 2.0 can be used after expiration. For how long it can be used, see this

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.