如何在 ASP.Net Core 中使用 NCache

存储 存储软件
虽然 ASP.Net Core 中缺少 Cache 对象,但它引入了三种不同的cache方式。内存缓存,分布式缓存,Response缓存。

[[384065]]

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

虽然 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 窗口输入如下命令进行安装。

Install-Package Alachisoft.NCache.SessionServices 
  • 1.

使用 IDistributedCache

要想在 ASP.Net Core 中使用分布式缓存,需要实现 IDistributedCache 接口,这个接口主要用于让第三方的缓存框架无缝对接到 ASP.Net Core 中,下面是 IDistributedCache 的骨架代码。

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

配置 NCache

要想把 NCache 作为分布式缓存,需要在 ConfigureServices() 中调用 AddNCacheDistributedCache 扩展方法将其注入到容器中,如下代码所示:

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

使用 NCache 进行CURD

为了方便演示,先来定义一个 Author 类,如下代码所示:

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

接下来实现从 NCache 中读取 Author 对象,如果缓存中存在 Author 对象,则直接从缓存中读取,如果缓存中没有,则需要先从数据库中获取 Author,然后再将 Author 塞入到 Cache 中,下面的具体代码逻辑仅供参考。

public async Task<Author> GetAuthor(int id) 

    _cache = NCache.InitializeCache("CacheName"); 
     
    var cacheKey = "Key"
 
    Author author = null
     
    if (_cache != null
    { 
        author = _cache.Get(cacheKey) as Author; 
    } 
     
    if (author == null) //Data not available in the cache 
    { 
        if (_cache != null
        { 
             _cache.Insert(cacheKey, author, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), Alachisoft.NCache.Runtime.CacheItemPriority.Default); 
        } 
    } 
    return author; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

NCache 由 Alachisoft 出品给 .NET 世界提供了一种分布式缓存的解决方案,同时你也能看到 IDistributedCache 是一套标准的用于分布式缓存的高层API,方便第三方的缓存无缝接入,比如:Redis,Mongodb,Mysql 等等。

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

 

责任编辑:武晓燕 来源: 码农读书
相关推荐

2021-03-17 09:45:31

LazyCacheWindows

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通讯TypeScript

2021-03-10 09:40:43

LamarASP容器

2021-02-03 13:35:25

ASPweb程序

2021-01-31 22:56:50

FromServiceASP

2021-01-28 22:39:35

LoggerMessa开源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-03-03 22:37:16

MediatR中介者模式

2021-02-07 17:29:04

监视文件接口

2021-06-22 16:59:56

微软.NETC# 软件开发

2021-01-04 05:44:54

框架日志

2021-04-12 07:03:10

轻量级模块化框架

2021-01-26 14:57:00

中间件应用模块化

2022-08-01 08:00:00

开发工具跟踪侦听器

2017-10-20 08:52:11

内存缓存并发模式Linux

2009-02-05 14:02:46

SmtpMail发送邮件ASP.NET

2021-11-01 14:52:38

ElasticSear索引SQL

2021-04-14 07:35:12

Json格式化日期

2021-02-17 08:51:55

cookie身份验证
点赞
收藏

51CTO技术栈公众号