IDC

如何在 ASP.Net Core 中使用 NCache

作者:admin 2021-04-09 我要评论

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。 虽然 ASP.Net Core 中缺少 Cache 对象,但它引入了三种不同的cache方式。 内...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号

虽然 ASP.Net Core 中缺少 Cache 对象,但它引入了三种不同的cache方式。

  • 内存缓存
  • 分布式缓存
  • Response缓存

Alachisoft 公司提供了一个开源项目 NCache,它是一个高性能的,分布式的,可扩展的缓存框架,NCache不仅比 Redis 快,而且还提供了一些Redis所不具有的分布式特性,如果你想了解 NCache 和 Redis 的异同,可参考如下链接:http://www.alachisoft.com/resources/comparisons/redis-vs-ncache.php ,这篇文章我们将会讨论如何在 ASP.Net Core 中使用 NCache。

要想在 ASP.Net Core 中使用 NCache,需要通过 NuGet 安装如下包,你可以通过 NuGet Package Manager console 窗口输入如下命令进行安装。

  1. Install-Package Alachisoft.NCache.SessionServices 

使用 IDistributedCache

要想在 ASP.Net Core 中使用分布式缓存,需要实现 IDistributedCache 接口,这个接口主要用于让第三方的缓存框架无缝对接到 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() 中调用 AddNCacheDistributedCache 扩展方法将其注入到容器中,如下代码所示:

  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 进行CURD

为了方便演示,先来定义一个 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.   } 

接下来实现从 NCache 中读取 Author 对象,如果缓存中存在 Author 对象,则直接从缓存中读取,如果缓存中没有,则需要先从数据库中获取 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 世界提供了一种分布式缓存的解决方案,同时你也能看到 IDistributedCache 是一套标准的用于分布式缓存的高层API,方便第三方的缓存无缝接入,比如:Redis,Mongodb,Mysql 等等。

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


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/5y_ikFcJjSX8aFuXcOxXew

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 太棒了!Python和Excel过了这么久终于

    太棒了!Python和Excel过了这么久终于

  • Java编程内功-数据结构与算法「线索化

    Java编程内功-数据结构与算法「线索化

  • 在Java中使用异步编程

    在Java中使用异步编程

  • Kafka性能篇:为何Kafka这么&quot;

    Kafka性能篇:为何Kafka这么&quot;

腾讯云代理商
海外云服务器