title: How to make IMemoryCache available in all controllers?
body: <p>I have followed this tutorial <a href="https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.2" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.2</a></p>
  
  <p>It is using Dependency Injection to make <code>IMemoryCache</code> available in specific controller only for example </p>
  
  <pre><code>public class TController : ControllerBase
  {
      public IConfiguration Configuration { get; }
  
      private IMemoryCache _cache;
  
      public TController(IConfiguration configuration, IMemoryCache memoryCache)
      {
          Configuration = configuration;
          _cache = memoryCache;
      }
  
      public IActionResult GetAccessToken()
      {
  
          string key ="IDGKey";
          string obj;
          if (!cache.TryGetValue&lt;string&gt;(key, out obj))
          {
              obj = DateTime.Now.ToString();
              _cache.Set&lt;string&gt;(key, obj);
          }
          return obj;
  
      }
  }
  </code></pre>
  
  <p>Now if I try to access <code>_cache</code> value in same namespace or in different controller </p>
  
  <pre><code>public class RController : ControllerBase
  {
      public IActionResult R()
      {
          var cb = _cache.Get("IDGKey");
          return Ok(cb);
      }
  }
  </code></pre>
  
  <p>It is giving the following error - </p>
  
  <blockquote>
    <p>The name '_cache' does not exist in the current context (CS0103)</p>
  </blockquote>
  
  <p>How can I make it _cache avilable to all controllers?</p>