Quantcast
Channel: State Street GangState Street Gang » Category » ASP.NET
Viewing all articles
Browse latest Browse all 5

Safest Way to Get the Cache Object

$
0
0

This is one of those “I keep forgetting this so I’d better put it in a blog post” blog posts. 

We’ve all seen this:

<span style="font-weight: bold; color: #2f2fff">object</span><span> </span><span style="color: navy">bar</span><span> </span><span style="color: fuchsia">=</span><span> </span><span style="color: #2b91af">HttpContext</span><span style="color: fuchsia">.</span><span style="color: navy">Current</span><span style="color: fuchsia">.</span><span style="color: navy">Cache</span><span>[</span><span style="background: #f3f8f3; color: green">"foo"</span><span>];</span>

And we’ve all had null reference exceptions thrown because of it.  Usually, we duct tape it back together with the pitiful:

<span style="font-weight: bold; color: #2f2fff">if</span><span> (</span><span style="color: #2b91af">HttpContext</span><span style="color: fuchsia">.</span><span style="color: navy">Current</span><span> </span><span style="color: fuchsia">==</span><span> </span><span style="font-weight: bold; color: #2f2fff">null</span><span>)</span>
<span style="font-weight: bold; color: #2f2fff"><font color="#000000">    </font>throw</span><span> </span><span style="font-weight: bold; color: #2f2fff">new</span><span> </span><span style="color: #2b91af">InvalidOperationException</span>
<span style="color: #2b91af">        </span><span>(</span><span style="background: #f3f8f3; color: green">"Lol you're screwed"</span><span>);</span>
<span style="font-weight: bold; color: #2f2fff">object</span><span> </span><span style="color: navy">bar</span><span> </span><span style="color: fuchsia">=</span><span> </span><span style="color: #2b91af">HttpContext</span><span style="color: fuchsia">.</span><span style="color: navy">Current</span><span style="color: fuchsia">.</span><span style="color: navy">Cache</span><span>[</span><span style="background: #f3f8f3; color: green">"foo"</span><span>];</span>

Or we add on more logic to return a non-offensive result that is incorrect, but since there is no current context (usually happens when the web server is recycling), what do we care? 

There is a better way.  One where you will always get the cache, and it will never be null:

<span style="font-weight: bold; color: #2f2fff">object</span><span> </span><span style="color: navy">bar</span><span> </span><span style="color: fuchsia">=</span><span> </span><span style="color: #2b91af">HttpRuntime</span><span style="color: fuchsia">.</span><span style="color: navy">Cache</span><span>[</span><span style="background: #f3f8f3; color: green">"foo"</span><span>];</span>

No matter what’s going on, the HttpRuntime’s cache property will never be null.  And it’s the same object as that in HttpContext.Current.  If you don’t believe me, just slap the following in any ASP.NET page:

<span style="color: #2b91af">Assert</span><span style="color: fuchsia">.</span><span style="color: navy">IsTrue</span><span>(</span><span style="font-weight: bold; color: #2f2fff">object</span><span style="color: fuchsia">.</span><span style="color: navy">Equals</span><span>(</span><span style="color: #2b91af">HttpContext</span><span style="color: fuchsia">.</span><span style="color: navy">Current</span><span style="color: fuchsia">.</span><span style="color: navy">Cache</span><span>,</span><span style="color: #2b91af">HttpRuntime</span><span style="color: fuchsia">.</span><span style="color: navy">Cache</span><span>);</span>

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images