这是Azure Redis缓存团队建议的模式:
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => { return ConnectionMultiplexer.Connect("mycache.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");});public static ConnectionMultiplexer Connection { get { return lazyConnection.Value; }}一些要点:
- 它使用Lazy
处理线程安全的初始化 - 它设置为“ abortConnect = false”,这意味着如果初始连接尝试失败,则ConnectionMultiplexer将在后台静默重试,而不是引发异常。
- 它并 没有 检查IsConnected财产,因为如果连接中断ConnectionMultiplexer会在后台自动重试。



