asp.net uses cache caching that resides in the page to regularly update data _ practical Tips

Source: Internet
Author: User

Just want to have the Web application from the beginning of the run to the end of the existence, and some people say why not application it? In fact, the cache can automatically update the data for a period of time, and application can't do this, and the other application in the web such a high concurrency system must consider the issue of thread safety, application itself is not thread-safe, and cache is thread safe. So generally I will be in many objects I only from the web when the start of the run from the database or file to get a data, in different pages, are used cache, and the cache data may be automatically updated, so the general situation does not need to consider the issue of data updates, Additionally, the cache is also associated with applications, all of which may reside on any page in the Web application.

Of course, first of all, we should familiarize ourselves with the System.Web.Caching.Cache class, and I'll just say that it uses a few more methods and attributes, please consult MSDN if you need more detailed description.

The Add () method is first introduced to add the specified object to the Cache object collection.

The Insert () method overwrites the cache top with the same key.

Remove () Removes the specified item from the application's cache object.

Count property to get the number of objects stored in the cache.

My main point here is the Add () method, because if we want to have a cache for a long time and automatically replace the cache for a while, we must know it very well. Let's look at MSDN for a detailed introduction to this approach. Oh.

C#

Public Object Add (
String key,
Object value,
CacheDependency dependencies,
DateTime absoluteexpiration,
TimeSpan slidingexpiration,
CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback
)

Parameters

Key
Type: System.String

The cache key used to reference the item.

Value
Type: System.Object

The item to add to the cache.

Dependencies
Type: System.Web.Caching.CacheDependency

The file dependency or cache key dependencies for the item. When any dependency changes, the object is invalid and removed from the cache. If there are no dependencies, this parameter contains a nullnothingnullptrnull reference (nothing in Visual Basic).

Absoluteexpiration
Type: System.DateTime

The time that the added object will expire and be removed from the cache. If you use a tunable expiration, the absoluteexpiration parameter must be noabsoluteexpiration.

SlidingExpiration
Type: System.TimeSpan

The time interval between when the added object was last accessed and when the object expired. If the value is equivalent to 20 minutes, the object expires and is removed from the cache after the last 20 minutes of access. If absolute expiration is used, the slidingexpiration parameter must be noslidingexpiration.

Priority
Type: System.Web.Caching.CacheItemPriority

The relative cost of the object, represented by the CacheItemPriority enumeration. The cache uses this value when exiting the object, and objects with lower costs are removed from the cache before objects with higher costs.

onRemoveCallback
Type: System.Web.Caching.CacheItemRemovedCallback

The delegate that is invoked when an object is removed from the cache, if provided. When you remove an application's object from the cache, you can use it to notify the application.

return value

Type: System.Object

Note

If an item with the same key parameter has been saved in the Cache, the call to this method will fail. To overwrite an existing Cache entry with the same key parameter, use the Insert method.

The absoluteexpiration and slidingexpiration parameters cannot be set at the same time. If you want the cached item to expire at a specific time, set the absoluteexpiration parameter to a specific time and set the slidingexpiration parameter to noslidingexpiration.

If you want the cached item to expire after a certain period of time after the last visit to the item, set the slidingexpiration parameter to the expiration interval and set the absoluteexpiration parameter to noabsoluteexpiration.

This section is reproduced from MSDN

Specifically how to add this cache, in general I will add the cache to the Init event or Load event in the master page, because it guarantees that the reference program can cache objects that need to be used frequently from the very beginning.

Protected void Page_Load (object sender, EventArgs e)
{
    cache.add ("Key",                //keys to add to cache
         New {value= "add value"},     //corresponding value
         null,                       //cache dependencies.
        DateTime.Now.AddMinutes (1),//fixed cache time
         System.Web.Caching.Cache.NoSlidingExpiration,//available to delay cache time,
         System.Web.Caching.CacheItemPriority.NotRemovable,//cache priority.
        New System.Web.Caching.CacheItemRemovedCallback (Onmovecacheback)) ;//callback function called when removed
}

public void Onmovecacheback (string key, object value, System.Web.Caching.CacheItemRemovedReason reason)
{
if (Cache[key]!= null)
{
Cache.remove (key);
}
Cache.Add ("Key",//keys to be added to cache)
New {value = ' update value '},//corresponding value
NULL,//cache dependencies.
DateTime.Now.AddMinutes (1),//fixed cache time
System.Web.Caching.Cache.NoSlidingExpiration,//available to delay cache time,
System.Web.Caching.CacheItemPriority.NotRemovable,//priority in cache.
New System.Web.Caching.CacheItemRemovedCallback (Onmovecacheback));//callback function called when removed
}

The specific use of the parameters must pay attention to three points,

The first is that cached dependencies must be specified as null.

The second fixed expiration cache time can not and can be to the time delay cache time, the implementation of my said periodic change of data, of course, the use of fixed expiration cache time.

The third is the priority of the cache, which is also more critical, be sure to specify the System.Web.Caching.CacheItemPriority.NotRemovable enumeration value, so that it will not be automatically retracted, but be sure to note the size of the 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.