title: Handling Azure Redis Cache exceptions
body: <p>I'm using Azure Redis Cache for development and wanted to verify the way I'm handling the exceptions. According to the best practices, it's possible to face RedisConnectionExceptions and to resolve this, we have to dispose the old ConnectionMultiplexer and create a new one. If abortConnect is set to false, then the multiplexer will silently retry connecting without throwing the error. So if the exception is thrown, it will only be after some attempts to reconect and still failing. Is my understanding of this correct?
  This is my connection string - </p>
  
  <pre><code>cachename.redis.cache.windows.net:6380,password=Password,ssl=True,abortConnect=False
  </code></pre>
  
  <p>I believe the connection exception will only occus when you try to call GetConnection() on the multiplexer. Find my Code below - </p>
  
  <pre class="lang-cs prettyprint-override"><code>    static Lazy&lt;ConnectionMultiplexer&gt; multiplexer = CreateMultiplexer();
  
      public static ConnectionMultiplexer GetConnection() =&gt; multiplexer.Value;
  
      private static Lazy&lt;ConnectionMultiplexer&gt; CreateMultiplexer()
      {
          return new Lazy&lt;ConnectionMultiplexer&gt;(() =&gt; ConnectionMultiplexer.Connect(connectionString));
      }
  
      private static void CloseMultiplexer(Lazy&lt;ConnectionMultiplexer&gt; oldMultiplexer)
      {
          if (oldMultiplexer != null)
          {
              oldMultiplexer.Value.Close();
          }
      }
  
      public static void Reconnect()
      {
          var oldMultiplexer = multiplexer;
          CloseMultiplexer(multiplexer);
          multiplexer = CreateMultiplexer();
      }
  </code></pre>
  
  <p>And I'm Consuming this below in another class - </p>
  
  <pre class="lang-cs prettyprint-override"><code>    public class RedisCacheManager
      {
          private static IDatabase _cache;
  
          private TimeSpan expiry = new TimeSpan(hours: 6, minutes: 0, seconds: 0);
  
          public RedisCacheManager()
          {
              try
              {
                  _cache = RedisCacheHelper.GetConnection().GetDatabase();
              }
              catch(RedisConnectionException)
              {
                  RedisCacheHelper.Reconnect();
                  new RedisCacheManager();
              }
          }
  
          public async Task&lt;RedisValue[]&gt; GetFromCacheAsync(List&lt;string&gt; keys)
          {
              var cacheValues = await _cache.StringGetAsync(keys.Select(k =&gt; (RedisKey)k).ToArray());
              return cacheValues;
          }
  
          public async Task SaveInCacheAsync&lt;TValue&gt;(Dictionary&lt;string, TValue&gt; kvps)
          {
              var tasks = new List&lt;Task&gt;();
  
              foreach(var kvp in kvps)
              {
                  tasks.Add(_cache.StringSetAsync(kvp.Key, JsonConvert.SerializeObject(kvp), expiry));
              }
              await Task.WhenAll(tasks);
          }
          }
  </code></pre>
  
  <p>I'm not sure id calling the constructor in the catch block is a good practice. And are there any other exceptions that I would need to handle while calling StringGetAsync and StringSetAsync?</p>