What we will learn today is ASP. NET data cache.
A friend who has been familiar with asp.net 1.x may say that this is not a new thing.
Yes, ASP. NET data cache is indeed not a new product of asp.net 2.0, but asp.net 2.0 gives data cache more attributes and methods, so that data cache can be a great deal in the asp.net 2.0 era.
The data cache stores frequently requested content in the server cache, so that the content can be responded more quickly when it is requested again, and effectively reduces the resource usage of the server, improves the running performance of the program.
Asp.net 2.0 supports the following cache solutions:
1. ASP. NET data cache for page output
The page output cache is a relatively simple caching mechanism at the traditional level. It caches page data in the server memory. When a client requests the content again, the server can directly output the page data until the data cache expires.
There are two ways to use the page output cache:
Use the @ OutputCache command. Common Code is as follows:
<%@ OutputCache Duration = "60"
VaryByParam = "sID" Location = "Any" %> 〉
The preceding example defines that the cache validity period for the page output is 60 s, and a new cache is created after 60 s. The Cache version varies depending on the sID parameter passed on the page. Location = "Any" is specified.
Use the page output cache API. This method is executed in part of the page program. Common Code is as follows:
Response. Cache. SetExpires
(DataTime. Now. AddSeconds (60 ));
Set the validity period of the page cache to 60 s.
There are many other attributes of the page output cache, which are commonly used here. For more information, see: http://www.itgao.com/html/2007-04/19811.html
2. Partial page Cache
Sometimes we may not want to cache the whole page, but just cache a part of the page. There are three common methods:
Use the @ OutputCache command
The essence of this method is to make this part of the content to be cached into a user-defined control, and then set the page cache code for the custom control. The method is the same as the page output cache.
Use the PartialCachingAttribute class
This method sets the cache configuration of the control in the code hidden file of the user control as follows,
PartialCaching (20)]
Public partial class NewUserControl: UserControl
{......}
Use the ControlCachePolicy class
Note the following when using the ControlCachePolicy class.
First, if you want to create a correct and valid ControlCachePolicy class instance to set the control cache;
You must access the BasePartialCachingControl. CachePolicy attribute of the PartialCachingControl class (BasePartialCachingControl is the base class of the PartialCachingControl class ).
Second, the ControlCachePolicy instance can be successfully operated only between the Init and PreRender stages of the control lifecycle.
Sample Code:
Use the PartialCachingAttribute class to set the user control Cache
(User control code hidden file)
[PartialCaching (100)]
Public partial class SimpleControl:
UserControl
{......}
ASP. NET page file source code.
Use the ControlCachePolicy class to set the user control Cache
(ASP. NET page file)
<% @ Page Language = "C #" Debug = "true" %> 〉
<%@ Reference Control = "SimpleControl. ascx" %> 〉
<Script language = "C #" runat = "server"> "〉
Void Page_Init (object sender, System. EventArgs e)
{
// Dynamically load user controls,
And returns the Instance Object of PartialCachingControl.
PartialCachingControl pcc = LoadControl
("SimpleControl. ascx") as PartialCachingControl;
// Obtain the ControlCachePolicy instance through the CachePolicy attribute
ControlCachePolicy cacheSettings = pcc. CachePolicy;
// If the cache expiration setting of the user control is greater than 60 seconds,
Set the new expiration time to 30 seconds and set it to an absolute expiration policy.
If (cacheSettings. Duration> TimeSpan. FromSeconds (60 ))
{
// Set the expiration time and cache expiration Policy for the user control
CacheSettings. SetExpires (DateTime. Now. Add
(TimeSpan. FromSeconds (30 )));
CacheSettings. SetSlidingExpiration (false );
}
// Add the user control to the page control hierarchy
Controls. Add (pcc );
}
</Script> 〉
3. Application Data Cache
The main function of ASP. NET data cache is to store various application-related objects in the memory. There are three methods:
Specify the key and value www.2cto.com
Cache ["keyName"] = "123 ";
This statement creates or overwrites the cache named txtName and assigns a value of 123.
Use the Add Method
Cache. Add ("keyName", "123", null, DataTime.
Now. AddSeconds (60), TimeSpan. Zero,
CacheItemPriority. High, onRemove );
This sentence implements the same functions as the preceding example, and sets its cache dependency to null. The cache validity period is 60 s; the time interval from the last access to the added object to the object's expiration time is zero (TimeSpan. zero); cache object priority is High; when the cache is deleted, the call delegate name is onRemove.
Insert Method
The Insert and Add methods are basically the same, but the Insert method also has several methods after reload, for example:
Cache. Insert ("keyName", "123 ");
4. cache dependency
ASP. NET data cache has many advantages, but it also has drawbacks. For example, if the real-time data is obtained, the page may be cached on the server several dozen seconds or even a few hours ago. This is intolerable for programs with high real-time requirements. At this time, we can determine whether the program needs to refresh the cache by setting the cache dependency and determining the changes to the dependent file.
There are many ways to cache dependencies. Here we will focus on custom dependency Caching: Custom cache dependency
Code:
String fileName = Server. MapPath
("File. xml"); // sets the file path
DateTime dt = DateTime. Now;
// Set the start time of the tracing dependent File
CacheDependency dep = new CacheDependency
(FileName, dt); // create a dependency object
From Qiu zhengxiang's column