前言
本來昨天應該更新的,但是由於各種原因,抱歉,讓追這個系列的朋友久等了。上一篇文章 在.Net Core 使用緩存和配置依賴策略 講的是如何使用本地緩存,那么本篇文章就來了解一下如何使用分布式緩存,通過本章,你將了解到如何使用分布式緩存,以及最重要的是,如何選擇適合自己的分布式緩存;本章主要包含兩個部分:
內容提要
- 使用 SqlServer 分布式緩存
- 使用 Redis 分布式緩存
- 實現自定義的分布式緩存客戶端注冊擴展
- 關於本示例的使用說明
1. 使用 SqlServer 分布式緩存
1.1 准備工作,請依照以下步驟實施
- 1 創建一個 Asp.Net Core MVC 測試項目:Ron.DistributedCacheDemo
- 2 為了使用 SqlServer 作為分布式緩存的數據庫,需要在項目中引用 Microsoft.EntityFrameworkCore 相關組件
- 3 在 SqlServer 數據庫引擎中創建一個數據庫,命名為:TestDb
- 4 打開 Ron.DistributedCacheDemo 項目根目錄,執行創建緩存數據表的操作,執行命令后如果輸出信息:Table and index were created successfully. 表示緩存表創建成功
dotnet sql-cache create "Server=.\SQLEXPRESS;User=sa;Password=123456;Database=TestDb" dbo AspNetCoreCache
1.2 開始使用 SqlServer 分布式緩存
.Net Core 中的分布式緩存統一接口是 IDistributedCache 該接口定義了一些對緩存常用的操作,比如我們常見的 Set/Get 方法,而 SqlServer 分布式緩存由 SqlServerCache 類實現,該類位於命名空間 Microsoft.Extensions.Caching.SqlServer 中
- 在 Startup.cs 中注冊分布式緩存
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedSqlServerCache(options =>
{
options.SystemClock = new BLL.LocalSystemClock();
options.ConnectionString = this.Configuration["ConnectionString"];
options.SchemaName = "dbo";
options.TableName = "AspNetCoreCache";
options.DefaultSlidingExpiration = TimeSpan.FromMinutes(1);
options.ExpiredItemsDeletionInterval = TimeSpan.FromMinutes(5);
});
...
}
上面的方法 ConfigureServices(IServiceCollection services) 中使用 services.AddDistributedSqlServerCache() 這個擴展方法引入了 SqlServer 分布式緩存,並作了一些簡單的配置,該配置是由 SqlServerCacheOptions 決定的,SqlServerCacheOptions 的配置非常重要,這里強烈建議大家手動配置
1.3 了解 SqlServerCacheOptions,先來看一下SqlServerCacheOptions 的結構
namespace Microsoft.Extensions.Caching.SqlServer
{
public class SqlServerCacheOptions : IOptions<SqlServerCacheOptions>
{
public SqlServerCacheOptions();
// 緩存過期掃描時鍾
public ISystemClock SystemClock { get; set; }
// 緩存過期逐出時間,默認為 30 分鍾
public TimeSpan? ExpiredItemsDeletionInterval { get; set; }
// 緩存數據庫連接字符串
public string ConnectionString { get; set; }
// 緩存表所屬架構
public string SchemaName { get; set; }
// 緩存表名稱
public string TableName { get; set; }
// 緩存默認過期時間,默認為 20 分鍾
public TimeSpan DefaultSlidingExpiration { get; set; }
}
}
該配置非常簡單,僅是對緩存使用的基本配置
首先,使用 options.SystemClock 配置了一個本地時鍾,接着設置緩存過期時間為 1 分鍾,緩存過期后逐出時間為 5 分鍾,其它則是連接數據庫的各項配置
在緩存過期掃描的時候,使用的時間正是 options.SystemClock 該時鍾的時間,默認情況下,該時鍾使用 UTC 時間,在我的電腦上,UTC 時間是得到的是美國時間,所以這里實現了一個本地時鍾,代碼非常簡單,只是獲取一個本地時間
public class LocalSystemClock : Microsoft.Extensions.Internal.ISystemClock
{
public DateTimeOffset UtcNow => DateTime.Now;
}
1.4 在控制器中使用分布式緩存
- 首先使用依賴注入,在 HomeController 中獲得 IDistributedCache 的實例對象,該實例對象的實現類型為 SqlServerCache,然后通過 Index 方法增加一項緩存 CurrentTime 並設置其值為當前時間,然后再另一接口 GetValue 中取出該 CurrentTime 的值
[Route("api/Home")]
[ApiController]
public class HomeController : Controller
{
private IDistributedCache cache;
public HomeController(IDistributedCache cache)
{
this.cache = cache;
}
[HttpGet("Index")]
public async Task<ActionResult<string>> SetTime()
{
var CurrentTime = DateTime.Now.ToString();
await this.cache.SetStringAsync("CurrentTime", CurrentTime);
return CurrentTime;
}
[HttpGet("GetTime")]
public async Task<ActionResult<string>> GetTime()
{
var CurrentTime = await this.cache.GetStringAsync("CurrentTime");
return CurrentTime;
}
}
- 運行程序,打開地址:http://localhost:5000/api/home/settime,然后查看緩存數據庫,緩存項 CurrentTime 已存入數據庫中
- 訪問接口:http://localhost:5000/api/home/gettime 得到緩存項 CurrentTime 的值
- 等到超時時間過期后,再到數據庫查看,發現緩存項 CurrentTime 還在數據庫中,這是因為緩存清理機制造成的
1.5 緩存清理
在緩存過期后,每次調用 Get/GetAsync 方法都會 調用 SqlServerCache 的 私有方法 ScanForExpiredItemsIfRequired() 進行一次掃描,然后清除所有過期的緩存條目,掃描方法執行過程也很簡單,就是直接執行數據庫查詢語句
DELETE FROM {0} WHERE @UtcNow > ExpiresAtTime
值得注意的是,在異步方法中使用同步調用不會觸發緩存逐出,因為其線程退出導致 Task.Run 未能運行,比如下面的代碼
[HttpGet("GetTime")]
public async Task<ActionResult<string>> GetTime()
{
var CurrentTime = this.cache.GetString("CurrentTime");
return CurrentTime;
}
將導致 SqlServerCache 無法完整執行方法 ScanForExpiredItemsIfRequired(),因為其內部使用了 Task 進行異步處理,正確的做法是使用 await this.cache.GetStringAsync("CurrentTime");
1.6 關於緩存清理方法 ScanForExpiredItemsIfRequired
private void ScanForExpiredItemsIfRequired()
{
var utcNow = _systemClock.UtcNow;
if ((utcNow - _lastExpirationScan) > _expiredItemsDeletionInterval)
{
_lastExpirationScan = utcNow;
Task.Run(_deleteExpiredCachedItemsDelegate);
}
}
在多線程環境下,該方法可能除非多次重復掃描,即可能會多次執行 SQL 語句 DELETE FROM {0} WHERE @UtcNow > ExpiresAtTime ,但是,這也僅僅是警告而已,並沒有任何可改變其行為的控制途徑
1.7 IDistributedCache 的其它擴展方法
.Net Core 中還對 IDistributedCache 進行了擴展,甚至允許通過 Set 方法傳入一個 DistributedCacheEntryOptions 以覆蓋全局設置,這些擴展方法的使用都比較簡單,直接傳入相應的值即可,在此不再一一介紹
希望深入研究的同學,可以手動逐一測試
1.8 關於 AddDistributedSqlServerCache() 方法
AddDistributedSqlServerCache 方法內部實際上是進行了一系列的注冊操作,其中最重要的是注冊了 SqlServerCache 到 IDistributedCache 接口,該操作使得我們可以在控制器中采用依賴注入的方式使用 IDistributedCache 的實例
查看 AddDistributedSqlServerCache 方法的代碼片段
public static IServiceCollection AddDistributedSqlServerCache(this IServiceCollection services, Action<SqlServerCacheOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
services.AddOptions();
AddSqlServerCacheServices(services);
services.Configure(setupAction);
return services;
}
internal static void AddSqlServerCacheServices(IServiceCollection services)
{
services.Add(ServiceDescriptor.Singleton<IDistributedCache, SqlServerCache>());
}
2. 使用 Redis 分布式緩存
要在 Asp.Net Core 項目中使用 Redis 分布式緩存,需要引用包:Microsoft.Extensions.Caching.Redis,.Net Core 中的 Redis 分布式緩存客戶端由 RedisCache 類提供實現 ,RedisCache 位於程序集 Microsoft.Extensions.Caching.StackExchangeRedis.dll 中,該程序集正是是依賴於大名鼎鼎的 Redis 客戶端 StackExchange.Redis.dll,StackExchange.Redis 有許多的問題,其中最為嚴重的是超時問題,不過這不知本文的討論范圍,如果你希望使用第三方 Redis 客戶端替代 StackExchange.Redis 來使用分布式緩存,你需要自己實現 IDistributedCache 接口,好消息是,IDistributedCache 接口並不復雜,定義非常簡單
2.1 在 Startup.cs 中注冊 Redis 分布式緩存配置
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedRedisCache(options =>
{
options.InstanceName = "TestDb";
options.Configuration = this.Configuration["RedisConnectionString"];
});
...
}
注冊 Redis 分布式緩存配置和使用 StackExchange.Redis 的方式完全相同,需要注意的是 RedisCacheOptions 包含 3 個屬性,而 Configuration 和 ConfigurationOptions 的作用是相同的,一旦設置了 ConfigurationOptions ,就不應該再去設置屬性 Configuration 的值,因為,在 AddDistributedRedisCache() 注冊內部,會判斷如果設置了 ConfigurationOptions 的值,則不再使用 Configuration;但是,我們建議還是通過屬性 Configuration 去初始化 Redis 客戶端,因為,這是一個連接字符串,而各種配置都可以通過連接字符串進行設置,這和使用 StackExchange.Redis 的方式是完全一致的
2.2 使用緩存
[Route("api/Home")]
[ApiController]
public class HomeController : Controller
{
private IDistributedCache cache;
public HomeController(IDistributedCache cache)
{
this.cache = cache;
}
[HttpGet("Index")]
public async Task<ActionResult<string>> SetTime()
{
var CurrentTime = DateTime.Now.ToString();
await this.cache.SetStringAsync("CurrentTime", CurrentTime);
return CurrentTime;
}
[HttpGet("GetTime")]
public async Task<ActionResult<string>> GetTime()
{
var CurrentTime = await this.cache.GetStringAsync("CurrentTime");
return CurrentTime;
}
}
細心的你可能已經發現了,上面的這段代碼和之前演示的 SqlServerCache 完全一致,是的,僅僅是修改一下注冊的方法,我們就能在項目中進行無縫的切換;但是,對於緩存有強依賴的業務,建議還是需要做好緩存遷移,確保項目能夠平滑過渡
唯一不同的是,使用 Redis 分布式緩存允許你在異步方法中調用同步獲取緩存的方法,這不會導致緩存清理的問題,因為緩存的管理已經完全交給了 Redis 客戶端 StackExchange.Redis 了
3. 實現自定義的分布式緩存客戶端,下面的代碼表示實現一個 CSRedis 客戶端的分布式緩存注冊擴展
3.1 定義 CSRedisCache 實現 IDistributedCache 接口
public class CSRedisCache : IDistributedCache, IDisposable
{
private CSRedis.CSRedisClient client;
private CSRedisClientOptions _options;
public CSRedisCache(IOptions<CSRedisClientOptions> optionsAccessor)
{
if (optionsAccessor == null)
{
throw new ArgumentNullException(nameof(optionsAccessor));
}
_options = optionsAccessor.Value;
if (_options.NodeRule != null && _options.ConnectionStrings != null)
client = new CSRedis.CSRedisClient(_options.NodeRule, _options.ConnectionStrings);
else if (_options.ConnectionString != null)
client = new CSRedis.CSRedisClient(_options.ConnectionString);
else
throw new ArgumentNullException(nameof(_options.ConnectionString));
RedisHelper.Initialization(client);
}
public void Dispose()
{
if (client != null)
client.Dispose();
}
public byte[] Get(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return RedisHelper.Get<byte[]>(key);
}
public async Task<byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
token.ThrowIfCancellationRequested();
return await RedisHelper.GetAsync<byte[]>(key);
}
public void Refresh(string key)
{
throw new NotImplementedException();
}
public Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
{
throw new NotImplementedException();
}
public void Remove(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
RedisHelper.Del(key);
}
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
await RedisHelper.DelAsync(key);
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
RedisHelper.Set(key, value);
}
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
await RedisHelper.SetAsync(key, value);
}
}
代碼不多,都是實現 IDistributedCache 接口,然后在 IDisposable.Dispose 中釋放資源
3.2 自定義一個配置類 CSRedisClientOptions
public class CSRedisClientOptions
{
public string ConnectionString { get; set; }
public Func<string, string> NodeRule { get; set; }
public string[] ConnectionStrings { get; set; }
}
該配置類主要是為 CSRedis 客戶端接收配置使用
3.3 注冊擴展方法 CSRedisCacheServiceCollectionExtensions
public static class CSRedisCacheServiceCollectionExtensions
{
public static IServiceCollection AddCSRedisCache(this IServiceCollection services, Action<CSRedisClientOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
services.AddOptions();
services.Configure(setupAction);
services.Add(ServiceDescriptor.Singleton<IDistributedCache, CSRedisCache>());
return services;
}
}
自定義一個擴展方法,進行配置初始化工作,簡化實際注冊使用時的處理步驟
3.4 在 Startup.cs 中使用擴展
public void ConfigureServices(IServiceCollection services)
{
services.AddCSRedisCache(options =>
{
options.ConnectionString = this.Configuration["RedisConnectionString"];
});
...
}
上面的代碼就簡單實現了一個第三方分布式緩存客戶端的注冊和使用
3.5 測試自定義分布式緩存客戶端,創建一個測試控制器 CustomerController
[Route("api/Customer")]
[ApiController]
public class CustomerController : Controller
{
private IDistributedCache cache;
public CustomerController(IDistributedCache cache)
{
this.cache = cache;
}
[HttpGet("NewId")]
public async Task<ActionResult<string>> NewId()
{
var id = Guid.NewGuid().ToString("N");
await this.cache.SetStringAsync("CustomerId", id);
return id;
}
[HttpGet("GetId")]
public async Task<ActionResult<string>> GetId()
{
var id = await this.cache.GetStringAsync("CustomerId");
return id;
}
}
該控制器簡單實現兩個接口,NewId/GetId,運行程序,輸出結果正常
- 調用 NewId 接口創建一條緩存記錄
- 調用 GetId 接口獲取緩存記錄
至此,我們完整的實現了一個自定義分布式緩存客戶端注冊
4. 關於本示例的使用說明
4.1 首先看一下解決方案結構
該解決方案紅框處定義了 3 個不同的 Startup.cs 文件,分別是
- CSRedisStartup (自定義緩存測試啟動文件)
- Sql_Startup (SqlServer 測試啟動文件)
- StackChangeRedis_Startup(StackChange.Redis 測試啟動文件)
- 在使用本示例的時候,通過在 Program.cs 中切換不同的啟動文件進行測試
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Ron.DistributedCacheDemo.Startups.SqlServer.Startup>();
結束語
通過介紹,我們了解到如何在 Asp.Net Core 中使用分布式緩存
了解了使用不同的緩存類型,如 SqlServer 和 Redis
了解到了如何使用不同的緩存類型客戶端進行注冊
了解到如何實現自定義緩存客戶端
還知道了在調用 SqlServer 緩存的時候,異步方法中的同步調用會導致 SqlServerCache 無法進行過期掃描
CSRedisCore 此項目是由我的好朋友 nicye 維護,GitHub 倉庫地址:訪問CSRedisCore
示例代碼下載
https://github.com/lianggx/EasyAspNetCoreDemo/tree/master/Ron.DistributedCacheDemo