NetCore的快取使用詳例
阿新 • • 發佈:2021-04-01
# 關於我
[作者部落格|文章首發](http://www.zhouhuibo.club)
# 快取基礎知識
快取可以減少生成內容所需的工作,從而顯著提高應用程式的效能和可伸縮性。 快取最適用於不經常更改的 **資料,生成** 成本很高。 通過快取,可以比從資料來源返回的資料的副本速度快得多。 應該對應用進行編寫和測試,使其 **永不** 依賴於快取的資料。
ASP.NET Core 支援多個不同的快取。 最簡單的快取基於 IMemoryCache。 `IMemoryCache` 表示儲存在 web 伺服器的記憶體中的快取。 在伺服器場上執行的應用 (多臺伺服器) 應確保會話在使用記憶體中快取時處於粘滯狀態。 粘滯會話確保來自客戶端的後續請求都將傳送到相同的伺服器。
記憶體中快取可以儲存任何物件。 分散式快取介面僅限 `byte[]` 。 記憶體中和分散式快取將快取項作為鍵值對。
# 快取指南
- 程式碼應始終具有回退選項,以獲取資料,而 **不**是依賴於可用的快取值。
- 快取使用稀有資源記憶體,限制快取增長:
- 不要 **使用外部** 輸入作為快取鍵。
- 使用過期限制快取增長。
- 使用 SetSize、Size 和 SizeLimit 限制快取大小]。 ASP.NET Core 執行時不會根據記憶體 **壓力限制快取** 大小。 開發人員需要限制快取大小。
# 使用
## DI注入
建立一個NetCore控制檯專案,進行快取的專案演示。
控制檯專案只有一個初始化的Program.cs檔案。基於NetCore進行專案編碼,每一步就是建立一個基礎模板,使用依賴注入的方式。
```
nuget install Microsoft.Extensions.Hosting
```
```
public static class Program
{
static async void Main(string[] args)
{
var builder = new HostBuilder().ConfigureServices((context, service) =>
{
});
await builder.RunConsoleAsync();
}
}
```
注入快取服務,控制檯需要下載庫 Microsoft.Extensions.Caching.Memory
```
nuget install Microsoft.Extensions.Caching.Memory
```
```
public static class Program
{
static async void Main(string[] args)
{
var builder = new HostBuilder().ConfigureServices((context, service) =>
{
service.AddMemoryCache();
service.AddScoped();//實際測試服務
service.AddHostedService();//後臺執行方法
});
await builder.RunConsoleAsync();
}
}
```
**後臺服務**
```
public class BackgroundJob : IHostedService
{
private readonly CacheService _cacheService;
public BackgroundJob(CacheService cacheService)
{
_cacheService = cacheService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_cacheService.Action();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
```
## MemoryCache使用總結
通過建構函式自動注入IMemoryCache
```
public class CacheService
{
private readonly IMemoryCache _memoryCache;
public CacheService(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
}
```
## 最基本的使用
Set方法根據Key設定快取,預設快取不過期
Get方法根據Key取出快取
```
///
/// 快取設定
///
public void BaseCache()
{
string cacheKey = "timestamp";
//set cache
_memoryCache.Set(cacheKey, DateTime.Now.ToString());
//get cache
Console.WriteLine(_memoryCache.Get(cacheKey));
}
```
IMemoryCache提供一些好的語法糖供開發者使用,具體內容看下方文件
```
///
/// 特殊方法的使用
///
public void ActionUse()
{
//場景-如果快取存在,取出。如果快取不存在,寫入
//原始寫法
string cacheKey = "timestamp";
if (_memoryCache.Get(cacheKey) != null)
{
_memoryCache.Set(cacheKey, DateTime.Now.ToString());
}
else
{
Console.WriteLine(_memoryCache.Get(cacheKey));
}
//新寫法
var dataCacheValue = _memoryCache.GetOrCreate(cacheKey, entry =>
{
return DateTime.Now.ToString();
});
Console.WriteLine(dataCacheValue);
//刪除快取
_memoryCache.Remove(cacheKey);
//場景 判斷快取是否存在的同時取出快取資料
_memoryCache.TryGetValue(cacheKey, out string cacheValue);
Console.WriteLine(cacheValue);
}
```
### 快取過期策略
設定快取常用的方式主要是以下二種
1. 絕對到期(指定在一個固定的時間點到期)
2. 滑動到期(在一個時間長度內沒有被命中則過期)
3. 組合過期 (絕對過期+滑動過期)
#### 絕對到期
過期策略 5秒後過期
```
//set absolute cache
string cacheKey = "absoluteKey";
_memoryCache.Set(cacheKey, DateTime.Now.ToString(), TimeSpan.FromSeconds(5));
//get absolute cache
for (int i = 0; i < 6; i++)
{
Console.WriteLine(_memoryCache.Get(cacheKey));
Thread.Sleep(1000);
}
```
#### 滑動到期
過期策略 2秒的滑動過期時間,如果2秒內有訪問,過期時間延後。當2秒的區間內沒有訪問,快取過期
```
//set slibing cache
string cacheSlibingKey = "slibingKey";
MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
options.SlidingExpiration = TimeSpan.FromSeconds(2);
_memoryCache.Set(cacheSlibingKey, DateTime.Now.ToString(), options);
//get slibing cache
for (int i = 0; i < 2; i++)
{
Console.WriteLine(_memoryCache.Get(cacheSlibingKey));
Thread.Sleep(1000);
}
for (int i = 0; i < 2; i++)
{
Thread.Sleep(2000);
Console.WriteLine(_memoryCache.Get(cacheSlibingKey));
}
```
#### 組合過期
過期策略
6秒絕對過期+2秒滑動過期
滿足任意一個快取都將失效
```
string cacheCombineKey = "combineKey";
MemoryCacheEntryOptions combineOptions = new MemoryCacheEntryOptions();
combineOptions.SlidingExpiration = TimeSpan.FromSeconds(2);
combineOptions.AbsoluteExpiration = DateTime.Now.AddSeconds(6);
_memoryCache.Set(cacheCombineKey, DateTime.Now.ToString(), combineOptions);
//get slibing cache
for (int i = 0; i < 2; i++)
{
Console.WriteLine(_memoryCache.Get(cacheCombineKey));
Thread.Sleep(1000);
}
for (int i = 0; i < 6; i++)
{
Thread.Sleep(2000);
Console.WriteLine(i+"|" + _memoryCache.Get(cacheCombineKey));
}
Console.WriteLine("------------combineKey End----------------");
```
## 快取狀態變化事件
當快取更新、刪除時觸發一個回撥事件,記錄快取變化的內容。
```
///
/// cache狀態變化回撥
///
public void CacheStateCallback()
{
MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
options.AbsoluteExpiration = DateTime.Now.AddSeconds(3
);
options.RegisterPostEvictionCallback(MyCallback, this);
//show callback console
string cacheKey = "absoluteKey";
_memoryCache.Set(cacheKey, DateTime.Now.ToString(), options);
Thread.Sleep(500);
_memoryCache.Set(cacheKey, DateTime.Now.ToString(), options);
_memoryCache.Remove(cacheKey);
}
private static void MyCallback(object key, object value, EvictionReason reason, object state)
{
var message = $"Cache entry state change:{key} {value} {reason} {state}";
((CacheService)state)._memoryCache.Set("callbackMessage", message);
Console.WriteLine(message);
}
```
## 快取依賴策略
> 設定一個快取A
> 設定一個快取B,依賴於快取A 如果快取A失效,快取B也失效
```
///
/// 快取依賴策略
///
public void CacheDependencyPolicy()
{
string DependentCTS = "DependentCTS";
string cacheKeyParent = "CacheKeys.Parent";
string cacheKeyChild = "CacheKeys.Child";
var cts = new CancellationTokenSource();
_memoryCache.Set(DependentCTS, cts);
//建立一個cache策略
using (var entry = _memoryCache.CreateEntry(cacheKeyParent))
{
//當前key對應的值
entry.Value = "parent" + DateTime.Now;
//當前key對應的回撥事件
entry.RegisterPostEvictionCallback(MyCallback, this);
//基於些key建立一個依賴快取
_memoryCache.Set(cacheKeyChild, "child" + DateTime.Now, new CancellationChangeToken(cts.Token));
}
string ParentCachedTime = _memoryCache.Get(cacheKeyParent);
string ChildCachedTime = _memoryCache.Get(cacheKeyChild);
string callBackMsg = _memoryCache.Get("callbackMessage");
Console.WriteLine("第一次獲取");
Console.WriteLine(ParentCachedTime + "|" + ChildCachedTime + "|" + callBackMsg);
//移除parentKey
_memoryCache.Get(DependentCTS).Cancel();
Thread.Sleep(1000);
ParentCachedTime = _memoryCache.Get(cacheKeyParent);
ChildCachedTime = _memoryCache.Get(cacheKeyChild);
callBackMsg = _memoryCache.Get("callbackMessage");
Console.WriteLine("第二次獲取");
Console.WriteLine(ParentCachedTime + "|" + ChildCachedTime + "|" + callBackMsg);
}
```
# 參考資料
[AspNetCore中的快取記憶體](https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/memory?view=aspnetcore-5.0#cache-dependencies)
[.NetCore快取篇之MemoryCache](https://www.cnblogs.com/zhangxiaoyong/p/9472637.html)
[Asp.Net Core 輕鬆學-在.Net Core 使用快取和配置依賴策略](https://www.cnblogs.com/viter/p/10146312.html)
[擁抱.NET Core系列:MemoryCache 快取過期](https://www.cnblogs.com/ants/p/8482227.html)
# 推薦閱讀
[Redis工具收費後新的開源已出現](https://mp.weixin.qq.com/s/-TUp2MKKLD3R0j3xt85NUA)
[GitHub上Star最高的工程師技能圖譜](https://mp.weixin.qq.com/s/NOmbb0FqUmxQOkCf_YhTDw)
[中國程式設計師最容易發錯的單詞 ](https://mp.weixin.qq.com/s/ceC0NK62bEz-fWVDdg4j9A)
[推薦!!! Markdown圖示索引網站](https://mp.weixin.qq.com/s/qYDEC-QBIUzgjxlBxQHrJQ)
# 最後
本文到此結束,希望對你有幫助