加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:56:47 所属栏目:百科 来源:网络整理
导读:我已经读过,为了连接到Azure Redis缓存,最好遵循以下做法: private static ConnectionMultiplexer Connection { get { return LazyConnection.Value; } } private static readonly LazyConnectionMultiplexer LazyConnection = new LazyConnectionMultiplex
我已经读过,为了连接到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的创建,直到第一次使用它为止.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读