HttpRuntime.Cache vs. HttpContext.Current.Cache in ASP.NET

Lately I'd been using the static Current property of the HttpContext object in ASP.NET to access it's Cache property and cache information in a web app. I was adding these cache entries with dependencies, so that when certain files were modified, the cache would trigger a callback function to reload. However, when testing this, I ran into an unforseen complication: when I modified a file I had specified as a dependancy, and the app tried to add the new data to the cache using HttpContext.Current.Cache, it couldn't because there was no current HttpContext.

The solution was to use HttpRuntime.Cache instead. This, like HttpContext.Current.Cache, represents the cache of the entire app, but does not require a reference to a current HttpContext. Also, I've heard it's a little bit faster.


Popular Posts

How To Mock Out Child Components In Unit Tests of Angular 2 Code

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

A Red Herring When Using Moq to Mock Methods With Optional Parameters

Unit Testing with a Mock Entity Framework DbContext and Fake DbSets