![](/img/trans.png)
[英].NET Core StackExchange.Redis ConnectionMultiplexer setup for multiple redis servers
[英]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文檔:
Azure Redis緩存的連接由ConnectionMultiplexer類管理。 此類旨在在整個客戶端應用程序中共享和重用,並且不需要基於每個操作創建。
那么在我的ASP.net MVC應用程序中共享ConnectionMultiplexer的最佳實踐是什么? 它應該在Global.asax中調用,還是應該每個Controller初始化一次,或者smth。 別的?
我也有服務,負責與應用程序通信,所以如果我想在服務內部與Redis通信,我應該將ControlMultiplexer的實例發送到控制器的服務,還是應該在我的所有服務中初始化它,或者?
你可以看到我在這里有點迷失,所以請幫忙!
親愛;
您可以使用以下代碼重用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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.