Implementation of caching services in the ASP.net SAF 1th/5 page _ Practical Tips

Source: Internet
Author: User
Tags xpath
Copy code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Webinfo info = new Webinfo ();
Response.Write ("Static execution Result:" + webinfo.a + "<br/>");
Response.Write ("No Static execution Result:" + info.b);
}

public class Webinfo
{
public static DateTime a = DateTime.Now;
Public DateTime B = DateTime.Now;
}

The following is more from the article: The result is that as long as the site does not restart (the code is not modified), then a value is constant, even if the page is closed open again, and B is the refresh on the change. If you are engaged in asp.net development, you may first think of output cache, data source cache, or System.Web.Caching.Cache object caching. The purpose of caching is actually to store objects (data) in memory without recreating objects (relatively time-consuming) every time the object service is required. When an object is declared static, the object is initialized when the class to which it belongs is loaded into the AppDomain, so that the object's lifecycle is as long as the AppDomain, thereby caching purposes.

Design ideas
We often need to cache some common data in our applications for global use to improve performance. If the type and number of objects you want to cache are fixed, we might declare them as static, and if we need to cache the type and number of objects that are variable, we might be able to use a static hashtable. But Hashtable has a flaw: it has no hierarchy, it always stores the data in the form of a key/value, and a key corresponds to a value, which is more difficult if we want to get the associated set of data.

The structure of the XML document is tree-shaped and has a standard hierarchical structure. XPath is used to select one or more nodes from an XML document. For example, "/bookstore/book", select all the child nodes under the book node.

The caching service in the SAF is used as a bridge through a dynamically constructed XML document tree in memory, combining static (static) caching and XPath techniques to support the use of XPath syntax to get objects in Hashtable. Where the static cache carries out the actual data cache, XPath is used to get the data object. From the programmer's point of view, that is, the Hashtable key supports XPath syntax, you can think of the original "flat" Hashtable as a "tree structure", its node contains the cached data, we pass the standard XPath to the node (of course this is just a false impression) and get the data. In this way, you can use XPath to get multiple related data objects in the Hashtable at once. Simply put, the SAF caching service is designed to implement a hierarchical (tree-shaped) caching structure that enables more flexible operation of the cache.

And how do you actually achieve this process? We look at it in a step-by-step way:
1, first in the memory of the dynamic construction of an XML document, it contains only a root node, can be arbitrarily named, here it named for cache.
2, provide an XPath path: to get the object (data) before the first to store the object, the object will naturally have to provide a path (called "path", because it is an XPath, is actually equivalent to the key in Hashtable).
3, according to the path provided in the previous step, to cache as the root node, depth to create XmlNode nodes.
4. Generate a GUID, add a key attribute to the leaf node, and assign the value to the GUID for this key property.
5. Store objects in Hashtable, where Hashtable key is the GUID generated in the previous step, and value is the object to be stored.

In this way, the actual key of the Hashtable, the dynamically generated GUID, is transparent to the programmer, and the programmer only needs to provide an XPath expression when storing/fetching the object. The following picture illustrates the relationship between them:

Here's another three points to note:
1. We use Hashtable to store objects, you can declare Hashtable as static directly, or you can declare Hashtable as instance, but declare the object that the hashtable belongs to as static. This applies the singleton pattern, encapsulating the operations of Hashtable into a class, and then applying the singleton pattern on that class to ensure that the class has only one (the Hashtable instance maintained by this class is naturally only one). Obviously, this class contains the main logic, which we name as cache.
2, the advantage of using Hashtable is that you can store any type of object, the disadvantage is the loss of type safety. Sometimes we might want to use a generic collection class to replace Hashtable, such as Dictionary<t key, T value>. So here we've introduced the strategy pattern and created a Icachestrategy interface that includes three methods for adding, fetching, and deleting objects, respectively.
3. When using XPath to obtain a node, it can be based on the relative path of the current node, or it can be an absolute path based on the root node. In the example program in this article, the absolute path is used, which is obviously more convenient.
Current 1/5 page 12345 Next read the full text
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.