Application Data Cache

Source: Internet
Author: User

Application Program The main function of data caching is to store various application-related objects in the memory. It is mainly implemented by the cache class, which belongs to the system. web. the Caching namespace. Its instance object is dedicated to each application. Through cache applications, you can easily add, retrieve, and remove Application Data caches, and notify the application when a cache item is removed.
Add an application data cache object
Three ways to add items to the application cache: 1. add items to the cache by setting keys and values. 2. use the insert method to add items to the cache. 3. use the add method to add items to the cache.
1. Add items to the cache using keys and values.
The following Code In the example, add an item named cacheitem1 to the cache object:
Cache ["cacheitem1"] = "cached Item 1 ";
2. add items to the cache by using the insert method
The cache. insert method can be reloaded in four ways. The following uses the sample code to describe the insert method.
Overload 1: Public void insert (string key, object value)
Insert an item to the cache object. This item carries a cache key to reference its location and uses the default value provided by cacheitempriority enumeration.
Parameter: Key: the cache key used to reference this item.
Value: the object to be inserted into the cache.
Sample Code: cache. insert ("DSN", connectionstring );
Overload 2: Public void insert (string key, object value, cachedependency dependencies)
Insert objects with file or key dependencies into the cache.
Parameter: Key: the cache key used to identify the item.
Value: the object to be inserted into the cache.
Dependencies: The file dependency or cache key dependency of the inserted object. When any dependency item is changed, the object is invalid and removed from the cache. If there is no dependency, this parameter contains a null reference.
Sample Code: cache. insert ("DSN", connectionstring, new cachedependency (server. mappath ("myconfig. xml ")));
Overload 3: Public void insert (string key, object value, cachedependency dependencies, datetime absoluteexpiration, timespan slidingexpiration)
Insert objects with dependency and expiration policies to the cache.
Parameters: Key: The cache key used to reference this object.
Value: The object to be inserted into the cache.
Dependencies: The file dependency or cache key dependency of the inserted object. When any dependency item is changed, the object is invalid and removed from the cache. If there is no dependency, this parameter contains a null reference.
Absoluteexpiration: The time when the inserted object expires and is removed from the cache. If an absolute expiration time is used Slidingexpiration The parameter must be noslidingexpiration.
Slidingexpiration: The interval between the last access to the inserted object and the expiration time of the object. If this value is equivalent to 20 minutes, the object will expire and be removed from the cache 20 minutes after the last access. If you use an adjustable expiration timeAbsoluteexpiration The parameter must be noabsoluteexpiration.
Sample Code: cache. insert ("DSN", connectionstring, null, datetime. Now. addminutes (2), timespan. Zero );
Overload 4: Public void insert (string key, object value, cachedependency dependencies, datetime absoluteexpiration, timespan slidingexpiration, cacheitempriority priority, cacheitemremovedcallback onremovecallback)
Insert an object to the cache object. The latter has a dependency, expiration and priority policy, and a delegate (which can be used to notify the application when the inserted item is removed from the cache ).
Parameter: Key: the cache key used to reference this object.
Value: the object to be inserted into the cache.
Dependencies: The file dependency or cache key dependency of the item. When any dependency item is changed, the object is invalid and removed from the cache. If there is no dependency, this parameter contains a null reference.
Absoluteexpiration: the time when the inserted object expires and is removed from the cache. If absolute expiration is used, the slidingexpiration parameter must be noslidingexpiration.
Slidingexpiration: The interval between the last access to the inserted object and the expiration time of the object. If this value is equivalent to 20 minutes, the object will expire and be removed from the cache 20 minutes after the last access. If you use callable expiration, the absoluteexpiration parameter must be noabsoluteexpiration.
priority: the cost of the object relative to other items stored in the cache, represented by cacheitempriority enumeration. This value is used when the cache exits the object. Objects with lower costs are removed from the cache before they have higher costs.
Onremovecallback: The delegate called when the object is removed from the cache (if provided ). When you delete an application object from the cache, you can use it to notify the application.
Sample Code: cache. insert ("DSN", connectionstring, null, datetime. Now. addminutes (2), timespan. Zero, cacheitempriority. High, onremove );
3. Use the add method to add items to the cache
Public object add (string key, object value, cachedependency dependencies, datetime absoluteexpiration,
Timespan slidingexpiration, cacheitempriority priority, cacheitemremovedcallback onremovecallback)
Parameter: Key: the cache key used to reference this item.
Value: the item to be added to the cache.
Dependencies: The file dependency or cache key dependency of the item. When any dependency item is changed, the object is invalid and removed from the cache. If there is no dependency, this parameter contains a null reference.
Absoluteexpiration: the time when the added object expires and is removed from the cache. If you use callable expiration, the absoluteexpiration parameter must be noabsoluteexpiration.
Slidingexpiration: The interval between the last access to the added object and the expiration time of the object. If this value is equivalent to 20 minutes, the object will expire and be removed from the cache 20 minutes after the last access. If absolute expiration is used, the slidingexpiration parameter must be noslidingexpiration.
Priority: the relative cost of the object, represented by cacheitempriority enumeration. This value is used when the cache exits the object. Objects with lower costs are removed from the cache before they have higher costs.
Onremovecallback: The delegate called when the object is removed from the cache (if provided ). When you delete an application object from the cache, you can use it to notify the application.

Sample Code
PublicObject add (
StringKey,
Object value,
Cachedependency dependencies,
Datetime absoluteexpiration,
Timespan slidingexpiration,
Cacheitempriority priority,
Cacheitemremovedcallback onremovecallback
)

Comparison between the add method and the insert method: the return value of the insert method is null. The add method returns the Data Object of the cache item. The insert method has four overload methods, which are more flexible; if an item with the same name as an existing item already exists in the cache, the insert method replaces this item, while the add method does not replace this item and does not cause an exception.

Retrieve the value of a cache item
1. Specify the key name to retrieve the specified item
Check the cache object to determine whether the item is not null (nothing in Visual Basic ). If this option exists, assign it to the variable. Otherwise, re-create the item, add it to the cache, and then access it.
The following code example shows how to retrieve an item named cacheitem from the cache. The Code assigns the content of this item to the variable named cachedstring. If this item is not in the cache, the Code adds it to the cache and assigns it to cachedstring. Sample Code
String Cachedstring;
Cachedstring = ( String ) Cache [ " Cacheitem " ];
If (Cachedstring =   Null )
{
Cachedstring= "Hello, world.";
Cache. insert ("Cacheitem", Cachedstring );
}

2. Use the get method to retrieve a specified item
Public object get (string key)
Parameter: Key: The identifier of the cache item to be retrieved.
Return Value: The retrieved cache item. If this key is not found, it is null.
Sample Code: cache. Get ("mytextbox. Value ");

3. Use the getenumerator method to retrieve a specified item
The getenumerator method is used to retrieve the dictionary enumeration numbers used to cyclically access key settings and their values contained in the cache.
Public idictionaryenumerator getenumerator ()
The returned value is the number of enumerations that need to be cyclically accessed to the cache object. This method can add items to the cache or remove items from the cache when enumerating all items.
The following example uses the getenumerator method to create an idictionaryenumerator object cacheenum. The enumerative number runs once throughout the cache, converts the values of each cache item into strings, and then writes these values to the "Web form" page.

Sample Code
Idictionaryenumerator cacheenum = Cache. getenumerator ();
While (Cacheenum. movenext ())
{
Cacheitem=Server. htmlencode (cacheenum. Current. tostring ());
Response. Write (cacheitem );
}

Delete items from ASP. NET Cache
Data in the ASP. NET cache is easy to lose, that is, it cannot be stored permanently. Data in the cache may be automatically removed for any of the following reasons:
The cache is full.
This item has expired.
The dependency is changed.

Explicitly delete an item from the cache
Call the Remove Method to pass the key of the item to be removed.
The following example shows how to remove an entry whose key is mydata1.
Cache. Remove ("mydata1 ");

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.