asp.net Caching (2)

Source: Internet
Author: User
Tags datetime implement relative time interval
asp.net Cache dependencies
Dependencies can cause key values to be deleted when a file is changed, or at a specified time
。 Let's take a look at each of these dependencies.

File dependencies (file-based Dependency)
File dependency is the deletion of the corresponding item in the cache object when a file on disk changes. Now let's look at an example of reading data from an XML file.

Dim Dom as XmlDocument ()
Dom. Load (Server.MapPath ("Product.xml")
Cache ("productdata") = Dom

When the contents of the Product.xml file are changed, we should invalidate the data in the cache. Assuming that Product.xml is in the same directory as the ASPX file, we can use the following code:

Dim dependency as New CacheDependency (Server.MapPath ("Product.xml"))
Cache.Insert ("Productdata", Dom, dependency)

In this code, we set up an instance of the Cachedependecy class dependency and pass the Product.xml path to this instance. Then use the Insert method to establish a file-dependent key value.

Time dependence (time-based Dependency)
Time dependence is the deletion of an item in the cache object at a specified time. Also, we can use the Insert method to add a time dependent key value.

Absolute time-set an absolute time. For example, after 10 minutes, delete this item.
Relative time-Deletes this item when a cache has not been accessed for a number of times.
Here is a piece of code that uses relative time dependencies so that Productdata will be deleted if it has not been accessed for 10 minutes. Each visit to the Productdata will keep the productdata for another 10-minute validity.

' Minute time span
Cache.Insert ("Productdata", Loaddataset (), Nothing, DateTime.MaxValue, Timespan.fromseconds (10))

The above is a simple discussion of the cache API, we can see from here that the interface is simple and easy to use. Asp. NET uses this set of APIs to implement the page output cache.


Paging output cache (page Caching)
Asp. NET page output cache will put the content of this page in the cache. We've seen how to put a dataset in the cache. So, can the entire page be cached? In this way, you don't need to execute code on every request, you can get the results directly from memory, which brings a huge performance boost.

There are two kinds of interfaces for page caching, high-level interfaces and underlying interfaces. Here we only discuss high-level interfaces. (for the underlying interface, refer to the relevant articles in MSDN)

The high-level interface includes two page indicators, a time interval that indicates page refreshes, and one that refreshes the page when a parameter changes.

<%@ OutputCache duration= "Ten"% varybyparam= "None" >

Put this indicator on top of the page and the page results will be cached for 10 seconds. After 10 seconds, this page will be executed again. Here is an example.

<%@ OutputCache duration= "Ten"% varybyparam= "None" >
<script runat= "Server" >
Public Sub Page_Load ()
Span1. InnerHtml = DateTime.Now.ToString ("R")
End Sub
</Script>
<font size=6>the time is: <font color=red><span id= "span1" runat= "Server"/></font></font >

This page is executed once on the first request, showing the time of execution. This result will be stored in the cache for 10 seconds. If we request this page at 10:30:12, we will see the same output for 10 seconds thereafter.


Conclusion
Caching is an easy-to-use and powerful new technology for ASP programmers. Its use and ASP original Application,session objects have many similarities, only in the cache object data will automatically expire. Cache also supports files, time dependencies, and more support for callbacks. Caching technology is also used to implement page caching.

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.