gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:56:49 24 4
gpt4 key购买 nike

我读到为了连接到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com