cache operation code

Source: Internet
Author: User
Tags datetime httpcontext

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 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 ).


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 ();

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: 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.