<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DucDigital &#187; httpcontext</title>
	<atom:link href="http://www.ducdigital.com/tag/httpcontext/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ducdigital.com</link>
	<description>for ( $girl = 1; $girl &#60; $required; $girl++ ) { echo "I love DucDigital"; }</description>
	<lastBuildDate>Sat, 07 Aug 2010 09:12:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Cache individual object in asp.net mvc</title>
		<link>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/</link>
		<comments>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 05:37:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Asp.net MVC]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[httpcontext]]></category>
		<category><![CDATA[linq to sql]]></category>
		<category><![CDATA[linq2sql]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[outputcache]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sap.net mvc]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=218</guid>
		<description><![CDATA[Upon writing a controller for exporting image on the fly, I struggled with the problem that: "Output cache does not cache HttpHeader (in this case, "Location" header)". Which lead me to despair since each time i called the controller to output image, it will have to connect to database and do a couple of query, [...]]]></description>
			<content:encoded><![CDATA[<p>Upon writing a controller for exporting image on the fly, I struggled with the problem that: "Output cache does not cache HttpHeader (in this case, "Location" header)". Which lead me to despair since each time i called the controller to output image, it will have to connect to database and do a couple of query, which is not practical in real life. So finally, i found out that i can easily cache objects into a cache provider in Asp.net Mvc. Very useful, all my queries go back to 0 (traced through Ling2sql profiler). </p>
<p>You can use this piece of code in anywhere of your code where you want to cache any object.<br />
(Notice: this code was used in a controller, so I used HttpContext.Cache. But if you use else where that make this code not working, try HttpContext.Current.Cache)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HttpContext.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ObjName&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                link <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>HttpContext.<span style="color: #0000FF;">Cache</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ObjName&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> link<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                HttpContext.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ObjName&quot;</span>, link, <span style="color: #0600FF;">null</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">NoAbsoluteExpiration</span>, <span style="color: #008000;">new</span> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">30</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">Caching</span></span>.<span style="color: #0000FF;">CacheItemPriority</span>.<span style="color: #0000FF;">High</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">return</span> link<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>What it did was to check if the ObjName exist in the context, if not, create one and dump the data into it. Remember when you call back from HttpContext, it's an object so you always need a cast. If you don't know about argument, let the intellisense do the work for you.</p>
<p>Time to exand further about the topic: <a href="http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add(VS.80).aspxx">HttpContext.Cache.Add</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2010%2F01%2F14%2Fcache-individual-object-in-asp-net-mvc%2F&amp;linkname=Cache%20individual%20object%20in%20asp.net%20mvc"><img src="http://www.ducdigital.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.ducdigital.com/2010/01/14/cache-individual-object-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
