title: Implementing a simple local memory cache on an Azure instance
body: <p>I'm looking for a simple way to implement a <strong>local memory store</strong> which can be used on an Azure .NET instance</p>
  
  <p>I've been looking at <a href="http://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/" rel="nofollow">Azure Co-located Caching</a> and it seems to support all of my requirements:</p>
  
  <ol>
  <li><p>Work on both web roles and worker roles</p></li>
  <li><p>Implement a simple LRU</p></li>
  <li><p>Keep cached objects in memory (RAM)</p></li>
  <li><p>Allow me to define the cache size as a percentage of the machine's total RAM</p></li>
  <li><p>Keep the cache on the same machine of the web/worker role (co-located mode)</p></li>
  <li><p>Allow me to access the same cache from multiple AppDomains running on the same machine (Web Roles may split my handlers into different AppDomains)</p></li>
  </ol>
  
  <p>The only problem I have with Azure Co-located caching is that different instances communicate and try to share their caches - and I don't really need all that.</p>
  
  <p>I want every machine to have its own separate in-memory cache. When I query this cache, I don't want to waste <strong>any</strong> time on making a network request to other instances' caches.</p>
  
  <p><strong>Local Cache config?</strong></p>
  
  <p>I've seen a configuration setting in Azure Caching to enable a <a href="http://msdn.microsoft.com/en-us/library/windowsazure/hh914126.aspx" rel="nofollow">Local Cache</a> - but it still seems like machines may communicate with each other (ie. during cache miss). This config also requires a <code>ttlValue</code> and <code>objectCount</code> and I want TTL to be "forever" and the object count to be "until you fill the entire cache". It feels like specifying <code>maxInt</code> in both cases seems wrong.</p>
  
  <p><strong>What about a simple static variable?</strong></p>
  
  <p>When I really think about it, all this Azure caching seems like a bit of an overkill for what I need. I basically just need a static variable in the application/role level.. except that doesn't work for requirement #6 (different AppDomains). Requirement #4 is also a bit harder to implement in this case.</p>
  
  <p><strong>Memcached</strong></p>
  
  <p>I think good old <code>memcached</code> seems to do exactly what I want. Problem is I'm using Azure as a PaaS and I don't really want to administer my own VM's. I don't think I can install <code>memcached</code> on my roles.. [UPDATE] It seems it is possible to run memcached locally on my roles. Is there a more elegant "native" solution without using memcached itself?</p>