title: Update Dictionary object in redis cache using StackExchange.Redis client
body: <p>I am using azure redis cache and StackExchange.redis client to access it. I am trying to figure out what is the best way to update dictionary values in redis. </p>
  
  <pre><code>public async Task&lt;T&gt; FetchAndCacheAsync&lt;T&gt;(string key, Func&lt;Task&lt;T&gt;&gt; retrieveData, TimeSpan absoluteExpiry)
          {
              T value;
  
              var output = await _cache.StringGetAsync(key);
              if (output.HasValue)
              {
                  value = _serializer.Deserialize&lt;T&gt;(output);
              }
              else
              {
                  value = await retrieveData();
                  await _cache.StringSetAsync(key, _serializer.Serialize(value), absoluteExpiry);
              }
  
              return value;
          }
  </code></pre>
  
  <p>_cache in the above code snippet is of type StackExchange.Redis.IDatabase.</p>
  
  <p>On initialization I store the dictionary object as</p>
  
  <p>FetchAndCacheAsync("diagnostics", () => new Dictionary());</p>
  
  <p>I have a function that will be called frequently to update the DateTime for a given key in the dictionary object. </p>
  
  <p><strong>Question:</strong> With my basic knowledge of redis cache I can only think of the following way to update a particular key value of a dictionary stored in a redis cache. </p>
  
  <pre><code>var diagnostics = FetchAndCacheAsync("diagnostics", () =&gt; new Dictionary&lt;Guid, DateTime&gt;());
  
  diagnostics[key] = value;
  _cache.KeyDeleteAsync("diagnostics");
  
  FetchAndCacheAsync("diagnostics", () =&gt; diagnostics);
  </code></pre>
  
  <p>But it doesn't seem right to me. I am sure there must be a better way to do this. Any suggestion?</p>