[英]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.