關于我
作者博客|文章首發
快取基礎知識
快取可以減少生成內容所需的作業,從而顯著提高應用程式的性能和可伸縮性, 快取最適用于不經常更改的 資料,生成 成本很高, 通過快取,可以比從資料源回傳的資料的副本速度快得多, 應該對應用進行撰寫和測驗,使其 永不 依賴于快取的資料,
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<CacheService>();//實際測驗服務
service.AddHostedService<BackgroundJob>();//后臺執行方法
});
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取出快取
/// <summary>
/// 快取設定
/// </summary>
public void BaseCache()
{
string cacheKey = "timestamp";
//set cache
_memoryCache.Set(cacheKey, DateTime.Now.ToString());
//get cache
Console.WriteLine(_memoryCache.Get(cacheKey));
}
IMemoryCache提供一些好的語法糖供開發者使用,具體內容看下方檔案
/// <summary>
/// 特殊方法的使用
/// </summary>
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 = https://www.cnblogs.com/foxhappy/p/_memoryCache.GetOrCreate(cacheKey, entry =>
{
return DateTime.Now.ToString();
});
Console.WriteLine(dataCacheValue);
//洗掉快取
_memoryCache.Remove(cacheKey);
//場景 判斷快取是否存在的同時取出快取資料
_memoryCache.TryGetValue(cacheKey, out string cacheValue);
Console.WriteLine(cacheValue);
}
快取過期策略
設定快取常用的方式主要是以下二種
- 絕對到期(指定在一個固定的時間點到期)
- 滑動到期(在一個時間長度內沒有被命中則過期)
- 組合過期 (絕對過期+滑動過期)
絕對到期
過期策略 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----------------");
快取狀態變化事件
當快取更新、洗掉時觸發一個回呼事件,記錄快取變化的內容,
/// <summary>
/// cache狀態變化回呼
/// </summary>
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也失效
/// <summary>
/// 快取依賴策略
/// </summary>
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 = "https://www.cnblogs.com/foxhappy/p/parent" + DateTime.Now;
//當前key對應的回呼事件
entry.RegisterPostEvictionCallback(MyCallback, this);
//基于些key創建一個依賴快取
_memoryCache.Set(cacheKeyChild, "child" + DateTime.Now, new CancellationChangeToken(cts.Token));
}
string ParentCachedTime = _memoryCache.Get<string>(cacheKeyParent);
string ChildCachedTime = _memoryCache.Get<string>(cacheKeyChild);
string callBackMsg = _memoryCache.Get<string>("callbackMessage");
Console.WriteLine("第一次獲取");
Console.WriteLine(ParentCachedTime + "|" + ChildCachedTime + "|" + callBackMsg);
//移除parentKey
_memoryCache.Get<CancellationTokenSource>(DependentCTS).Cancel();
Thread.Sleep(1000);
ParentCachedTime = _memoryCache.Get<string>(cacheKeyParent);
ChildCachedTime = _memoryCache.Get<string>(cacheKeyChild);
callBackMsg = _memoryCache.Get<string>("callbackMessage");
Console.WriteLine("第二次獲取");
Console.WriteLine(ParentCachedTime + "|" + ChildCachedTime + "|" + callBackMsg);
}
參考資料
AspNetCore中的快取記憶體
.NetCore快取篇之MemoryCache
Asp.Net Core 輕松學-在.Net Core 使用快取和配置依賴策略
擁抱.NET Core系列:MemoryCache 快取過期
推薦閱讀
Redis工具收費后新的開源已出現
GitHub上Star最高的工程師技能圖譜
中國程式員最容易發錯的單詞
推薦!!! Markdown圖示索引網站
最后
本文到此結束,希望對你有幫助 ??
如果還有什么疑問或者建議,可以多多交流,原創文章,文筆有限,才疏學淺,文中若有不正之處,萬望告知,
更多精彩技術文章匯總在我的 公眾號【程式員工具集】,持續更新,歡迎關注訂閱收藏,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/270592.html
標籤:Go
