asp.net USES Cache which resides on the page to cache data that can be regularly updated

  • 2020-05-09 18:24:25
  • OfStack

I want the Web application to run from 1 all the way to the end of 1, and someone said why not use Application? In fact, Cache can update the data automatically within a period of time, while Application cannot do so. In addition, Application must consider the issue of thread safety in a highly concurrent system like Web. Application itself is not thread safe, while Cache is thread safe. So I would like 1 in many objects from Web I only start to run from a database or file access data 1 time, in a different page, are using Cache, and data of the Cache may get into automatic updates, so 1 case do not need to consider the problem of data updating, Cache is also associated with application, all may reside in the Web any page of the application.

, of course, first of all, we should be familiar with 1 next System. Web. Caching. Cache this class, I here also just about using more of a few methods and properties, if you need more details, please refer to MSDN.

It starts with its Add() method, which adds the specified object to the Cache object collection.

The Insert() method will override the Cache tops that have 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.

The main thing I'm talking about here is the Add() method, because we have to know a lot about Cache if we want to persist and automatically replace Cache over a period of time. Let's look at the details of this method in MSDN. Ah.

C#

public Object Add(
string key,
Object value,
CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback
) parameter 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

File or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this parameter contains an nullNothingnullptrnull reference (Nothing in Visual Basic).

absoluteExpiration Type: System DateTime

The time at which the added object will expire and be removed from the cache. If the tunable expires, the absoluteExpiration parameter must be NoAbsoluteExpiration.

slidingExpiration Type: System TimeSpan

The time interval between the last access to the added object and the expiration of the object. If the value is equivalent to 20 minutes, the object will expire and be removed from the cache 20 minutes after the last time it was accessed. 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. Use this value when caching exits an object; Objects with lower costs are removed from the cache before those with higher costs.

onRemoveCallback Type: System. Web. Caching CacheItemRemovedCallback

The delegate (if provided) that is invoked when an object is removed from the cache. It can be used to notify an application when an application object is removed from the cache.

The return value

Type: System Object

note

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

The absoluteExpiration and slidingExpiration parameters cannot be set simultaneously. If you want the cache entry to expire at a specific time, set the absoluteExpiration parameter to a specific time and the slidingExpiration parameter to NoSlidingExpiration.

If you want the cache entry to expire after some time after the last access to the item, set the slidingExpiration parameter to the expiration interval and the absoluteExpiration parameter to NoAbsoluteExpiration.

This paragraph is reproduced from MSDN

In the case of 1, I will add the cache to the Init or Load events on the master page, because this ensures that the reference program will be able to cache objects that need to be used frequently from 1.

protected void Page_Load(object sender, EventArgs e)
{
      Cache. Add (" key."                     // need to add the key to Cache
            new {value=" add value "},         // corresponding value
              null,                                   // cache dependencies.
            DateTime Now AddMinutes(1),// fixed cache time
              System. Web. Caching. Cache. NoSlidingExpiration, / / but to delay the cache time,
              System. Web. Caching. CacheItemPriority. NotRemovable, / / cache the priority.
              new System. Web. Caching. CacheItemRemovedCallback (OnMoveCacheBack)); The callback function called when // is removed
}

public void OnMoveCacheBack(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)
{
      if (Cache[key] != null)
      {
              Cache.Remove(key);
      }
      Cache. Add (" key."                       // need to be added to Cache
            new {value = "update value"},           // corresponding value
              null,                           // cache dependencies.
            Now.AddMinutes (1),// fixed cache time
              System. Web. Caching. Cache. NoSlidingExpiration, / / but to delay the cache time,
              System. Web. Caching. CacheItemPriority. NotRemovable, / / cache the priority.
              new System. Web. Caching. CacheItemRemovedCallback (OnMoveCacheBack)); The callback function called when // is removed
}

The specific use of parameters 1 must pay attention to 3 points,

The first is that the cache dependency 1 must be specified as null.

Second, the fixed expiration cache time cannot be specified at the same time as the delayed cache time. If the data is changed regularly as I said, of course, the fixed expiration cache time will be used.

3 is the priority of the cache, this is the key, specified as much as System. Web. Caching. CacheItemPriority. NotRemovable enumeration values, so as not to be automatically withdraw, but one must pay attention to the size of the cache.


Related articles: