title: How to cache in ASP.NET?
body: <p>I have some commonly used data that I would like to load from my database and cache during <code>Application_Start</code> in my global.asax file.  I've been reading <a href="http://msdn.microsoft.com/en-us/liBRary/18c1wd61(v=vs.100).aspx" rel="nofollow">MSDN's article on caching</a>, but I'm a bit confused on the proper way to do this.  </p>
  
  <p>They example they give to insert data into cache is below:</p>
  
  <pre><code>Cache.Insert("CacheItem2", "Cached Item 2");
  </code></pre>
  
  <p>So I added the following to my global.asax:</p>
  
  <pre><code>using System.Web.Caching;
  ...
  Cache.Insert("audioVariables", audioVariables);
  </code></pre>
  
  <p>But this throws, <code>An object reference is required....</code>.  Ok, fine - so I created an instance of the Cache class like so in <code>Application_start</code>:</p>
  
  <pre><code>Cache c = new Cache();
  c.Insert("audioVariables", audioVariables);
  </code></pre>
  
  <p>When I call <code>Insert</code> it throws a <code>An unhandled exception of type 'System.NullReferenceException' occurred in System.Web.dll</code> error.</p>
  
  <p>What is the proper way for me to insert an object into cache on <code>Application_Start</code>?</p>
  
  <p>UPDATE:</p>
  
  <p>Stack trace - </p>
  
  <blockquote>
    <p>[NullReferenceException: Object reference not set to an instance of an
    object.]    System.Web.Caching.Cache.Insert(String key, Object value)
    +66    MyVerbalInk.MvcApplication.Application_Start() in c:\inetpub\VerbalInk2.0\MyVerbalInk\Global.asax.cs:26</p>
    
    <p>[HttpException (0x80004005): Object reference not set to an instance
    of an object.]<br>
    System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext
    context, HttpApplication app) +9935033<br>
    System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr
    appContext, HttpContext context, MethodInfo[] handlers) +118<br>
    System.Web.HttpApplication.InitSpecial(HttpApplicationState state,
    MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172<br>
    System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr
    appContext, HttpContext context) +336<br>
    System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr
    appContext) +296</p>
    
    <p>[HttpException (0x80004005): Object reference not set to an instance
    of an object.]    System.Web.HttpRuntime.FirstRequestInit(HttpContext
    context) +9913572<br>
    System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
    +101    System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
    wr, HttpContext context) +254</p>
  </blockquote>