ncache .net_如何在ASP.Net Core中使用NCache

ncache .net

尽管ASP.Net Core缺少缓存对象,但它提供了对几种不同类型的缓存的支持,包括内存中缓存,分布式缓存和响应缓存。 NCache是Alachisoft提供的开源产品,它是一种非常快的,内存中的,分布式,可伸缩的缓存框架,可用于.Net应用程序。

NCache是​​100%本机.Net。 它不仅比Redis快,而且还提供了Redis不支持的几种分布式缓存功能。 您可以在此处了解有关NCache和Redis之间差异的更多信息。 本文将讨论如何在ASP.Net Core应用程序中使用NCache。

[Kubernetes,无服务器框架,Kafka,Redis,.Net Core等:请参阅InfoWorld的2019年度技术奖获奖者 | 通过《 InfoWorld日报》时事通讯了解最新的企业技术中的关键新闻和问题。 ]

诸如NCache之类的分布式缓存可以提高应用程序的性能和可伸缩性。 在分布式缓存中,缓存的数据不驻留在单个Web服务器的内存中。 您可以在不影响缓存或缓存数据的情况下添加或删除服务器。 并且,如果任何服务器出现故障或停止响应,其他服务器仍将能够检索缓存的数据。 这解释了为什么分布式缓存中的缓存数据可以在服务器重新启动后继续存在。

在Visual Studio中创建一个ASP.Net Core项目

首先,让我们创建一个ASP.Net Core项目。 如果您的系统已启动并运行Visual Studio 2017,请按照以下步骤在Visual Studio中创建一个新的ASP.Net Core项目。

  1. 启动Visual Studio 2017 IDE。
  2. 单击文件>新建>项目。
  3. 从显示的模板列表中选择“ ASP.Net Core Web应用程序(.Net Core)”。
  4. 指定项目的名称。
  5. 单击确定保存项目。
  6. 接下来显示一个新窗口“ New .Net Core Web Application…”。
  7. 选择.Net Core作为运行时,并从顶部的下拉列表中选择ASP.Net Core 2.2(或更高版本)。
  8. 选择API作为项目模板
  9. 确保未选中“启用Docker支持”和“配置HTTPS”复选框,因为我们此处将不使用这些功能。
  10. 确保选择“无身份验证”,因为我们也不会使用身份验证。
  11. 单击确定。

现在,您应该已经准备好在Visual Studio中使用一个新的ASP.Net Core项目。 接下来,您将需要安装必要的NuGet软件包才能使用NCache。 通过NuGet软件包管理器窗口或从NuGet软件包管理器控制台安装以下NuGet软件包:

Alachisoft.NCache.SessionServices

在您的项目中安装此NuGet程序包后,就可以全部使用NCache了。

在ASP.Net Core中使用IDistributedCache接口

若要在ASP.Net Core应用程序中使用分布式缓存,应使用IDistributedCache接口。 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);
    }
}

在ASP.Net Core中将NCache配置为IDistributedCache提供程序

要使用NCache处理分布式缓存,您应该在Startup.cs文件的ConfigureServices方法中调用AddNCacheDistributedCache方法,如下面的代码片段所示。 请注意,AddNCacheDistributedCache()方法是ASP.Net Core的AddNDistributedCache()方法的扩展。

public void ConfigureServices(IServiceCollection services)
        {
            services.AddNCacheDistributedCache(configuration =>
            {
                configuration.CacheName = "IDGDistributedCache";
                configuration.EnableLogs = true;
                configuration.ExceptionsEnabled = true;
            });          
 services.AddMvc().SetCompatibilityVersion
(CompatibilityVersion.Version_2_2);
        }

这就是您需要做的。 现在,您可以开始在项目中使用NCache。

使用NCache在ASP.Net Core中存储和检索缓存的对象

以下代码片段说明了如何使用NCache。 下面显示的G​​etAuthor方法(如果可用)从缓存中检索Author对象。 如果Author对象在缓存中不可用,则GetAuthor方法从数据库中获取该对象,然后将该对象存储在缓存中。

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
            {
                //Write code here to fetch the author
                // object from the database
                if (author != null)
                {
                    if (_cache != null)
                    {
                        _cache.Insert(cacheKey, author, null,
                         Cache.NoAbsoluteExpiration,
                         TimeSpan.FromMinutes(10), 
                         Alachisoft.NCache.Runtime.
                         CacheItemPriority.Default);
                    }
                }
            }
            return author;
        }

这是Author类。

public class Author
    {
        public int AuthorId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Alachisoft的NCache是​​.Net的分布式缓存解决方案。 IDistributedCache接口提供了用于在ASP.Net Core中使用分布式缓存的标准API。 您可以使用它快速轻松地插入第三方缓存,例如NCache。

翻译自: https://www.infoworld.com/article/3342120/how-to-use-ncache-in-aspnet-core.html

ncache .net

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值