C # cache absoluteexpiration, slidingexpiration two parameters of doubt

Source: Internet
Author: User

After reading a lot of data, I finally figured out the meaning of the two parameters in the cache absoluteexpiration,slidingexpiration.

Absoluteexpiration: Used to set an absolute expiration time, which means that as soon as the time expires, So the type is System.DateTime, when the parameter is set to a time, the value of the slidingexpiration parameter can only be cache.noslidingexpiration, otherwise error;

SlidingExpiration: Used to set the adjustable expiration time, which indicates that it expires after a certain period of time from the last access. So the type is System.TimeSpan, when the parameter is set a time period, the value of absoluteexpiration can only be cache.noabsoluteexpiration, otherwise error;

Two usage instances

Cache.Add ("name", content, NULL, System.Web.Caching.Cache.NoAbsoluteExpiration, Timespan.fromminutes (10), System.Web.Caching.CacheItemPriority.Normal, NULL);

Cache.Add ("name", content, NULL, DateTime.Now.AddMinutes (Ten), System.Web.Caching.Cache.NoSlidingExpiration, Cacheitempriority.normal, NULL);

//
Summary:
Inserts an object to the System.Web.Caching.Cache object together with dependencies,
Expiration policies, and a delegate that's can use to notify the application
Before the item is removed from the cache.
//
Parameters:
Key
The cache key is used to reference the object.
//
Value
The object to insert into the cache.
//
Dependencies
The file or cache key dependencies for the item. When any dependency changes,
The object becomes invalid and is removed from the cache. If There is no dependencies,
This parameter contains null.
//
Absoluteexpiration:
The time at which the inserted object expires and was removed from the cache.
To avoid possible issues with local time such as changes from
Daylight saving time, use System.DateTime.UtcNow instead of System.DateTime.Now
For this parameter value. If you is using absolute expiration, the slidingexpiration
Parameter must is set to System.Web.Caching.Cache.NoSlidingExpiration.
//
SlidingExpiration:
The interval between the time that the cached object is last accessed and the
Time at which this object expires. If This value is the equivalent of minutes,
The object would expire and be removed from the cache, minutes after it is
Last accessed. If you is using sliding expiration, the absoluteexpiration parameter
Must is set to System.Web.Caching.Cache.NoAbsoluteExpiration.
//
Onupdatecallback:
A delegate that'll be called before the object was removed from the cache. You
Can use this to update the cached item and ensure that it's not a removed from
The cache.
//
Abnormal:
T:System.ArgumentNullException:
The key, value, or onupdatecallback parameter is null.
//
T:System.ArgumentOutOfRangeException:
You set the slidingexpiration parameter to less than TimeSpan.Zero or the equivalent
Of more than one year.
//
T:System.ArgumentException:
The absoluteexpiration and slidingexpiration parameters is both set for the
Item trying to add to the Cache.-or-the dependencies parameter is NULL,
And the absoluteexpiration parameter is set to System.Web.Caching.Cache.NoAbsoluteExpiration,
And the slidingexpiration parameter is set to System.Web.Caching.Cache.NoSlidingExpiration.
public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteexpiration, TimeSpan slidinge Xpiration, Cacheitemupdatecallback onupdatecallback);

using System;

using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Caching; namespace HuaTong.General.Utility {      /// <summary>      /// 缓存操作,默认缓存1分钟      /// </summary>      public static class CacheHelper      {          static int cacheTime = 1;          /// <summary>          /// 读取缓存项          /// </summary>          /// < returns ></ returns >          public static object CacheReader(string cacheKey)          {              return HttpRuntime.Cache[cacheKey];          }          /// <summary>          /// 写入缓存项          /// </summary>          public static void CacheWriter(string cacheKey, object cacheValue,  int cache_time = 0)          {              HttpRuntime.Cache. Insert (cacheKey, cacheValue,  null ,                  DateTime.Now.AddMinutes(cache_time <= 0 ? cacheTime : cache_time),                  Cache.NoSlidingExpiration);          }          /// <summary>          /// 移除指定缓存项          /// </summary>          public static void CacheRemove(string cacheName)          {              HttpRuntime.Cache.Remove(cacheName);          }          /// <summary>          /// 缓存对象泛型实现          /// </summary>          public static T ObjectReader<T>(string cacheKey =  null )              where T : class          {              string cachekey = typeof(T).GetHashCode() + StringHelper.ToString(cacheKey);              var obj = CacheReader(cachekey)  as T;              return obj;          }          /// <summary>          /// 缓存对象泛型实现          /// </summary>          public static void ObjectWriter<T>(T cacheValue, string cacheKey =  null int cache_time = 0)              where T : class          {              string cachekey = typeof (T).GetHashCode() + StringHelper.ToString(cacheKey);              CacheWriter(cachekey, cacheValue, cache_time);          }      } }

C # cache absoluteexpiration, slidingexpiration two parameters of doubt

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.