ASP.NET Core中使用LazyCache的全過程
前言
微軟的 ASP.NET Core 已經(jīng)是一個非常流行的用于構(gòu)建 高性能, 模塊化 并能運行在 Windows, Linux, MacOS 上的 WEB 框架,通常能夠讓程序保持高性能的一個有效途徑就是通過緩存熱鏈上的數(shù)據(jù)來應(yīng)對高頻的請求。
LazyCache 是一款基于內(nèi)存的易于使用和線程安全的緩存組件,值得注意的是,這里的 Lazy 指的是 LazyCache 永遠不會在 緩存未命中 時觸發(fā)一次以上的緩存委托函數(shù),因為內(nèi)置了鎖,換句話說,Lazy 減少了不必要的計算開銷,這篇文章我們將會討論如何在 ASP.NET Core 5.0 中使用 LazyCache。
安裝 LazyCache
要想在 ASP.NET Core MVC 5 中使用 LazyCache,你需要安裝如下兩個 nuget 包。
- LazyCache
- LazyCache.AspNetCore
你可以通過 Nuget 可視化工具安裝或者通過如下命令進行安裝。
PM> Install-Package LazyCache
PM> Install-Package LazyCache.AspNetCore
為什么要緩存?
緩存是一種 狀態(tài)管理策略,在web應(yīng)用程序中,它常常用來緩存一些數(shù)據(jù)到內(nèi)存中供后續(xù)請求復(fù)用,這樣就規(guī)避了原來不得不走硬盤取數(shù)據(jù)的尷尬,顯而易見,緩存是提高 web 程序性能的一大利器,
雖然 ASP.NET Core 缺少內(nèi)置的 Cache,但它提供了對三種緩存類型的支持:內(nèi)存緩存,分布式緩存 和 響應(yīng)緩存。
為什么要使用 LazyCache
LazyCache 是一個開源的,簡單的,線程安全的,可擴展的 緩存組件,在底層,LazyCache 使用了 Microsoft.Extensions.Caching 下的 MemoryCache 并使用了 懶鎖 來確保 委托 只會執(zhí)行一次。
如果你想緩存從數(shù)據(jù)庫中取得的數(shù)據(jù),復(fù)雜的對象圖 和 web服務(wù)調(diào)用,那么使用 LazyCache 是一個非常好的選擇,通常默認緩存的時間是 20min。
下面簡單羅列了 LazyCache 的一些特點。
- 可擴展性
- 開源
- 友好的 API 接口
- 內(nèi)置 lazy 鎖
- 底層使用 MemoryCache
配置 LazyCache
要想配置 LazyCache,可以在 ConfigureServices() 方法中通過調(diào)用 AddLazyCache() 將 LazyCache 注入到 ServiceContainer 中,如下代碼所示:
public void ConfigureServices(IServiceCollection services) { services.AddLazyCache(); services.AddControllers(); }
這樣就可以確保 LazyCache 可以貫穿在你的應(yīng)用程序中,要想訪問 LazyCache 可以通過 IAppCache 接口,定義如下:
public interface IAppCache { ICacheProvider CacheProvider { get; } CacheDefaults DefaultCachePolicy { get; } void Add<T>(string key, T item, MemoryCacheEntryOptions policy); T Get<T>(string key); Task<T> GetAsync<T>(string key); T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory); T GetOrAdd<T>(string key, Func<ICacheEntry, T> addItemFactory, MemoryCacheEntryOptions policy); Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory); Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> addItemFactory, MemoryCacheEntryOptions policy); void Remove(string key); bool TryGetValue<T>(string key, out object value); }
將 IAppCache 注入到 Controller 中
一切都配置好之后,接下來就可以將 IAppCache 通過構(gòu)造函數(shù)注入的方式灌到 Controller 中,如下代碼所示:
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private readonly IAppCache _lazyCache; public WeatherForecastController(IAppCache cache) { this._lazyCache = cache; } }
通過 LazyCache 新增,獲取數(shù)據(jù)
考慮如下返回 list 的 GetData() 方法。
private async Task<List<string>> GetData() { return new List<string>() { "Joydip Kanjilal", "Steve Smith", "Rick Smith" }; }
接下來通過 GetOrAddAsync 方法從緩存獲取或添加數(shù)據(jù),如下代碼所示:
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private readonly IAppCache _lazyCache; public WeatherForecastController(IAppCache cache) { this._lazyCache = cache; } [HttpGet] public async Task<IEnumerable<string>> Get() { var data = await _lazyCache.GetOrAddAsync("Authors", GetData, DateTimeOffset.Now.AddMinutes(30)); return data; } private async Task<List<string>> GetData() { return new List<string>() { "Joydip Kanjilal", "Steve Smith", "Rick Smith" }; } }
如果你想在內(nèi)存中存儲更多的數(shù)據(jù)或者你想使用檔次更高的緩存服務(wù),推薦使用 Redis 作為分布式緩存,而 LazyCache 🐂👃的地方在于你使用的一直是 IAppCache,這就意味著你可以輕松的用 Redis 來替換底層默認的 MemoryCache。
譯文鏈接:https://www.infoworld.com/article/3608568/how-to-use-lazycache-in-aspnet-core-mvc-5.html
總結(jié)
到此這篇關(guān)于ASP.NET Core中使用LazyCache的文章就介紹到這了,更多相關(guān)ASP.NET Core使用LazyCache內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET MVC3 SEO優(yōu)化:利用Routing特性提高站點權(quán)重
這篇文章主要介紹了ASP.NET MVC3 SEO優(yōu)化:利用Routing特性消除多個路徑指向同一個Action,從而提高站點權(quán)重,需要的朋友可以參考下。2016-06-06Visual Studio 2017新版發(fā)布 更強大!
Visual Studio 2017新版發(fā)布 更強大!對Visual Studio 2017感興趣的小伙伴們可以參考一下2017-05-05asp.net Silverlight應(yīng)用程序中獲取載體aspx頁面參數(shù)
有時候SL應(yīng)用中需要使用由aspx頁面中傳遞過來的參數(shù)值,此時通常有兩種方法獲取2009-11-11