ASP.net MVC 中的 Azure Redis StackExchange.Redis ConnectionMultiplexer
Azure Redis StackExchange.Redis ConnectionMultiplexer in ASP.net MVC
我读到为了连接到 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 中调用它,还是我应该为每个控制器初始化一次,或者 smth。否则?
我还有一个任务是与应用程序通信的服务,所以如果我想在服务内部与 Redis 通信,我应该将 ConnectionMultiplexer 的实例发送到控制器的服务,还是应该在我的所有服务中初始化它,或者?
如您所见,我在这里有点迷路,所以请帮忙!
文档是正确的,您应该只有一个 ConnectionMultiplexer 实例并重用它。请勿创建多个,建议为shared and reused。
现在对于创建部分,它不应该在 Controller 或 Global.asax 中。通常你应该有自己的 RedisCacheClient class (可能实现一些 ICache 接口),它在内部使用 ConnectionMultiplexer 私有静态实例,这就是你的创建代码应该在的地方 - 就像你在问题中写的那样。 Lazy 部分会将 ConnectionMultiplexer 的创建推迟到第一次使用时。
亲爱的;
您可以使用以下代码重用 StackExchange.Redis ConnectionMultiplexer。它可以在代码的任何层中使用。
public class RedisSharedConnection
{
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString);
connectionMultiplexer.PreserveAsyncOrder = false;
return connectionMultiplexer;
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
}
我读到为了连接到 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 中调用它,还是我应该为每个控制器初始化一次,或者 smth。否则?
我还有一个任务是与应用程序通信的服务,所以如果我想在服务内部与 Redis 通信,我应该将 ConnectionMultiplexer 的实例发送到控制器的服务,还是应该在我的所有服务中初始化它,或者?
如您所见,我在这里有点迷路,所以请帮忙!
文档是正确的,您应该只有一个 ConnectionMultiplexer 实例并重用它。请勿创建多个,建议为shared and reused。
现在对于创建部分,它不应该在 Controller 或 Global.asax 中。通常你应该有自己的 RedisCacheClient class (可能实现一些 ICache 接口),它在内部使用 ConnectionMultiplexer 私有静态实例,这就是你的创建代码应该在的地方 - 就像你在问题中写的那样。 Lazy 部分会将 ConnectionMultiplexer 的创建推迟到第一次使用时。
亲爱的;
您可以使用以下代码重用 StackExchange.Redis ConnectionMultiplexer。它可以在代码的任何层中使用。
public class RedisSharedConnection
{
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString);
connectionMultiplexer.PreserveAsyncOrder = false;
return connectionMultiplexer;
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
}