title: Refreshing in-memory cache across multiple instances in Azure
body: <p>I have an Azure web app which uses in-memory caching, adding keys as follows</p>
  
  <pre><code>    public static void Add(object item, string key)
      {
          var wrapper = new CacheItemWrapper()
          {
              InsertedAt = DateTime.Now,
              Item = item
          };
          MemoryCache.Default.Add(key, wrapper, ObjectCache.InfiniteAbsoluteExpiration);
      }
  </code></pre>
  
  <p>Occasionally the user of our application will make a change which requires the cache to refresh. I can call a method which clears the cache, the problem is, it only works on the instance which picks up the request. Other instances still have the old values in memory.</p>
  
  <p>Is there any way I can do either of these things</p>
  
  <p>a) run a method across multiple instances, or
  b) raise an event which all instances listen for?</p>
  
  <p>The code above could be changed to expire within a short time so that all instances could pick this up. However, it's quite a long process to update the cache and this might affect performance. Given the application knows when it needs to refresh the cache, it would be much better and more responsive if it could be done programmatically.</p>