乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      NetCore MemoryCache使用

       路人甲Java 2020-04-29

      引用類庫

      1.Install-Package Microsoft.Extensions.Caching.Memory

      MemoryCacheOptions 緩存配置

      1.ExpirationScanFrequency 獲取或設(shè)置對(duì)過期項(xiàng)的連續(xù)掃描之間的最短時(shí)間間隔

      2.SizeLimit 緩存是沒有大小的的,此值設(shè)置緩存的份數(shù)

      3.CompactionPercentage 獲取或設(shè)置在超過最大大小時(shí)壓縮緩存的數(shù)量,優(yōu)先壓縮優(yōu)先級(jí)較低的緩存,0.2代表20%

        services.AddMemoryCache(options => {
                      // 緩存最大為100份
                      //##注意netcore中的緩存是沒有單位的,緩存項(xiàng)和緩存的相對(duì)關(guān)系
                      options.SizeLimit = 2;
                      //緩存滿了時(shí)候壓縮20%的優(yōu)先級(jí)較低的數(shù)據(jù)
                      options.CompactionPercentage = 0.2;
                      //兩秒鐘查找一次過期項(xiàng)
                      options.ExpirationScanFrequency = TimeSpan.FromSeconds(2);
                  });

       

       

       

      MemoryCacheEntryOptions 單個(gè)緩存項(xiàng)配置

      1.AbsoluteExpiration 絕對(duì)過期時(shí)間

      2. AbsoluteExpirationRelativeToNow 相對(duì)于現(xiàn)在的絕對(duì)過期時(shí)間

      3.SlidingExpiration 滑動(dòng)過期時(shí)間,在時(shí)間段范圍內(nèi) 緩存被再次訪問,過期時(shí)間將會(huì)被重置

      4.Priority 優(yōu)先級(jí)

      5.Size 緩存份數(shù)

       public bool Add(string key, object value, int ExpirtionTime = 20)
              {
                  if (!string.IsNullOrEmpty(key))
                  {
                      MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions()
                      {
                          //滑動(dòng)過期時(shí)間 20秒沒有訪問則清除
                          SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime),
                          //設(shè)置份數(shù)
                          Size = 1,
                          //優(yōu)先級(jí)
                          Priority = CacheItemPriority.Low,
                      };
                      //過期回掉
                      cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) =>
                      {
                          Console.WriteLine($"回調(diào)函數(shù)輸出【鍵:{keyInfo},值:{valueInfo},被清除的原因:{reason}】");
                      });
                      _cache.Set(key, value, cacheEntityOps);
                  }
                  return true;
              }

      完整代碼

      1.接口

        public interface ICacheService
          {
              /// <summary>
              ///  新增
              /// </summary>
              /// <param name="key"></param>
              /// <param name="value"></param>
              /// <param name="ExpirtionTime"></param>
              /// <returns></returns>
              bool Add(string key, object value, int ExpirtionTime = 20);
      
      
              /// <summary>
              /// 獲取
              /// </summary>
              /// <param name="key"></param>
              /// <returns></returns>
              string GetValue(string key);
              /// <summary>
              /// 驗(yàn)證緩存項(xiàng)是否存在
              /// </summary>
              /// <param name="key">緩存Key</param>
              /// <returns></returns>
              bool Exists(string key);
      
              /// <summary>
              /// 移除
              /// </summary>
              /// <param name="key"></param>
              /// <returns></returns>
              bool Remove(string key);
          }

      2. 實(shí)現(xiàn)  ICacheService

      /// <summary>
          /// 緩存接口實(shí)現(xiàn)
          /// </summary>
          public class MemoryCacheService : ICacheService
          {
              protected IMemoryCache _cache;
      
              public MemoryCacheService(IMemoryCache cache)
              {
                  _cache = cache;
              }
      
              public bool Add(string key, object value, int ExpirtionTime = 20)
              {
                  if (!string.IsNullOrEmpty(key))
                  {
                      MemoryCacheEntryOptions cacheEntityOps = new MemoryCacheEntryOptions()
                      {
                          //滑動(dòng)過期時(shí)間 20秒沒有訪問則清除
                          SlidingExpiration = TimeSpan.FromSeconds(ExpirtionTime),
                          //設(shè)置份數(shù)
                          Size = 1,
                          //優(yōu)先級(jí)
                          Priority = CacheItemPriority.Low,
                      };
                      //過期回掉
                      cacheEntityOps.RegisterPostEvictionCallback((keyInfo, valueInfo, reason, state) =>
                      {
                          Console.WriteLine($"回調(diào)函數(shù)輸出【鍵:{keyInfo},值:{valueInfo},被清除的原因:{reason}】");
                      });
                      _cache.Set(key, value, cacheEntityOps);
                  }
                  return true;
              }
      
              public bool Remove(string key)
              {
                  if (string.IsNullOrEmpty(key))
                  {
                      return false;
                  }
                  if (Exists(key))
                  {
                      _cache.Remove(key);
                      return true;
                  }
                  return false;
              }
      
              public string GetValue(string key)
              {
                  if (string.IsNullOrEmpty(key))
                  {
                      return null;
                  }
                  if (Exists(key))
                  {
                      return _cache.Get(key).ToString();
                  }
                  return null;
              }
      
              public bool Exists(string key)
              {
                  if (string.IsNullOrEmpty(key))
                  {
                      return false;
                  }
      
                  object cache;
                  return _cache.TryGetValue(key, out cache);
              }
      
          }

      大神貼1:https://www.cnblogs.com/mylinx/p/10443494.html

      大神貼2:https://www.cnblogs.com/wyy1234/p/10519681.html#_label1_0

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多