成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何在 ASP.Net Core 中使用 NCache

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

[[384065]]

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

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

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

Alachisoft 公司提供了一個(gè)開(kāi)源項(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 類(lèi),如下代碼所示:

  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ì)象,則直接從緩存中讀取,如果緩存中沒(méi)有,則需要先從數(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)讀書(shū)
相關(guān)推薦

2021-03-17 09:45:31

LazyCacheWindows

2021-02-06 21:40:13

SignalR通訊TypeScript

2021-02-02 16:19:08

Serilog日志框架

2021-03-10 09:40:43

LamarASP容器

2021-02-03 13:35:25

ASPweb程序

2021-01-07 07:39:07

工具接口 Swagger

2021-03-03 22:37:16

MediatR中介者模式

2021-01-28 22:39:35

LoggerMessa開(kāi)源框架

2021-01-31 22:56:50

FromServiceASP

2021-02-07 17:29:04

監(jiān)視文件接口

2021-06-22 16:59:56

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

2021-01-26 14:57:00

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

2021-04-12 07:03:10

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

2021-01-04 05:44:54

框架日志

2017-10-20 08:52:11

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

2022-08-01 08:00:00

開(kāi)發(fā)工具跟蹤偵聽(tīng)器

2009-02-05 14:02:46

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

2021-04-14 07:35:12

Json格式化日期

2021-11-01 14:52:38

ElasticSear索引SQL

2009-03-30 10:34:03

ASP.NETMySQL
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 国产精品美女久久久久久不卡 | 久久精品国产一区 | 日韩中文字幕 | 美女国内精品自产拍在线播放 | 91精品在线播放 | 亚洲精品aⅴ | 亚洲精品一区中文字幕乱码 | 殴美黄色录像 | 欧美一区二区在线观看 | 中日av | 国产欧美在线一区 | 国产精品久久久久无码av | 久久国产成人 | 在线视频中文字幕 | 国产精品久久久久久婷婷天堂 | 国产中文字幕在线 | 国产成人久久av免费高清密臂 | 欧美性受xxxx白人性爽 | 成人精品在线 | 精品久久久久久久久久久久 | 理论片免费在线观看 | 久久久久亚洲精品中文字幕 | 91av在线免费看 | 日韩小视频在线 | 久久精品久久久 | 欧美黄a| 精品一区二区久久久久久久网精 | www.成人.com| 欧美日韩亚 | 国产精品久久久久久婷婷天堂 | 久久激情网 | 中文字幕一区二区三区精彩视频 | 色伊人久久 | 久久精品免费观看 | 精品视频免费 | 久久久久久免费毛片精品 | 亚洲国产成人av好男人在线观看 | 国产激情视频在线观看 | 精品一区二区久久久久久久网站 | 欧洲一区二区视频 | 国产精产国品一二三产区视频 |