title: Multiple MemoryCache instances in .NET Core
body: <p>We have two types of data we need to cache using MemoryCache.</p>
  
  <ol>
  <li>Security token</li>
  <li>Book (for eg)</li>
  </ol>
  
  <p>We don't want to set the SizeLimit on Security token cache as this will mean each entry into the cache will need to specify the size as per <a href="https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-3.0#use-setsize-size-and-sizelimit-to-limit-cache-size" rel="nofollow noreferrer">.NET Core doco</a>. This is because the security tokens are inserted by a compiled nuget type that we don't have control over.</p>
  
  <p>So it makes sense to create another instance of MemoryCache to hold the Book items only so we can set the size limit and size of the item while adding a book to the cache.</p>
  
  <p>Also, .NET Core documentation suggests that developers need to manage the limit of cache.</p>
  
  <p>I've read conflicting posts <a href="https://stackoverflow.com/questions/6140704/single-vs-multiple-memorycache-instances">here</a> and <a href="https://stackoverflow.com/questions/8463962/using-multiple-instances-of-memorycache">here</a> which support/reject multiple memory cache instances.  Additionally, these are related to .NET and not .NET Core.</p>
  
  <p>Since .NET Core's MemoryCache requires developer managed eviction of cached items and considering we'll have a lot more books than tokens in the cache, we're leaning towards having 2 instances of MemoryCache.</p>
  
  <ol>
  <li>Security token - without any size limits which has setters in compiled nuget packages</li>
  <li>Books - with size limits as all the setters are within the project code</li>
  </ol>
  
  <p>Is there any possible issues that we may run into by having multiple instances of MemoryCache in our .NET Core web application?</p>
  
  <p>We're using <em>.NET Core 2.1</em> with <em>Microsoft.Extensions.Caching.Memory v=2.1.2.0</em></p>