如何在ASP.Net Core使用分布式緩存的實現(xiàn)
ASP.Net Core 提供了多種類型的緩存,除了內存緩存和響應緩存之外,還提供了對 分布式緩存 的支持。在之前的一篇文章中,我討論了 ASP.Net Core 的內存緩存。在本文中,我們將討論如何在 ASP.Net Core 中使用分布式緩存,本篇就拿 Redis 和 SQL Server 作為演示。
什么是分布式緩存
分布式緩存 可用于提高應用程序的性能和可伸縮性,通常 分布式緩存 被多個應用服務器共享,在分布式緩存中,緩存的數(shù)據不會落在某些個別的web服務器內存中,這些緩存數(shù)據采用集中化存儲,這樣多個應用服務器都可以直接使用,這樣做的好處在于,如果任何一個服務器宕機或者停止響應,其他的服務器仍然能夠檢索緩存的數(shù)據。分布式緩存的另一個優(yōu)點是,緩存的數(shù)據在服務器重啟后仍然存在,當你的應用集群擴展時,并不會對緩存服務器造成任何影響。
要想在 ASP.NET Core 中使用分布式緩存,需要用到 IDistributedCache 接口,在下一節(jié)中,我們將會一起討論 IDistributedCache 和 IMemoryCache 接口的區(qū)別。
IDistributedCache 接口
在.Net Core 中用于分布式緩存的 IDistributedCache 接口要比 單機版的 IMemoryCache 接口更復雜,先來看一下 IMemoryCache 接口定義。
public interface IMemoryCache : IDisposable { bool TryGetValue(object key, out object value); ICacheEntry CreateEntry(object key); void Remove(object key); }
IDistributedCache 接口是為 web farm 場景設計的, 它包含了一組同步和異步方法,可用于對緩存的 Add,Remove,Retrieve 操作,下面是 IDistributedCache 接口的定義。
public interface IDistributedCache { byte[] Get(string key); Task<byte[]> GetAsync(string key); void Set(string key, byte[] value, DistributedCacheEntryOptions options); Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options); void Refresh(string key); Task RefreshAsync(string key); void Remove(string key); Task RemoveAsync(string key); }
有一點值得注意,上面的 Set 方法的 value 僅支持 byte[],有點坑哈,當然你要塞入 string 的話, 不用擔心,ASP.NET Core 也提供了擴展方法對其進行支持.
如何使用 Redis 作為緩存介質
可以通過 Nuget 來安裝如下擴展包,代碼如下:
Install-Package Microsoft.Extensions.Caching.Redis
為了能夠把 Redis 作為應用底層緩存,需要使用 AddDistributedRedisCache() 擴展方法,下面的代碼展示了如何去配置:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddDistributedRedisCache(option => { option.Configuration ="localhost"; option.InstanceName ="IDG"; }); }
如何注入到 Controller
下面的代碼清單展示了如何將 IDistributedCache 注入到 Controller 中并實現(xiàn)從 Redis 中進行插入和讀取。
public class DefaultController : Controller { private readonly IDistributedCache _distributedCache; public HomeController(IDistributedCache distributedCache) { _distributedCache = distributedCache; } [HttpGet] public async Task<string> Get() { var cacheKey ="IDG"; var data = _distributedCache.GetString(cacheKey); if (!string.IsNullOrEmpty(data)) { return data; //returned from Cache } else { string str ="Hello World"; _distributedCache.SetString(cacheKey, str); return str; } } }
如何使用 SqlServer 作為緩存介質
要想將 SqlServer 作為底層的緩存介質,需要通過 Nuget 安裝如下包:
Install-Package Microsoft.Extensions.Caching.SqlServer Install-Package Microsoft.Extensions.Caching.SqlConfig.Tools
如何在 Startup.ConfigureServices() 中做如下配置。
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDistributedSqlServerCache(x => { x.ConnectionString = Configuration["ConnectionStrings:Default"]; x.SchemaName = "dbo"; x.TableName = "IDGCache"; }); }
接下來通過如下命令在 SqlServer 中生成 Table 來存放緩存數(shù)據,代碼如下:
dotnet sql-cache create <connection string> <schema> <table>
ASP.Net Core 提供了分布式緩存的高層抽象。因此,無論底層緩存介質是 Redis 還是 SQL Server, IDistributedCache接口都提供了統(tǒng)一并且便捷的操控Cache的API,而且 IDistributedCache 注入到 Controller 中也是非常方便的。
譯文鏈接:https://www.infoworld.com/article/3262990/how-to-implement-a-distributed-cache-in-aspnet-core.html
到此這篇關于如何在ASP.Net Core使用分布式緩存的實現(xiàn)的文章就介紹到這了,更多相關ASP.Net Core 分布式緩存內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
asp.net中的“按需打印”(打印你需要打印的部分) 實現(xiàn)代碼
有時我們需要對asp.net網頁中某些特定部分進行打印,很多人采用CSS樣式或特定的打印控件來解決網頁定制打印功能。這里采用Javascript樣式替換方式進行打印,使網頁顯示與打印效果相分離。2013-06-06asp.net中在用ajax格式傳遞數(shù)據到aspx頁面時出現(xiàn)亂碼
asp.net中在用ajax格式傳遞數(shù)據到aspx頁面時有時會出現(xiàn)亂碼,很是疑惑,不要走開接下來介紹解決方法,感興趣的朋友可以了解下2013-01-01通過RadioButton對DataList控件進行單選實例說明
本例實現(xiàn)通過RadioButton對DataList控件進行單選,aspx拉一個DataList控件,把RadioButton置于DataList的ItemTemplate模版內;在.aspx.cs內為DataList控件綁定數(shù)據,很實用的功能,感興趣的朋友可以了解下啊2013-01-01asp.net實現(xiàn)固定GridView標題欄的方法(凍結列功能)
這篇文章主要介紹了asp.net實現(xiàn)固定GridView標題欄的方法,即凍結列功能,涉及GridView結合前端js操作數(shù)據顯示的相關技巧,需要的朋友可以參考下2016-06-06