Database Cache Dependencies _ database

Source: Internet
Author: User
Explanation Process for caching namespaces
16.1 System.Web.Caching IntroductionThis section begins with a general overview and composition of the cache namespace, summarizing the System.Web.Caching as a whole. 16.1.1 System.Web.Caching OverviewSystem.Web.Caching is the namespace used to manage caching. Caching is the temporary storage of server-side data on the client, which facilitates user's reading. The parent space of the cache namespace is "system.web", which shows that caching is commonly used in the development of Web sites, including in B/s projects. Caching is designed primarily to take into account that network bandwidth may delay data submission and postback, if the data stored in the client, users can read data directly from the client, so that the data is extracted from the local, will not be affected by the network. The System.Web.Caching namespace provides all the action classes associated with caching, including which classes are described in detail in the next section. 16.1.2 The classes within the System.Web.Caching namespaceThe caching namespace mainly provides three kinds of operations: caching Data Objects, object cache dependencies, and database cache dependencies. It caches any object using a class cache, but when the cache changes, normal objects and database objects rely on different processing. Figure 16-2 Lists the deployment scenarios that are cached in a three-tier structure. The two dependent classes CacheDependency and SQLCache dependency primarily change the cached data that is changed, and act as a notification. When the data is not cached, use the cache class to add it. The following is a description of the caching classes used in the diagram, based on the deployment of Figure 16-2. The descriptions of these classes are shown in table 16-1. Figure 16-2 the cached deployment chart in the three-tier structure the classes in the cache namespace and their descriptions
Class name Description
Cache The editing class for cached objects, which includes adding, deleting, and changing the cache
CacheDependency Base cache object dependencies, updating cached content when basic objects change
SqlCacheDependency Dependency of Database Cache objects, updating cached content when data in the database changes
16.2 Admin Cache classes: Cache classThe cache class is used to store data objects and provides methods for editing these objects. This section focuses on the methods that are included with the cache class, and how to use these methods to implement caching of data objects. 16.2.1 Function DescriptionThe cache class belongs to the dictionary class, which stores the data that the user needs according to certain rules, such as String, array, data table, DataSet and hash table. The advantage of using the cache class is that when the cached data changes, the cache class invalidates the data and implements the cached data to be added, and then notifies the application to report the cache's timely update. 16.2.2 Syntax DefinitionThe syntax for the cache class is defined as follows: public sealed class cache:ienumerable by its definition, the cache class is a class defined by "sealed", which means that the class is sealed and cannot be inherited. The cache class also inherits the IEnumerable interface, which indicates that the data in the collection is allowed to enumerate operations. The lifecycle of the cache terminates as the activity of the application domain ends, which means that as long as the application domain is still active, the cache persists because each application domain creates a cache instance. The information for this instance is obtained by HttpContext the cache property of the object or the Cache property of the Page object.        The following code shows how to add array data to the cache: ArrayList myarray = new ArrayList (); Creates an array data myarray. Add ("1. Learning Corner"); MyArray. Add ("2. Exchange Forum"); MyArray. ADD ("3. Help");        Cache.Add ("Category", myarray); Adding an array to the cache A detailed explanation of 16.2.3 methodThe method of cache class mainly provides editing operation of cached data, such as adding, deleting and changing. The most common methods and their descriptions are shown in table 16-2. Table 16-2 The main method of cache class and its description tr> The
name      " "      Ming
Add Add data to cache object
Insert Insert data items into cache that can be used to modify existing Data cache entry
Remove Remove cached data entry in cache object
get gets the specified data item from the cache object, noting that the object type is returned and that a type conversion is required
GetType Gets the type of the data item from the cache object, and after the data type is judged, facilitates conversion
GetEnumerator iterates through the cached data items in the cache object. Note that its return type is "IDictionaryEnumerator"
Tip: To modify the cached data, you only need to assign a value to the cache again. The most notable is the Add method, which uses the following syntax: Public Object ADD (     string key,      Object value, &NB sp;    cachedependency dependencies,      DateTime absoluteexpiration,       TimeSpan slidingexpiration,      cacheitempriority priority,       cacheitemremovedcallback onRemoveCallback) When using the Add method, the above 7 parameters are required and represent the following meaning:-The parameter "key" represents the key value of the cached data item and must be unique. -The parameter ' value ' represents the contents of the cached data, which can be any type. -The parameter "dependencies" represents a cached dependency, meaning that the change in this item means that the cached content has expired. If there are no dependencies, you can set this value to NULL. -Parameter ' absoluteexpiration ' is a date-type data indicating when the cache expires, the cache provided by. NET 2.0 can be used after expiration and how long it can be used to see the setting of this parameter. -the type of the parameter "SlidingExpiration" represents a time interval that indicates how long the cache parameter will be deleted, and this parameter is associated with the absoluteexpiration parameter. -The parameter "priority" represents the priority value of the undo cache, the value of this parameter is taken from the enumeration variable "CacheItemPriority", and the data item with a lower priority is deleted first. This parameter is primarily used when caching exits an object. -The parameter "onRemoveCallback" represents the event that is invoked when the data object is deleted, and is generally used as a notification program. The following code shows how to apply these methods of the cache class. When using this code, you need to be aware that the ArrayList object is used in your code, so you need to add a reference to the namespace "System.Collections" and forget to add the namespace using the cache category "SyStem. Web.caching ".     protected void Page_Load (object sender, EventArgs e)     {    & nbsp;   ArrayList myarray = new ArrayList ();        //create array data         myarray. Add ("1. Learning Corner");         myarray. Add ("2. Exchange Forum");         myarray. ADD ("3. Help");        //Adding arrays to the cache--using the Add method         Cache.Add ("Category", myarray, NULL, DateTime.Now.AddSeconds (), TimeSpan.Zero, cacheitempriority.normal, NULL);         myarray[1] = "2. Exchange Corner";                     //Modify array data            cache.insert ("Category", myarray);          //UseInsert method modifies cached data         string tmpstr = "This is a temporary data";         cache["tmpdata"] = TMPSTR;        //Using Get method to obtain cached data         Response.Write (Cache.get ("Tmpdata"). ToString () + "<br/>");/        cache["tmpdata"] = "This is a temporary string";         //re-assigning techniques for caching         Response.Write (Cache.get) (" Tmpdata "). ToString () + "<br/>");        //Using the GetType method to determine the type of cached data         if ( cache["Category"]. GetType (). Name = = "ArrayList")             Response.Write ("type is an array");        //Using the GetEnumerator method to traverse the data in the cache          IDictionaryEnumerator Mycache = Cache.getenumerator ();         while (Mycache. MoveNext ())             Response.Write (Mycache. Value + "<br/>");         cache.remove ("Tmpdata")//Remove cached temporary data using the Remove method tip: When using the GetType method, If you want to determine the type, you need to use Object.GetType (). The Name property gets the names of the types. The results of the above code are as follows: This is a temporary data this is a temporary string type is an array this is a temporary string System.Collections.ArrayList which when reading data of type ArrayList, because there is no type conversion, So the object that is typed "System.Collections.ArrayList" is taken out. This book will explain how to read the details of an array in the last instance of this section. 16.2.4 Attribute DetailedThe properties of the cache class are used primarily to get some basic information about the cached data, such as the total number of cached items, cache entries at the specified location, and so on. This book mainly introduces two attributes: Count and item. Count is used to get the total number of items in the cache. The use method is as follows: Response.Write (Cache.count); Item is used to return the contents of the specified item, which is typically inherited by a class that inherits the "IEnumerable" interface, and notes that the item needs to be wrapped with "[]". Its use is as follows: Response.Write (cache["Category"). ToString ()); 16.2.5 Typical application: Realize cache fast reading function of dataCache is used primarily for caching data that is used frequently and does not need to be updated regularly. This example implements a cache of directory listings. For simplicity, the contents of the list are not read from the database, but are kept in a ArrayList object. The purpose of this example is to populate the list of directories into a drop-down box, and when the cache fails, the contents of the directory list are empty.      The steps in the demo are described below.      Create a Web site in VS2005 named "Cachesample". Open the default generated Default.aspx page, where you add a drop-down list box and a button.

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.