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

我已经读过,为了连接到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通信,我应该将ControlMultiplexer的实例发送到控制器的服务,还是应该在我的所有服务中初始化它,或者?

你可以看到我在这里有点失落,所以请帮忙!

解决方法

文档是对的,你应该只有一个ConnectionMultiplexer实例并重用它.不要创建多个,建议它将是 shared and reused.
现在对于创建部分,它不应该在Controller或Global.asax中.通常,您应该拥有自己的RedisCacheClient类(可能实现一些ICache接口),该类使用内部的ConnectionMultiplexer私有静态实例,这就是您的创建代码所在的位置 – 正如您在问题中所写的那样. Lazy部分将推迟ConnectionMultiplexer的创建,直到第一次使用它为止.

相关文章

1:最直白的循环遍历方法,可以分为遍历key--value键值对以及...
private void ClearTextBox(){ foreach (var control in pnl...
原文叫看《墨攻》理解IOC概念 2006年多部贺岁大片以让人应接...
右击文件夹-&gt;安全选项卡-&gt;添加-&gt;高级-...