Reputation: 4578
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.
Here's the caching code as it stands:
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
And average page load time of ~35ms (bar one anomaly):
After making a change to use HttpContext.Items, average page load time has drastically increased:
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
Average page load time has jumped from 35 ms to 1,000 - 1,200ms!
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.
Upvotes: 2
Views: 375
Reputation: 4578
I forgot to follow-up with the answer to this question - basically, it's not slower. If a product image was missing from the local website's image folder, the application would make an HTTP request to the live site to download that product's image and sets a cache key to save that it had made this request, to avoid having to make these lookups on subsequent page refreshes.
This was dependent on using in-RAM cache to store whether it had attempted to download a product image, and after changing this to use HttpContext.Items, the application would only remember that it'd attempted these downloads for the duration of that request, and then forget it - so refreshing the page would trigger a whole load of HTTP requests again to the live site to download the product images, which is done server-side and takes a lot longer.
Silly me :)
Upvotes: 1