ASP.NET Core 中的緩存


緩存的基本概念

緩存是分布式系統中的重要組件,主要解決高並發,大數據場景下,熱點數據訪問的性能問題。提供高性能的數據快速訪問。

緩存原理

  • 將數據寫入到讀取速度更快的存儲設備;
  • 將數據緩存到離應用最近的位置;
  • 將數據緩存到離用戶最近的位置。

緩存設計

  • 緩存內容 熱點數據,靜態資源
  • 緩存位置 CDN,反向代理,分布式緩存服務器,本機(內存,硬盤)
    • CDN:存放HTML、CSS、JS等靜態資源;
    • 反向代理:動靜分離,只緩存用戶請求的靜態資源;
    • 分布式緩存:緩存數據庫中的熱點數據;
    • 本地緩存:緩存應用字典等常用數據。
  • 過期策略 固定時間,相對時間
  • 同步機制 實時寫入,異步刷新

分布式緩存 Memcache 與 Redis 的比較

  • 數據結構:Memcache只支持key value存儲方式,Redis支持更多的數據類型,比如Key value、hash、list、set、zset;
  • 多線程:Memcache支持多線程,Redis支持單線程;CPU利用方面Memcache優於Redis;
  • 持久化:Memcache不支持持久化,Redis支持持久化(快照和AOF日志兩種持久化方式);
  • 內存利用率:Memcache高,Redis低(采用壓縮的情況下比Memcache高)。使用簡單的key-value存儲的話,Memcached的內存利用率更高,而如果Redis采用hash結構來做key-value存儲,由於其組合式的壓縮,其內存利用率會高於Memcache。
  • 過期策略:Memcache過期后,不刪除緩存,會導致下次取數據數據的問題,Redis有專門線程,清除緩存數據;

緩存穿透,緩存擊穿,緩存雪崩解決方案

  • 緩存穿透
    緩存穿透是指查詢一個一定不存在的數據。由於會頻繁的請求數據庫,對數據庫造成訪問壓力。
    解決方法:
    • 對結果為空的數據也進行緩存,不過設置它的過期時間會很短,最長不超過五分鍾。
    • 一定不存在的key,采用布隆過濾器,建立一個大的Bitmap中,查詢時通過該bitmap過濾。

      布隆過濾器(Bloom Filter)是1970年由布隆提出的。它實際上是一個很長的二進制向量和一系列隨機映射函數。布隆過濾器可以用於檢索一> 個元素是否在一個集合中。它的優點是空間效率和查詢時間都遠遠超過一般的算法,缺點是有一定的誤識別率和刪除困難
      如果想要判斷一個元素是不是在一個集合里,一般想到的是將所有元素保存起來,然后通過比較確定。鏈表,樹等等數據結構都是這種思路. > 但是隨着集合中元素的增加,我們需要的存儲空間越來越大,檢索速度也越來越慢(O(n),O(logn))。不過世界上還有一種叫作散列表(又叫哈> 希表,Hash table)的數據結構。它可以通過一個Hash函數將一個元素映射成一個位陣列(Bit array)中的一個點。這樣一來,我們只要看看這個點是不是1就可以知道集合中有沒有它了。這就是布隆過濾器的基本思想。

  • 緩存雪崩
    緩存雪崩是指在我們設置緩存時采用了相同的過期時間,導致緩存在某一時刻同時失效,請求全部轉發到DB,DB瞬時壓力過重雪崩。
    解決方法:
    • 通過加鎖或者隊列來控制讀數據庫寫緩存的線程數量。比如對某個key只允許一個線程查詢數據和寫緩存,其他線程等待。
    • 分散緩存失效時間,比如在設置過期時間的時候增加一個隨機數盡可能的保證緩存不會大面積的同時失效。
  • 緩存擊穿
    緩存擊穿是指對於一些設置了過期時間的key,如果這些key可能會在過期后的某個時間點被超高並發地訪問。這個和緩存雪崩的區別在於這里針對某一key緩存,前者則是很多key。
    解決方法:
    • 使用互斥鎖來解決問題,通俗的描述就是,一萬個用戶訪問了,但是只有一個用戶可以拿到訪問數據庫的權限,當這個用戶拿到這個權限之后重新創建緩存,這個時候剩下的訪問者因為沒有拿到權限,就原地等待着去訪問緩存。

數據一致性

數據不一致的幾種情況:

  • 數據庫有數據,緩存沒有數據;
  • 數據庫有數據,緩存也有數據,數據不相等;
  • 數據庫沒有數據,緩存有數據。

目前比較常用的數據緩存策略的是Cache Aside Pattern,更新緩存是先把數據存到數據庫中,成功后,再讓緩存失效。
這種策略下不一致產生的原因只有更新數據庫成功,但是刪除緩存失敗。
解決方案:

  1. 對刪除緩存進行重試.
  2. 定期全量更新緩存。
  3. 合理設置緩存過期時間。

使用內置 MemoryCache

ASP.NET Core 支持多種不同的緩存。包括內存緩存,分布式緩存(Redis 和 SQL Server)。Github 開源地址 Libraries for in-memory caching and distributed caching.

IMemoryCache是把數據存儲在Web服務器的內存中。

  1. 在 ConfigureServices 中調用 AddMemoryCache 通過依賴關系注入引用服務。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
    
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
    
  2. 在控制器類中用構造器注入的方式創建 IMemoryCache 的對象。

    using Microsoft.Extensions.Caching.Memory;
    
    public class ValuesController : ControllerBase
    {
        private IMemoryCache _cache;
    
        public ValuesController(IMemoryCache cache)
        {
            _cache = cache;
        }
    }
    
  3. 一些常用操作

    • 創建緩存

      Set()

      DateTime cacheEntry1 = DateTime.Now;
      
      var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(3));
      
      _cache.Set("cache1", cacheEntry1, cacheEntryOptions);
      

      GetOrCreate() GetOrCreateAsync

      var cacheEntry = _cache.GetOrCreate("cache1", entry => 
      {
          entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(3));
          return DateTime.Now;
      });
      
    • 獲取緩存

      Get()

      var cacheEntry = this._cache.Get<DateTime?>("cache1");
      

      TryGetValue()

      DateTime cacheEntry;
      
      if (!_cache.TryGetValue("cache1", out cacheEntry))
      {
          // Key not in cache, so get data.
          cacheEntry = DateTime.Now;
      
          var cacheEntryOptions = new MemoryCacheEntryOptions()
              .SetAbsoluteExpiration(TimeSpan.FromSeconds(3));
      
          _cache.Set("cache1", cacheEntry, cacheEntryOptions);
      }
      
    • 刪除緩存

      Remove()

      _cache.Remove("cache1");
      
  4. 其他知識點

    ICacheEntry成員:

    • Key 緩存key
    • Value 緩存值
    • AbsoluteExpiration 絕對過期時間,為null則條件無效
    • AbsoluteExpirationRelativeToNow 相對當前時間的絕對過期時間(使用TimeSpan),為null條件無效
    • SlidingExpiration 滑動過期時間
    • ExpirationTokens 提供用來自定義緩存過期
    • PostEvictionCallbacks 緩存失效回調
    • Priority 緩存項優先級(在緩存滿載的時候絕對清除的順序)
    • Size 代表緩存數據的大小,在內存緩存中一般為null

    緩存過期的方式

    • 絕對到期(指定在一個固定的時間點到期)
    • 滑動到期(在一個時間長度內沒有被命中則過期,如果命中則順延)
    • 到期Token(自定義過期)

使用分布式緩存 Redis

  1. Nuget 安裝 Microsoft.Extensions.Caching.Redis

  2. ConfigureServices 方法里面添加服務 AddDistributedRedisCache

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDistributedRedisCache(options => {
            options.Configuration = "localhost";
            options.InstanceName = "Instance1";
        });
    
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }
    
  3. 常用操作
    RedisCache 實現了 IDistributedCache 接口,提供了常用的添加、檢索和刪除操作。

    • GetGetAsync 采用字符串鍵並以byte[]形式檢索緩存項(如果在緩存中找到)
    • SetSetAsync 使用字符串鍵向緩存添加項byte[]形式
    • RefreshRefreshAsync 根據鍵刷新緩存中的項,並重置其可調過期超時值(如果有)
    • RemoveRemoveAsync 根據鍵刪除緩存項
    var now = DateTime.Now;
    var cacheValue = System.Text.Encoding.UTF8.GetBytes(now.ToString());
    var options = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(3));
    _cache.Set("cache1", cacheValue, options);
    
    _cache.Refresh("cache1");
    
    var value = _cache.Get("cache1");
    var nowString = System.Text.Encoding.UTF8.GetString(value);
    
    _cache.Remove("cache1");
    

    由於自帶的 RedisCache 繼承 IDistributedCache 接口並沒有提供 Redis的一些高級特性比如Hash, List, Set等。

使用 Stackexchange.Redis 自己封裝一個 RedisHelper 類

基於Stackexchange.Redis封裝一個簡單RedisHelper類:

RedisHelper 類

public class RedisHelper
{
    private readonly RedisOptions _options;

    private readonly Lazy<ConnectionMultiplexer> _connectionMultiplexer;

    public RedisHelper(IOptions<RedisOptions> optionsAccessor)
    {
        if (optionsAccessor == null)
        {
            throw new ArgumentNullException(nameof(optionsAccessor));
        }

        _options = optionsAccessor.Value;
        _connectionMultiplexer = new Lazy<ConnectionMultiplexer>(CreateConnectionMultiplexer);
    }

    public IDatabase GetDatabase()
    {
        return _connectionMultiplexer.Value.GetDatabase();
    }

    private ConnectionMultiplexer CreateConnectionMultiplexer()
    {
        if (_options.ConfigurationOptions != null)
        {
            return ConnectionMultiplexer.Connect(_options.ConfigurationOptions);
        }
        else
        {
            return ConnectionMultiplexer.Connect(_options.Configuration);
        }
    }
}

RedisOptions 配置類

public class RedisOptions : IOptions<RedisOptions>
{
    /// <summary>
    /// The configuration used to connect to Redis.
    /// </summary>
    public string Configuration { get; set; }

    /// <summary>
    /// The configuration used to connect to Redis.
    /// This is preferred over Configuration.
    /// </summary>
    public ConfigurationOptions ConfigurationOptions { get; set; }

    /// <summary>
    /// The Redis instance name.
    /// </summary>
    public string InstanceName { get; set; }

    RedisOptions IOptions<RedisOptions>.Value
    {
        get { return this; }
    }
}

RedisHelperServiceCollectionExtensions 擴展類

public static class RedisHelperServiceCollectionExtensions
{
    public static IServiceCollection AddRedisHelper(this IServiceCollection services, Action<RedisOptions> setupAction)
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }

        if (setupAction == null)
        {
            throw new ArgumentNullException(nameof(setupAction));
        }

        services.AddOptions();
        services.Configure(setupAction);
        services.AddSingleton<RedisHelper>();

        return services;
    }
}

在 ConfigureServices 里面添加服務引用

public void ConfigureServices(IServiceCollection services)
{
    var redisOptions = Configuration.GetSection("RedisOptions").Get<RedisOptions>();
    services.AddRedisHelper(options => {
        options.Configuration = redisOptions.Configuration;
        options.InstanceName = redisOptions.InstanceName;
    });

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

在 Controller 里面使用

public class ValuesController : ControllerBase
{
    private readonly RedisHelper _redisHelper;

    public ValuesController(RedisHelper redisHelper)
    {
        _redisHelper = redisHelper;
    }

    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        _redisHelper.GetDatabase().StringSet("test_key_2", "test_value_2", TimeSpan.FromSeconds(60));
        return new string[] { "value1", "value2" };
    }
}

網上一些開源的Redis擴展:

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM