博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在 ASP.Net Core 中使用 NCache
阅读量:4034 次
发布时间:2019-05-24

本文共 2766 字,大约阅读时间需要 9 分钟。

虽然 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

使用 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);        }    }

配置 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();        }

使用 NCache 进行CURD

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

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

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

        public async Task
 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;        }

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

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

转载地址:http://boudi.baihongyu.com/

你可能感兴趣的文章
linux安装usb wifi接收器
查看>>
用防火墙自动拦截攻击IP
查看>>
补充自动屏蔽攻击ip
查看>>
谷歌走了
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
SQL 多表联合查询
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
在Dll中调用自身的位图资源
查看>>
IP校验和详解
查看>>
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>