<?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; csharp</title>
	<atom:link href="http://www.ducdigital.com/tag/csharp/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>Memcached and Asp.net mvc</title>
		<link>http://www.ducdigital.com/2010/01/23/memcached-and-asp-net-mvc/</link>
		<comments>http://www.ducdigital.com/2010/01/23/memcached-and-asp-net-mvc/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 16:48:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Asp.net MVC]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[enyim memcached]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linq to sql]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[MemcachedProviders]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=220</guid>
		<description><![CDATA[Today I was hanging around on Google and found a lot of useful stuff about Memcached and .net. Currently, I am using System.Web.Cache to cache objects for my webpage stuff. It's good, but not enough for scalable since it's often remove my object when run on low memory. So I go on with Memcached instead. [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was hanging around on Google and found a lot of useful stuff about Memcached and .net. Currently, I am using System.Web.Cache to cache objects for my webpage stuff. It's good, but not enough for scalable since it's often remove my object when run on low memory.  So I go on with Memcached instead. I found a lot of library which could help us develop faster. Here are some library/solution that I found:</p>
<p><a href="http://ayende.com/Blog/archive/2008/06/06/Scratching-an-itch-NMemcached.aspx">http://ayende.com/Blog/archive/2008/06/06/Scratching-an-itch-NMemcached.aspx</a><br />
<a href="http://www.codeproject.com/KB/aspnet/memcached_aspnet.aspx">http://www.codeproject.com/KB/aspnet/memcached_aspnet.aspx</a><br />
<a href="http://maxi326.wordpress.com/2009/11/02/use-memcached-in-net-application-with-linq/">http://maxi326.wordpress.com/2009/11/02/use-memcached-in-net-application-with-linq/</a><br />
<a href="http://zvolkov.com/blog/post/2009/06/18/Using-MemCached-with-NHibernate.aspx">http://zvolkov.com/blog/post/2009/06/18/Using-MemCached-with-NHibernate.aspx</a><br />
<a href="http://latebound.blogspot.com/2008/10/using-memcached-from-c.html">http://latebound.blogspot.com/2008/10/using-memcached-from-c.html</a><br />
<a href="http://www.codeplex.com/memcachedproviders">http://www.codeplex.com/memcachedproviders</a><br />
<a href="http://code.google.com/p/beitmemcached/">http://code.google.com/p/beitmemcached/</a><br />
<a href="http://sourceforge.net/projects/memcacheddotnet/">http://sourceforge.net/projects/memcacheddotnet/</a><br />
<a href="http://www.codeplex.com/EnyimMemcached/">http://www.codeplex.com/EnyimMemcached/</a><br />
and the list go on...</p>
<p>What is MemCached? To put this in simple term, it's a cache store / hash table, where you can store almost everything, like objects, on it. Basically, it's very useful since there are parts where you don't want to cache your whole webpage but you just want to cache your object which you finished process or pulled out from database. It can save you hundreds of ms query time, server loads, and process time, which is very very very useful.</p>
<p>There are some downside if you run Memcached on Windows, <del datetime="2010-01-22T12:46:56+00:00">the latest update for Memcached for windows that i can found is 1.2.5</del><a href="http://blog.northscale.com/northscale-blog/2009/12/memcached-144-windows-32bit-binary-now-available.html"> I found a latest version here: 1.4.4</a>, and the lates for linux is 1.4.4 (at the time I write this post). There are some solution for this problem. By using another computer/server installed as linux, you can solely run Memcached for cache purpose on multiple server. If you are not having more than 1 server, then just fall back to use System.Web.Cache since it's quite good for production already. You can also try out Velocity, which currently on it's CTP and I am not sure about whether Microsoft going to charge users for this.</p>
<p>I suggest you use MemCached Provider, it's the simplest to implement memcached in asp.net mvc, the following web.config instruction provided by MemCached Provider</p>
<p>Open Web.config file and add the following section to configSections tag</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cacheProvider&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">allowDefinition</span>=<span style="color: #ff0000;">&quot;MachineToApplication&quot;</span> <span style="color: #000066;">restartOnExternalChanges</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sectiongroup</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;enyim.com&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;memcached&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sectiongroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;log4net&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Add the following section to configure Enyim’s client to point to Memcached servers</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;enyim</span> .com<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;memcached<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- put your own server(s) here--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;127.0.0.1&quot;</span> <span style="color: #000066;">port</span>=<span style="color: #ff0000;">&quot;11211&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;socketpool</span> <span style="color: #000066;">minPoolSize</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">maxPoolSize</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">connectionTimeout</span>=<span style="color: #ff0000;">&quot;00:00:10&quot;</span> <span style="color: #000066;">deadTimeout</span>=<span style="color: #ff0000;">&quot;00:02:00&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/memcached<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/enyim<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Add the following section to configure Memcached Cache Provider. keySuffix attribute allows for adding suffix to cache provider keys  in order to simulate namespaces.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cacheprovider</span> <span style="color: #000066;">defaultProvider</span>=<span style="color: #ff0000;">&quot;MemcachedCacheProvider&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MemcachedCacheProvider&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">keySuffix</span>=<span style="color: #ff0000;">&quot;_MySuffix_&quot;</span> <span style="color: #000066;">defaultExpireTime</span>=<span style="color: #ff0000;">&quot;2000&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/providers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cacheprovider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p> Add following section to configure log4net</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;log4net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- Define some output appenders --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ConsoleAppender&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Appender.ConsoleAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Layout.PatternLayout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;conversionpattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;%date [%thread] %-5level %logger [%property{NDC}]</span>
<span style="color: #009900;">- %message%newline&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!--&lt;threshold value=&quot;OFF&quot; /&gt;--&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- Setup the root category, add the appenders and set the default priority --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;priority</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;WARN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> -ref <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;ConsoleAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Filter.LevelRangeFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;levelmin</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;WARN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;levelmax</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;FATAL&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/log4net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Provided you include all the reference libraries, for example, this is my category controller, which will cache the object if it's not existed in the MemCached's memory.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> ActionResult Category<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var returnObj <span style="color: #008000;">=</span> DistCache.<span style="color: #0000FF;">Get</span><span style="color: #008000;">&lt;</span>list <span style="color: #008000;">&lt;</span>CategoryObject<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;fullCatListObject&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>returnObj <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                P015.<span style="color: #0000FF;">Modules</span>.<span style="color: #0000FF;">Categories</span>.<span style="color: #0000FF;">Categories</span> _cat <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> P015.<span style="color: #0000FF;">Modules</span>.<span style="color: #0000FF;">Categories</span>.<span style="color: #0000FF;">Categories</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                returnObj <span style="color: #008000;">=</span> _cat.<span style="color: #0000FF;">getCatObject</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                DistCache.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;fullCatListObject&quot;</span>, returnObj, <span style="color: #008000;">new</span> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">29</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;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>returnObj<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>list<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>One thing you need to remember when using this library is to have all your objects that you want to save in memcached serialized. For example, this is my CategoryObject class use for the above example:</p>
<p>CategoryObject.cs</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #000000;">&#91;</span>Serializable<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CategoryObject
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> CAT_NAME <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> CAT_ID <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> LEVEL <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>In case you are using Linq, or more specific, linq to sql, you can open your DBML file in design mode, change the <em>Serialize Mode</em> option to <strong>Unidirectional</strong>.</p>
<p>This works for me, so it should works for you too. Enjoy <img src='http://www.ducdigital.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2010%2F01%2F23%2Fmemcached-and-asp-net-mvc%2F&amp;linkname=Memcached%20and%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/23/memcached-and-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
		<item>
		<title>Convert latin accent to non-latin accent (Normalization Form)</title>
		<link>http://www.ducdigital.com/2009/11/30/convert-latin-accent-to-non-latin-accent/</link>
		<comments>http://www.ducdigital.com/2009/11/30/convert-latin-accent-to-non-latin-accent/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 08:20:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Asp.net MVC]]></category>
		<category><![CDATA[Programing]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://www.ducdigital.com/?p=134</guid>
		<description><![CDATA[I'm sure some of you have been tried to remove the special accent sign like "ắ ế í u ư đ" and replace it to a new string without accent for tagging, sluging, permalink purpose in your webpage... this function use for asp.net / C# to replace everything to Normalization Form... Tiếng việt: Chắc hẳn [...]]]></description>
			<content:encoded><![CDATA[<p>I'm sure some of you have been tried to remove the special accent sign like "ắ ế í u ư đ" and replace it to a new string without accent for tagging, sluging, permalink purpose in your webpage...</p>
<p>this function use for asp.net / C# to replace everything to Normalization Form...</p>
<p>Tiếng việt:</p>
<p>Chắc hẳn một số bạn muốn thay các chữ có dấu tiếng vịêt thành chữ không dấu để lưu vào cơ sở dữ liệu, làm permalink hoặc một số chức năng khác, đây là function để thay thế toàn bộ chữ có dấu thành không dấu trong tiếng việt.</p>
<p>Chúc các bạn may mắn /  Good luck</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> LatinToAscii<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> InString<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> newString <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>, charString<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">char</span> ch<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> charsCopied<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> InString.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                charString <span style="color: #008000;">=</span> InString.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>i, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                charString <span style="color: #008000;">=</span> charString.<span style="color: #0000FF;">Normalize</span><span style="color: #000000;">&#40;</span>NormalizationForm.<span style="color: #0000FF;">FormKD</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">// If the character doesn't decompose, leave it as-is</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>charString.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
                    newString <span style="color: #008000;">+=</span> charString<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    charsCopied <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> charString.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        ch <span style="color: #008000;">=</span> charString<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                        <span style="color: #008080; font-style: italic;">// If the char is 7-bit ASCII, add</span>
&nbsp;
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ch <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">128</span><span style="color: #000000;">&#41;</span>
                        <span style="color: #000000;">&#123;</span>
                            newString <span style="color: #008000;">+=</span> ch<span style="color: #008000;">;</span>
                            charsCopied<span style="color: #008000;">++;</span>
                        <span style="color: #000000;">&#125;</span>
                    <span style="color: #000000;">&#125;</span>
                    <span style="color: #008080; font-style: italic;">/* If we've decomposed non-ASCII, give it back
                     * in its entirety, since we only mean to decompose
                     * Latin chars.
                    */</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>charsCopied <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                        newString <span style="color: #008000;">+=</span> InString.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>i, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> newString.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'đ'</span>, <span style="color: #666666;">'d'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Modify from source: <a href="http://www.codeproject.com/KB/cs/UnicodeNormalization.aspx">http://www.codeproject.com/KB/cs/UnicodeNormalization.aspx</a></pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.ducdigital.com%2F2009%2F11%2F30%2Fconvert-latin-accent-to-non-latin-accent%2F&amp;linkname=Convert%20latin%20accent%20to%20non-latin%20accent%20%28Normalization%20Form%29"><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/2009/11/30/convert-latin-accent-to-non-latin-accent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
