ASP. NET data cache (application cache)

Source: Internet
Author: User

application caching is used to store application-related objects, primarily by the cacheclass to implement.
named controls: Using System.Web.Caching;
Add Cache
1, specifying keys and values
cache["key"] = "value"; This is the easiest way to add a cache, but it becomes powerless if you need to set the cache's expiration date, dependencies, and so on.
2, using the Add ()Method--used only in ASP .Add a new cache entry in the cache (If you overwrite an existing cache entry with it, it will fail)。
Cache.Add ("Key", "value", CACHEDEPENDENCY,DATETIME,TIMESPAN,CACHEITEMPRIORITY,CACHEITEMREMOVEDCALLBAC);
This method is useful for caches that need to set the cache's expiration date, dependencies, and so on, but be aware that it's 7parameters must be fully written to take effect, so flexibility is not good enough.
parameter Description:
first parameter: key--represents a key that references a cached object (key)
second parameter: value--represents the object to be cached (value)
third parameter: cachedependency--represents the addition of a dependency (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)
Fourth parameter: datetime--represents an absolute expiration time (indicates when the cached data expires)
Fifth parameter: timespan--indicates that the adjustable expiration time is also called the elastic expiration time. Indicates how long cached data can be retained in the cache after the last access (In other words, the relative expiration time is the starting point of the relative expiration time and the remaining point of time. TimeSpan.Zeroindicates that no adjustable expiration time is specified) ()
Sixth parameter: cacheitempriority--represents a priority, removing those low-priority data when the cache is filled
Seventh parameter: cacheitemremovedcallbac--represents a callback custom method (Example: You can require notification when a cache entry is removed from the cache), it is of the type CacheItemRemovedCallbacktype of

Example One: (Specify a 5minutes Absolute Expiration time)
Cache.Add ("Key", "value", Null,datetime.now.addminutes (5), timespan.zero,cacheitempriority.normal,null); //creates a specified 5the cache of the minute absolute expiration time

Example two: (Specify a 5The elastic expiration time of the minute, no absolute expiration is specified)
Cache.Add ("Key", "value", Null,datetime.maxvalue,timespan.fromminutes (5), cacheitempriority.normal,null); //creates a specified 5the cache of minute elastic expiration time
Example Three: (expiration time also depends on the modification of a file)
//expiration time depends on Test.xmlfile modification. Which means when Test.xmlThis cache expires when file content is modified
CacheDependency dependency = new CacheDependency ("C:\\test.xml");
//Create a cache that relies on file modification and expires
Cache.Add ("Key", "value", Dependency,datetime.maxvalue,timespan.fromminutes (5), cacheitempriority.normal,null);
Example four: (the expiration time depends on the modification of another item in the cache)
Cache.Add ("Key1", "value", null,datetime.maxvalue,timespan.zero,cacheitempriority.normal,null); //when the cache Key1when the value is modified, the cache Key2it will expire.
//Add love to the array
string[] Dependencykeys = new String[1];
Dependencykeys[0] = "Key1";
CacheDependency dependency = new CacheDependency (null, Dependencykeys);
//Create a cache that relies on another cache modification that expires
Cache.Add ("Key2", "value", dependency,datetime.maxvalue,timespan.zero,cacheitempriority.normal,null);
Example five: (callback method after cache expiration)
public void Itemremovedcallback (String key, Object value, CacheItemRemovedReason reason) {}//the method that the cache expires will be called
//Create a 5 when specifiedThe elastic expiration time of the minute is called after ItemremovedcallbackCaching of methods
Cache.Add ("Key", "value", Null,datetime.maxvalue,timespan.fromminutes (5), cacheitempriority.normal,new CacheItemRemovedCallback (this. Itemremovedcallback));

Priority value Order

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 normal for the priority level or the following cache entries are reserved more preferentially

Normal

cache entries that are set to this priority are belownormal than the priority level and Low cache entries are reserved more preferentially

BelowNormal

This is the priority of the second-to-last level; The cache entry for this priority is set to low only for the priority level cache entries are reserved more preferentially

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 the ASP. do not remove it from the cache, even if it is low on memory

3.Use the Insert () Method-- to overwrite existing cache entries in the ASP. This approach can be implemented in a variety of ways, using a flexible approach ( but the main usage and Cache.Add usage is good) .

Cache.Insert ("Key", "value");


Example One: (key Value Added)
Cache.Insert ("Key1", "value1");
Example two: (Add a cache of dependencies)
Cache.Insert ("Key", "value", System.Web.Caching.CacheDependecy (null,dependencies));
Example Three: (add a valid expiration time)
Cache.Insert ("Key", "value", DateTime.Now.AddMinutes (1d), System.Web.Caching.Cache.NoSlidingExpiration);
Example four: (add a priority cache)
Cache.Insert ("Key", "value", Null,system.web.caching.cache.noslidingexpiration, System.web.caching.cache.noslidingexpiration,system.web.caching.cacheitempriority.high,null);
4, retrieve cache
if (cache["key"]! = NULL)
{    //Processing statements}
5, remove cache
(1)Automatic Removal
cache entries are automatically removed when a cache is full, expired, or a dependency change occurs
(2)Show Removal
Cache.remove ("key");

ASP. NET data cache (application cache)

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.