简体   繁体   English

ASP.NET Core中的多种类型的分布式缓存

[英]Multiple types of Distributed Cache in ASP.NET Core

Suppose I have a ASP.NET Core 2.x application. 假设我有一个ASP.NET Core 2.x应用程序。

I would like to use Redis for standard IDistributedCache dependency injection, but use SQL Server Distributed cache as the backing for Session middleware. 我想将Redis用于标准的IDistributedCache依赖项注入,但是将SQL Server分布式缓存用作Session中间件的支持。

Is this possible? 这可能吗? If so, how would you go about configuring this in Startup.cs ? 如果是这样,您将如何在Startup.cs配置?

The distributed session state storage injects the IDistributedCache instance by default. 分布式会话状态存储默认情况下会注入IDistributedCache实例。 This means you should configure the SQL Server distributed cache as the default one if you would like to use that for session state. 这意味着如果要将会话状态用于SQL Server分布式缓存,则应将其配置为默认缓存。

For your own caching purposes, you could create a "wrapper interface" which specifically represents the Redis cache (such as IRedisCache ), register it and inject that in your middleware/controllers/services. 为了您自己的缓存目的,您可以创建一个“包装器接口”,专门表示Redis缓存(例如IRedisCache ),进行注册并将其注入到中间件/控制器/服务中。 For example: 例如:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM