如何在ASP.Net Core使用分布式缓存的实现
ASP.NetCore提供了多种类型的缓存,除了内存缓存和响应缓存之外,还提供了对分布式缓存的支持。在之前的一篇文章中,我讨论了ASP.NetCore的内存缓存。在本文中,我们将讨论如何在ASP.NetCore中使用分布式缓存,本篇就拿Redis和SQLServer作为演示。
什么是分布式缓存
分布式缓存可用于提高应用程序的性能和可伸缩性,通常分布式缓存被多个应用服务器共享,在分布式缓存中,缓存的数据不会落在某些个别的web服务器内存中,这些缓存数据采用集中化存储,这样多个应用服务器都可以直接使用,这样做的好处在于,如果任何一个服务器宕机或者停止响应,其他的服务器仍然能够检索缓存的数据。分布式缓存的另一个优点是,缓存的数据在服务器重启后仍然存在,当你的应用集群扩展时,并不会对缓存服务器造成任何影响。
要想在ASP.NETCore中使用分布式缓存,需要用到IDistributedCache接口,在下一节中,我们将会一起讨论IDistributedCache和IMemoryCache接口的区别。
IDistributedCache接口
在.NetCore中用于分布式缓存的IDistributedCache接口要比单机版的IMemoryCache接口更复杂,先来看一下IMemoryCache接口定义。
publicinterfaceIMemoryCache:IDisposable
{
boolTryGetValue(objectkey,outobjectvalue);
ICacheEntryCreateEntry(objectkey);
voidRemove(objectkey);
}
IDistributedCache接口是为webfarm场景设计的,它包含了一组同步和异步方法,可用于对缓存的Add,Remove,Retrieve操作,下面是IDistributedCache接口的定义。
publicinterfaceIDistributedCache
{
byte[]Get(stringkey);
TaskGetAsync(stringkey);
voidSet(stringkey,byte[]value,DistributedCacheEntryOptionsoptions);
TaskSetAsync(stringkey,byte[]value,DistributedCacheEntryOptionsoptions);
voidRefresh(stringkey);
TaskRefreshAsync(stringkey);
voidRemove(stringkey);
TaskRemoveAsync(stringkey);
}
有一点值得注意,上面的Set方法的value仅支持byte[],有点坑哈,当然你要塞入string的话,不用担心,ASP.NETCore也提供了扩展方法对其进行支持.
如何使用Redis作为缓存介质
可以通过Nuget来安装如下扩展包,代码如下:
Install-PackageMicrosoft.Extensions.Caching.Redis
为了能够把Redis作为应用底层缓存,需要使用AddDistributedRedisCache()扩展方法,下面的代码展示了如何去配置:
publicvoidConfigureServices(IServiceCollectionservices)
{
services.AddMvc();
services.AddDistributedRedisCache(option=>
{
option.Configuration="localhost";
option.InstanceName="IDG";
});
}
如何注入到Controller
下面的代码清单展示了如何将IDistributedCache注入到Controller中并实现从Redis中进行插入和读取。
publicclassDefaultController:Controller
{
privatereadonlyIDistributedCache_distributedCache;
publicHomeController(IDistributedCachedistributedCache)
{
_distributedCache=distributedCache;
}
[HttpGet]
publicasyncTaskGet()
{
varcacheKey="IDG";
vardata=_distributedCache.GetString(cacheKey);
if(!string.IsNullOrEmpty(data))
{
returndata;//returnedfromCache
}
else
{
stringstr="HelloWorld";
_distributedCache.SetString(cacheKey,str);
returnstr;
}
}
}
如何使用SqlServer作为缓存介质
要想将SqlServer作为底层的缓存介质,需要通过Nuget安装如下包:
Install-PackageMicrosoft.Extensions.Caching.SqlServer Install-PackageMicrosoft.Extensions.Caching.SqlConfig.Tools
如何在Startup.ConfigureServices()中做如下配置。
publicvoidConfigureServices(IServiceCollectionservices)
{
services.AddControllersWithViews();
services.AddDistributedSqlServerCache(x=>
{
x.ConnectionString=Configuration["ConnectionStrings:Default"];
x.SchemaName="dbo";
x.TableName="IDGCache";
});
}
接下来通过如下命令在SqlServer中生成Table来存放缓存数据,代码如下:
dotnetsql-cachecreate
ASP.NetCore提供了分布式缓存的高层抽象。因此,无论底层缓存介质是Redis还是SQLServer,IDistributedCache接口都提供了统一并且便捷的操控Cache的API,而且IDistributedCache注入到Controller中也是非常方便的。
译文链接:https://www.infoworld.com/article/3262990/how-to-implement-a-distributed-cache-in-aspnet-core.html
到此这篇关于如何在ASP.NetCore使用分布式缓存的实现的文章就介绍到这了,更多相关ASP.NetCore分布式缓存内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
热门推荐
- 返回顶部
- 3162201930
- czq8825@qq.com