c# - Azure Redis StackExchange.ASP.net MVC 中的 Redis ConnectionMultiplexer

标签 c# asp.net-mvc redis stackexchange.redis azure-redis-cache

我读到为了连接到 Azure Redis 缓存最好遵循这种做法:

private static ConnectionMultiplexer Connection { get { return LazyConnection.Value; } }

    private static readonly Lazy<ConnectionMultiplexer> LazyConnection =
        new Lazy<ConnectionMultiplexer>(
            () =>
            {
                return
                    ConnectionMultiplexer.Connect(connStinrg);
            });

根据 Azure Redis 文档:

The connection to the Azure Redis Cache is managed by the ConnectionMultiplexer class. This class is designed to be shared and reused throughout your client application, and does not need to be created on a per operation basis.

那么在我的 ASP.net MVC 应用程序中共享 ConnectionMultiplexer 的最佳实践是什么? 应该在 Global.asax 中调用它,还是应该为每个 Controller 初始化一次,或者 smth。不然呢?

我还有一个任务是与应用程序通信的服务,所以如果我想在服务内部与 Redis 通信,我应该将 ConnectionMultiplexer 的实例发送到 Controller 的服务,还是应该在我的所有服务中初始化它,或者?

如您所见,我在这里有点迷路,所以请帮忙!

最佳答案

文档是正确的,因为您应该只有一个 ConnectionMultiplexer 实例并重用它。不要创建多个,建议为shared and reused .

现在对于创建部分,它不应该在 Controller 或 Global.asax 中。通常你应该有你自己的 RedisCacheClient 类(可能实现一些 ICache 接口(interface)),它在内部使用 ConnectionMultiplexer 私有(private)静态实例,这就是你的创建代码应该所在的位置 - 就像你在问题中写的那样。 Lazy部分将延迟 ConnectionMultiplexer 的创建,直到它第一次被使用。

关于c# - Azure Redis StackExchange.ASP.net MVC 中的 Redis ConnectionMultiplexer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525273/

相关文章:

c# - WPF 绑定(bind) : cannot resolve symbol due to unknown datacontext

c# - 删除充满文件的文件夹不会立即释放空间

c# - 将 SAML token 发布到 ASP.NET MVC 网站

ubuntu - 如何保存并退出redis.conf?

c# - 统一: Register and resolve class with generic type

c# - MVC @Html.DropDownList 在 ViewBag 中使用 SelectList 时出错

c# - 如何使用带有 Serilog 的 postsharp 来分离日志记录方面?

c# - 如何用 MVC ActionFilter(或其他东西)替换基本 Controller ?

python - 如何在没有命令行界面的情况下启动 redis 队列工作程序?

php - 一对一私有(private)聊天使用套接字和 Laravel 5.1