C#緩衝absoluteExpiration、slidingExpiration兩個參數的疑惑

來源:互聯網
上載者:User

標籤:expires   name   bsp   possible   his   一個   when   var   實現   

看了很多資料終於搞明白cache中absoluteExpiration,slidingExpiration這兩個參數的含義。

absoluteExpiration:用於設定絕對到期時間,它表示只要時間一到就到期,所以類型為System.DateTime,當給這個參數設定了一個時間時,slidingExpiration參數的值就只能為Cache.NoSlidingExpiration,否則出錯;

slidingExpiration:用於設定可調到期時間,它表示當離最後訪問超過某個時間段後就到期,所以類型為System.TimeSpan,當給這個參數設定了一個時間段時,absoluteExpiration的值就只能為Cache.NoAbsoluteExpiration,否則出錯;

     兩個使用執行個體

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(10), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

 

 

  //
        // 摘要:
        //     Inserts an object into the System.Web.Caching.Cache object together with dependencies,
        //     expiration policies, and a delegate that you can use to notify the application
        //     before the item is removed from the cache.
        //
        // 參數:
        //   key:
        //     The cache key that 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 are no dependencies,
        //     this parameter contains null.
        //
        //   absoluteExpiration:
        //     The time at which the inserted object expires and is removed from the cache.
        //     To avoid possible issues with local time such as changes from standard time to
        //     daylight saving time, use System.DateTime.UtcNow instead of System.DateTime.Now
        //     for this parameter value. If you are using absolute expiration, the slidingExpiration
        //     parameter must be set to System.Web.Caching.Cache.NoSlidingExpiration.
        //
        //   slidingExpiration:
        //     The interval between the time that the cached object was last accessed and the
        //     time at which that object expires. If this value is the equivalent of 20 minutes,
        //     the object will expire and be removed from the cache 20 minutes after it was
        //     last accessed. If you are using sliding expiration, the absoluteExpiration parameter
        //     must be set to System.Web.Caching.Cache.NoAbsoluteExpiration.
        //
        //   onUpdateCallback:
        //     A delegate that will be called before the object is removed from the cache. You
        //     can use this to update the cached item and ensure that it is not removed from
        //     the cache.
        //
        // 異常:
        //   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 are both set for the
        //     item you are 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 slidingExpiration, 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#緩衝absoluteExpiration、slidingExpiration兩個參數的疑惑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.