自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在 ASP.Net Core 中使用 NCache

存儲(chǔ) 存儲(chǔ)軟件
雖然 ASP.Net Core 中缺少 Cache 對(duì)象,但它引入了三種不同的cache方式。內(nèi)存緩存,分布式緩存,Response緩存。

[[384065]]

本文轉(zhuǎn)載自微信公眾號(hào)「碼農(nóng)讀書」,作者碼農(nóng)讀書。轉(zhuǎn)載本文請(qǐng)聯(lián)系碼農(nóng)讀書公眾號(hào)。

雖然 ASP.Net Core 中缺少 Cache 對(duì)象,但它引入了三種不同的cache方式。

  • 內(nèi)存緩存
  • 分布式緩存
  • Response緩存

Alachisoft 公司提供了一個(gè)開源項(xiàng)目 NCache,它是一個(gè)高性能的,分布式的,可擴(kuò)展的緩存框架,NCache不僅比 Redis 快,而且還提供了一些Redis所不具有的分布式特性,如果你想了解 NCache 和 Redis 的異同,可參考如下鏈接:http://www.alachisoft.com/resources/comparisons/redis-vs-ncache.php ,這篇文章我們將會(huì)討論如何在 ASP.Net Core 中使用 NCache。

要想在 ASP.Net Core 中使用 NCache,需要通過(guò) NuGet 安裝如下包,你可以通過(guò) NuGet Package Manager console 窗口輸入如下命令進(jìn)行安裝。

  1. Install-Package Alachisoft.NCache.SessionServices 

使用 IDistributedCache

要想在 ASP.Net Core 中使用分布式緩存,需要實(shí)現(xiàn) IDistributedCache 接口,這個(gè)接口主要用于讓第三方的緩存框架無(wú)縫對(duì)接到 ASP.Net Core 中,下面是 IDistributedCache 的骨架代碼。

  1. namespace Microsoft.Extensions.Caching.Distributed 
  2.     { 
  3.         public interface IDistributedCache 
  4.         { 
  5.             byte[] Get(string key); 
  6.             void Refresh(string key); 
  7.             void Remove(string key); 
  8.             void Set(string key, byte[] value, 
  9.             DistributedCacheEntryOptions options); 
  10.         } 
  11.     } 

配置 NCache

要想把 NCache 作為分布式緩存,需要在 ConfigureServices() 中調(diào)用 AddNCacheDistributedCache 擴(kuò)展方法將其注入到容器中,如下代碼所示:

  1. // This method gets called by the runtime. Use this method to add services to the container. 
  2.         public void ConfigureServices(IServiceCollection services) 
  3.         { 
  4.             services.AddNCacheDistributedCache(configuration => 
  5.             { 
  6.                 configuration.CacheName = "IDGDistributedCache"
  7.                 configuration.EnableLogs = true
  8.                 configuration.ExceptionsEnabled = true
  9.             }); 
  10.  
  11.             services.AddControllersWithViews(); 
  12.         } 

使用 NCache 進(jìn)行CURD

為了方便演示,先來(lái)定義一個(gè) Author 類,如下代碼所示:

  1. public class Author 
  2.   { 
  3.       public int AuthorId { get; set; } 
  4.       public string FirstName { get; set; } 
  5.       public string LastName { get; set; } 
  6.   } 

接下來(lái)實(shí)現(xiàn)從 NCache 中讀取 Author 對(duì)象,如果緩存中存在 Author 對(duì)象,則直接從緩存中讀取,如果緩存中沒有,則需要先從數(shù)據(jù)庫(kù)中獲取 Author,然后再將 Author 塞入到 Cache 中,下面的具體代碼邏輯僅供參考。

  1. public async Task<Author> GetAuthor(int id) 
  2.     _cache = NCache.InitializeCache("CacheName"); 
  3.      
  4.     var cacheKey = "Key"
  5.  
  6.     Author author = null
  7.      
  8.     if (_cache != null
  9.     { 
  10.         author = _cache.Get(cacheKey) as Author; 
  11.     } 
  12.      
  13.     if (author == null) //Data not available in the cache 
  14.     { 
  15.         if (_cache != null
  16.         { 
  17.              _cache.Insert(cacheKey, author, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), Alachisoft.NCache.Runtime.CacheItemPriority.Default); 
  18.         } 
  19.     } 
  20.     return author; 

NCache 由 Alachisoft 出品給 .NET 世界提供了一種分布式緩存的解決方案,同時(shí)你也能看到 IDistributedCache 是一套標(biāo)準(zhǔn)的用于分布式緩存的高層API,方便第三方的緩存無(wú)縫接入,比如:Redis,Mongodb,Mysql 等等。

譯文鏈接:https://www.infoworld.com/article/3342120/how-to-use-ncache-in-aspnet-core.html

 

責(zé)任編輯:武曉燕 來(lái)源: 碼農(nóng)讀書
相關(guān)推薦

2021-01-31 22:56:50

FromServiceASP

2021-02-03 13:35:25

ASPweb程序

2021-03-03 22:37:16

MediatR中介者模式

2021-03-10 09:40:43

LamarASP容器

2021-01-28 22:39:35

LoggerMessa開源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-03-17 09:45:31

LazyCacheWindows

2021-02-06 21:40:13

SignalR通訊TypeScript

2021-02-02 16:19:08

Serilog日志框架

2021-02-07 17:29:04

監(jiān)視文件接口

2021-01-26 14:57:00

中間件應(yīng)用模塊化

2021-04-12 07:03:10

輕量級(jí)模塊化框架

2021-01-04 05:44:54

框架日志

2021-06-22 16:59:56

微軟.NETC# 軟件開發(fā)

2009-02-05 14:02:46

SmtpMail發(fā)送郵件ASP.NET

2017-10-20 08:52:11

內(nèi)存緩存并發(fā)模式Linux

2022-08-01 08:00:00

開發(fā)工具跟蹤偵聽器

2021-04-14 07:35:12

Json格式化日期

2009-03-30 10:34:03

ASP.NETMySQL

2021-02-17 08:51:55

cookie身份驗(yàn)證
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)