title: Why is accessing HttpContext.Items much slower than HttpContext.Cache?
body: <p>Our website currently runs on a single web server, and makes use of HttpContext.Cache. We're looking to make the site load balanced, and need to use some form of distributed cache. I've been looking at AppFabric, but I recall using HttpContext.Items as a per-request cache, and thought I may be able to leverage this to achieve similar functionality to a distributed cache.</p>
  
  <p>Here's the caching code as it stands:</p>
  
  <pre><code>Public Shared Function getFromCache(key As String) As Object
      Return HttpContext.Current.Cache(key)
  End Function
  
  Public Shared Sub addToCache(key As String, value As Object)
      addToCache(key, value, CACHE_DURATION_IN_MINUTES)
  End Sub
  
  Public Shared Sub addToCache(ByVal key As String, ByVal value As Object, ByVal durationInMinutes As Integer)
      HttpContext.Current.Cache.Insert(key, value, Nothing, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(durationInMinutes))
  End Sub
  </code></pre>
  
  <p>And average page load time of ~35ms (bar one anomaly):</p>
  
  <p><a href="https://i.stack.imgur.com/qS2Hy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qS2Hy.png" alt="Page load times with HttpContext.Cache"></a></p>
  
  <p>After making a change to use HttpContext.Items, average page load time has <em>drastically</em> increased:</p>
  
  <pre><code>Public Shared Function getFromCache(key As String) As Object
      Return HttpContext.Current.Items(key)
  End Function
  
  Public Shared Sub addToCache(key As String, value As Object)
      addToCache(key, value, CACHE_DURATION_IN_MINUTES)
  End Sub
  
  Public Shared Sub addToCache(ByVal key As String, ByVal value As Object, ByVal durationInMinutes As Integer)
      HttpContext.Current.Items.Add(key, value)
  End Sub
  </code></pre>
  
  <p>Average page load time has jumped from 35 ms to 1,000 - 1,200ms!</p>
  
  <p><a href="https://i.stack.imgur.com/ixQqQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ixQqQ.png" alt="Page load times with HttpContext.Items"></a></p>
  
  <p>Does anything look out of the odd here? It was my understanding that HttpContext.Current.Items is a per-request dictionary that is initialised before each request, so I was assuming that access to this would be as quick as using the Cache.</p>