I wanted to write more about technology early.
One is for themselves can more aspects of http://www.php.cn/php/php-tp-demand.html "target=" _blank "> Query, and of course, the more aspects of everyone pull. Ha
Less gossip.
We're going to put together a little bit today.
Page Caching
Use the OutputCache directive.
<%@ OutputCache duration= "3600"
location= "any"
varybycustom= "Browser"
Varybyparam= "RequestID"%>
Where the duration and VaryByParam characteristics are required.
Location controls where the page caches
Location
|
Meaning
|
Any
|
The default value. means that the output of the page can slow down the presence of a client browser, slow down any "downstream" clients (such as a proxy server), or cache the Web server itself
|
Client
|
Indicates that the output cache can only be stored in the local cache of the client that made the request (that is, the browser)
|
Downstream
|
Indicates that the output cache can be stored in any device that supports HTTP1.1 caching (such as a proxy server)
|
Server
|
Indicates that the output cache will be stored on the Web server
|
None
|
Indicates that the page disables output caching
|
Duration allows us to control how long the page survives in the cache (in seconds)
VaryByParam allows us to cache different versions of a page. In the example above, VaryByParam is set to RequestID, so ASP. NET uses different values of the RequestID parameter, either in the query string for HTTP GET or in the parameters of the HTTP post. The application can be differentiated by checking the value of the RequestID parameter, and by placing varybyparam= "RequestID" in the outputcache instruction of the page, you can have ASP. NET to cache different versions of the page for each user.
If you do not want to cache the page's not-asked version based on the value of the parameter, simply set VaryByParam to none.
You can also ask ASP to cache one version of the page for each possible combination of parameters. To do this, VaryByParam can be set to *.
The VaryByHeader and VaryByCustom features are similar to VaryByParam in that they allow you to specify when a new cached version of the page should be created.
VaryByHeader allows us to cache the non-versioned version of the page based on the list of HTTP headers separated by semicolons.
VaryByCustom when set to browser, allows us to cache different versions based on the browser's name and major version information. It can also be set to the name of a custom method, thus implementing our own logic to control the cached version.
Fragment Cache
You can use user controls to segment pages, write cached statements in an ascx file, and not write cache statements in an ASPX file, so that ASP. NET can cache only the output of the ascx fragment. Generally, like a header or footer is basically the same, you do not need to reload. However, if there is a dynamic change in the data can not be cached, because once cached, the program will not create its instance to update the data display, only wait until the lifetime expires, so it is not suitable for this case with page fragment caching.
Attention:
Note that the fragment cache does not support the location feature; the only legitimate place to cache a page fragment is the Web server. This is because the fragment cache is a new feature in ASP., so browsers and proxy servers are not supported.
Second, fragment cache has another feature--varybycontrol that is not in the page cache. The VaryByControl feature allows you to specify a semicolon-delimited list of strings that represent the names of the controls used within the user control, and ASP. NET generates a cached version of the user artifact for each different combination of values.
Data Caching
The low-level API is the cache class, which is located in the System.web.Caching namespace in ASP. You can use it to cache data that is very resource-intensive. The use and session of the cache class is as simple as the Application object. There is only one cache object per application-this means that the data stored in the cache using the cached object is an application-level data. To make things easier, the cache property of the page class enables the application's cache object instance to be used in code.
Data cached through the cache object is stored in the application's memory. This means that the lifetime of the data does not exceed the restart of the application (in fact, this is the same as the data stored in the application and Session object, unless the session data is stored using the Stateservice or SQL State session mode).
The specific use and syntax are the same as the session and application. When you convert back, you need to be aware of the types of coercion that are cast.
This is not the only way to add cache entries in the ASP. The cache object has two methods for the Insert () method and the Add () method, which are more flexible. Their usage is approximate, but slightly different:
The Insert () method is used to overwrite existing cache entries in the ASP.
The Add () method is used only to add a new cache entry in the ASP. NET cache (if it overwrites an existing cache entry, it will fail).
Each method has 7 parameters, and the parameters of the two methods are the same.
When you cache an item, you can specify its dependencies to tell ASP. NET that the cache entry remains in the cache until an event occurs.
Correlation value
|
Meaning
|
CacheDependency
|
Allows you to specify a file or cache key. If the file changes, the object is deleted. If the cache key is changed, the object is also deleted.
|
Datetime
|
This is a Datatime value that indicates when the cached data expires (absolute expiration time)
|
TimeSpan
|
This is a time interval that indicates how long cached data can be retained in the cache after the last access (elastic expiration Time)
|
Use CacheItemPriority to specify the priority of the cached data to remove those low-priority data when the cache is filled.
Priority value
|
Meaning
|
High
|
The cache entry that is set for this priority is the least likely to be deleted when the memory is low
|
AboveNormal
|
Cache entries that are set to this priority are reserved more preferentially than cache entries with a priority of normal or less
|
Normal
|
Cache entries that are set to this priority are reserved more preferentially than cache entries with priority BelowNormal and low
|
BelowNormal
|
This is the priority of the second-to-last level; The cache entry for this priority is reserved only for cache entries that are set to low priority
|
Low
|
The cache entry that is set to this priority is most likely to be deleted when the memory is low
|
Default
|
The default value for the priority of the cache entry is normal
|
Notremovable
|
When the cache entry is set to this priority, you are telling ASP. Even if it is out of memory, do not remove it from the cache
|
DateTime dt = new DateTime (DATETIME.NOW.YEAR,12,31);
Cache.Add ("Membersdataset", Dsmembers,null,
Dt,timespan.zero,
Cacheitempriority.normal,null);
The first parameter is the key that references the cached object, and the second parameter is the object to be cached. The third parameter is null (indicating no correlation).
The four and fifth parameters are the absolute expiration time and the elastic expiration time. Here, we specify that the cache should expire on the last day of the current year (DT). We want to specify an expiration time that is not elastic, so the fifth parameter uses TimeSpan.Zero. The sixth parameter uses a value from the System.Web.Caching.CacheItemPriority enumeration to set the priority to normal.
Specifies a 5-minute elastic expiration time without specifying an absolute expiration time
Cache.Add ("Membersdataset", Dsmembers,null,
Datetime.maxvalue,timespan.fromminutes (5),
Cacheitempriority.normal,null);
Adds a dependency. In this example, the expiration time also depends on the modification of a file, the Test.xml file:
CacheDependency dep = new CacheDependency (@ "C:/test.xml");
Cache.Add ("Membersdataset", DSMEMBERS,DEP,
Datetime.maxvalue,timespan.fromminutes (5),
Cacheitempriority.normal,null);
The expiration time depends on the modification of another item in the cache:
string[] Dependencykeys = new String[1];
Dependencykeys[0] = "memberschanged";
CacheDependency dependency = new CacheDependency (null, Dependencykeys);
Cache.Add ("Membersdataset", Dsmembers,dependency,
Datetime.maxvalue,timespan.zero,
Cacheitempriority.normal,null);
The last parameter is the CacheItemRemovedCallback type, which allows us to request notification when the cache entry is removed from the cache, and can write a custom method (like the Itemremovedcallback () method here), Then specify the method in the 7th parameter:
public void Itemremovedcallback (String key, Object value, CacheItemRemovedReason reason)
{
}
Cache.Add ("Membersdataset", Dsmembers,dependency,
Datetime.maxvalue,timespan.fromminutes (5),
Cacheitempriority.normal,
New CacheItemRemovedCallback (this. Itemremovedcallback));
The first parameter is the key that is used when storing the cache entry in the cache, the second is the stored object itself, and the third is the reason for the cache entry deletion.