[英]Multiple types of Distributed Cache in ASP.NET Core
假設我有一個ASP.NET Core 2.x應用程序。
我想將Redis用於標准的IDistributedCache
依賴項注入,但是將SQL Server分布式緩存用作Session中間件的支持。
這可能嗎? 如果是這樣,您將如何在Startup.cs
配置?
分布式會話狀態存儲默認情況下會注入IDistributedCache
實例。 這意味着如果要將會話狀態用於SQL Server分布式緩存,則應將其配置為默認緩存。
為了您自己的緩存目的,您可以創建一個“包裝器接口”,專門表示Redis緩存(例如IRedisCache
),進行注冊並將其注入到中間件/控制器/服務中。 例如:
public interface IRedisDistributedCache : IDistributedCache
{
}
public void ConfigureServices(IServiceCollection services)
{
// Add Redis caching
services.AddDistributedRedisCache();
services.AddSingleton<IRedisDistributedCache, RedisCache>();
// Add SQL Server caching as the default cache mechanism
services.AddDistributedSqlServerCache();
}
public class FooController : Controller
{
private readonly IRedisDistributedCache _redisCache;
public FooController(IRedisDistributedCache redisCache)
{
_redisCache = redisCache;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.