Asp tutorial. net provides three main forms of cache: page-level output cache, user control-level output cache (or segment cache), and cache api. The advantage of output cache and fragment cache is that it is very easy to implement. In most cases, it is sufficient to use these two caches. The cache api provides additional flexibility (in fact, it is quite flexible) and can be used to take advantage of the cache at each layer of the application.
Cache api, Using cache objects
Page-level and user control-level output cache is indeed a way to quickly and easily improve the performance of the site, but in the asp.net tutorial, the real flexibility and powerful functions of cache are provided through cache objects. With cache objects, you can store any serializable Data Objects and control the expiration method of cache entries based on the combination of one or more dependencies. These dependencies can include the time since the item was cached, the time since the item was last accessed, changes to files and/or folders, and changes to other cache items, changes to specific tables in the database tutorial can also be included after slight processing.
Store data in cache
The simplest way to store data in a cache is to assign values to a key, just like a hashtable or dictionary object:
Cache ["key"] = "value ";
This method stores items in the cache without any dependencies, so it does not expire unless the cache engine deletes the items to provide space for other cached data. To include specific cache dependencies, you can use the add () or insert () methods. Each method has several reloads. The only difference between add () and insert () Is that add () returns a reference to a cached object, while insert () does not return a value (null in c, in vb ).
Example
Cache. insert ("key", myxmlfiledata, new
System. web. caching. cachedependency (server. mappath ("users. xml ")));
In this example, the xml data in the file can be inserted into the cache, without reading from the file in future requests. Cachedependency is used to ensure that the cache expires immediately after the file is changed, so that the latest data can be extracted from the file and cached again. If the cached data comes from several files, you can also specify an array of file names.
Cache. insert ("dependentkey", mydependentdata, new
System. web. caching. cachedependency (new string [] {}, new string []
{"Key "}));
In this example, the second data block with the key value "key" can be inserted (depending on whether the first data block exists ). If a key named "key" does not exist in the cache, or if the item associated with the key expires or is updated, the cache entry of "dependentkey" expires.
Cache. insert ("key", mytimesensitivedata, null,
Datetime. now. addminutes (1), timespan. zero );
Absolute Expiration: In this example, the cache will be cached for one minute, and the cache will expire after one minute. Note: the absolute expiration and sliding expiration (see below) cannot be used together.
Cache. insert ("key", myfrequentlyaccesseddata, null,
System. web. caching. cache. noabsoluteexpiration,
Timespan. fromminutes (1 ));
Sliding Expiration: This example caches frequently used data. The data will be kept in the cache until it has not been referenced for up to one minute. Note: slide expiration and absolute expiration cannot be used together
The following is a detailed example.
Using system. collections. generic;
Using system. web;
Using system;
Namespace dataaccess
{
/// <Summary>
/// Cache control class
/// </Summary>
Public class cachecontrol
{
Public static list <string> allusecachekey = new list <string> ();
/// <Summary>
/// Add Cache
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
/// <Param name = "absoluteexpiration"> </param>
Public static void addcache (string key, object value, datetime absoluteexpiration)
{
If (! Allusecachekey. contains (key ))
{
Allusecachekey. add (key );
}
Httpcontext. current. cache. add (key, value, null, absoluteexpiration, timespan. zero, system. web. caching. cacheitempriority. normal, null );
}
/// <Summary>
/// Remove cache
/// </Summary>
/// <Param name = "key"> </param>
Public static void removecache (string key)
{
If (allusecachekey. contains (key ))
{
Allusecachekey. remove (key );
}
Httpcontext. current. cache. remove (key );
}
/// <Summary>
/// Clear the cache used
/// </Summary>
Public static void upload AchE ()
{
Foreach (string value in allusecachekey)
{
Httpcontext. current. cache. remove (value );
}
Allusecachekey. clear ();
}
}
}