<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Arunkumar&#039;s Blog</title>
	<atom:link href="http://narunkumar.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://narunkumar.wordpress.com</link>
	<description>Experience on my SharePoint Work</description>
	<lastBuildDate>Tue, 10 Jan 2012 07:41:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='narunkumar.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Arunkumar&#039;s Blog</title>
		<link>http://narunkumar.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://narunkumar.wordpress.com/osd.xml" title="Arunkumar&#039;s Blog" />
	<atom:link rel='hub' href='http://narunkumar.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Object Caching Techniques SharePoint 2010</title>
		<link>http://narunkumar.wordpress.com/2012/01/10/object-caching-techniques-sharepoint-2010/</link>
		<comments>http://narunkumar.wordpress.com/2012/01/10/object-caching-techniques-sharepoint-2010/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 07:38:58 +0000</pubDate>
		<dc:creator>Arunkumar</dc:creator>
				<category><![CDATA[MOSS]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint 2010]]></category>

		<guid isPermaLink="false">http://narunkumar.wordpress.com/?p=241</guid>
		<description><![CDATA[Many developers use the Microsoft .NET Framework caching objects (for example, System.Web.Caching.Cache) to help take better advantage of memory and increase overall system performance. Many objects are not &#8220;thread safe,&#8221; however, and caching those objects can cause applications to fail and cause unexpected or unrelated user errors. Note The caching techniques discussed in this section [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=narunkumar.wordpress.com&amp;blog=4347064&amp;post=241&amp;subd=narunkumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="introduction">
<p>Many developers use the Microsoft .NET Framework caching objects (for example, <span class="input">System.Web.Caching.Cache</span>) to help take better advantage of memory and increase overall system performance. Many objects are not &#8220;thread safe,&#8221; however, and caching those objects can cause applications to fail and cause unexpected or unrelated user errors.</p>
<div class="alert">
<table>
<tbody>
<tr>
<th><strong>Note</strong></th>
</tr>
<tr>
<td>The caching techniques discussed in this section are different from the custom caching options for Web content management that are discussed in <a href="http://msdn.microsoft.com/en-us/library/aa589700.aspx">Custom Caching Overview in SharePoint Server 2010 (ECM)</a>.</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<div>
<h4><span><span class="LW_CollapsibleArea_Title">Caching Data and Objects</span></span></h4>
<div class="LW_CollapsibleArea_HrDiv">
<hr class="LW_CollapsibleArea_Hr" />
</div>
</div>
<div class="sectionblock">
<p>Caching is a good way to improve system performance. However, you must weigh the benefits of caching against the need for thread safety, because some SharePoint objects are not thread safe and caching causes them to perform in unexpected ways.</p>
<h4 class="subHeading">Caching SharePoint Objects That Are Not Thread Safe</h4>
<div class="subsection">
<p>You might try to increase performance and memory usage by caching <span class="input">SPListItemCollection</span> objects that are returned from queries. In general, this is a good practice; however, the <span class="input">SPListItemCollection</span> object contains an embedded <span class="input">SPWeb</span> object that is not thread safe and should not be cached.</p>
<p>For example, assume the <span class="input">SPListItemCollection</span> object is cached in a thread. As other threads try to read this object, the application can fail or behave strangely because the embedded <span class="input">SPWeb</span> object is not thread safe. For more information about the <span class="input">SPWeb</span> object and thread safety, see the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx">Microsoft.SharePoint.SPWeb</a> class.</p>
<p>The guidance in the following section describes how you can prevent problems that occur when you cache SharePoint objects that are not thread-safe in a multithreaded environment.</p>
</div>
<h4 class="subHeading">Understanding the Potential Disadvantages of Thread Synchronization</h4>
<div class="subsection">
<p>You might not be aware that your code is running in a multithreaded environment (by default, Internet Information Services, or IIS, is multithreaded) or how to manage that environment. The following example shows code that is sometimes used to cache <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitemcollection.aspx">Microsoft.SharePoint.SPListItemCollection</a> objects that are not thread safe.</p>
<p><strong>Bad Coding Practice</strong></p>
<p><em>Caching an object that multiple threads might read</em></p>
<div class="codeSnippetContainer">
<div class="codeSnippetContainerCodeContainer">
<div id="CodeSnippetContainerCode_1f73bf81-8263-4336-8666-713cefbee0a3" class="codeSnippetContainerCode" dir="ltr">
<div style="color:black;">
<pre><span style="color:blue;">public</span> <span style="color:blue;">void</span> CacheData()
{
   SPListItemCollection oListItems;

   oListItems = (SPListItemCollection)Cache[<span style="color:#a31515;">"ListItemCacheName"</span>];
   <span style="color:blue;">if</span>(oListItems == <span style="color:blue;">null</span>)
   {
      oListItems = DoQueryToReturnItems();
      Cache.Add(<span style="color:#a31515;">"ListItemCacheName"</span>, oListItems, ..);
   }
}</pre>
</div>
</div>
</div>
</div>
<p>The use of the cache in the preceding example is functionally correct; however, because the ASP.NET cache object is thread safe, it introduces potential performance problems. (For more information about ASP.NET caching, see the <a href="http://msdn2.microsoft.com/EN-US/library/8bx0tk8a" target="_blank">Cache</a> class.) If the query in the preceding example takes 10 seconds to complete, many users could try to access that page simultaneously during that amount of time. In this case, all of the users would run the same query, which would attempt to update the same cache object. If that same query runs 10, 50, or 100 times, with multiple threads trying to update the same object at the same time—especially on multiprocess, hyperthreaded computers—performance problems would become especially severe.</p>
<p>To prevent multiple queries from accessing the same objects simultaneously, you must change the code as follows.</p>
<p><strong>Applying a Lock</strong></p>
<p><em>Checking for null</em></p>
<div class="codeSnippetContainer">
<div class="codeSnippetContainerCodeContainer">
<div id="CodeSnippetContainerCode_951ed40d-8782-4ac4-8693-73d6edc1783a" class="codeSnippetContainerCode" dir="ltr">
<div style="color:black;">
<pre><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">object</span> _lock =  <span style="color:blue;">new</span> <span style="color:blue;">object</span>();

<span style="color:blue;">public</span> <span style="color:blue;">void</span> CacheData()
{
   SPListItemCollection oListItems;

   <span style="color:blue;">lock</span>(_lock)
   {
      oListItems = (SPListItemCollection)Cache[<span style="color:#a31515;">"ListItemCacheName"</span>];
      <span style="color:blue;">if</span>(oListItems == <span style="color:blue;">null</span>)
      {
         oListItems = DoQueryToReturnItems();
         Cache.Add(<span style="color:#a31515;">"ListItemCacheName"</span>, oListItems, ..);
     }
   }
}</pre>
</div>
</div>
</div>
</div>
<p>You can increase performance slightly by placing the lock inside the <span class="code">if(oListItems == null)</span> code block. When you do this, you do not need to suspend all threads while checking to see if the data is already cached. Depending on the time it takes the query to return the data, it is still possible that more than one user might be running the query at the same time. This is especially true if you are running on multiprocessor computers. Remember that the more processors that are running and the longer the query takes to run, the more likely putting the lock in the <span class="code">if()</span> code block will cause problems. To ensure that another thread has not created <span class="code">oListItems</span> before the current thread has a chance to work on it, you could use the following pattern.</p>
<p><strong>Applying a Lock</strong></p>
<p><em>Rechecking for null</em></p>
</div>
</div>
</div>
<div id="CodeSnippetContainerCode_8c6af057-30e3-4e00-9d11-1fa054fef1ed" class="codeSnippetContainerCode" dir="ltr">
<div style="color:black;">
<pre><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">object</span> _lock =  <span style="color:blue;">new</span> <span style="color:blue;">object</span>();

<span style="color:blue;">public</span> <span style="color:blue;">void</span> CacheData()
{
   SPListItemCollection oListItems;
       oListItems = (SPListItemCollection)Cache[<span style="color:#a31515;">"ListItemCacheName"</span>];
      <span style="color:blue;">if</span>(oListItems == <span style="color:blue;">null</span>)
      {
         <span style="color:blue;">lock</span> (_lock)
         {
              <span style="color:green;">// Ensure that the data was not loaded by a concurrent thread </span>
              <span style="color:green;">// while waiting for lock.</span>

              oListItems = (SPListItemCollection)Cache[“ListItemCacheName”];
              <span style="color:blue;">if</span> (oListItems == <span style="color:blue;">null</span>)
              {
                   oListItems = DoQueryToReturnItems();
                   Cache.Add(<span style="color:#a31515;">"ListItemCacheName"</span>, oListItems, ..);
              }
         }
     }
}</pre>
</div>
</div>
<p>If the cache is already populated, this last example performs as well as the initial implementation. If the cache is not populated and the system is under a light load, acquiring the lock will cause a slight performance penalty. This approach should significantly improve performance when the system is under a heavy load, because the query is executed only once instead of multiple times, and queries are usually expensive compared with the cost of synchronization.</p>
<p>The code in these examples suspends all other threads in a critical section running in IIS, and prevents other threads from accessing the cached object until it is completely built. This addresses the thread synchronization issue; however, the code is still not correct because it is caching an object that is not thread safe.</p>
<p>To address thread safety, you can cache a <span class="input">DataTable</span> object that is created from the <span class="input">SPListItemCollection</span> object. You would modify the previous example as follows so that your code gets the data from the <span class="input">DataTable</span> object.</p>
<p><strong>Good Coding Practice</strong></p>
<p><em>Caching a DataTable object</em></p>
<div class="codeSnippetContainer">
<div class="codeSnippetContainerCodeContainer">
<div id="CodeSnippetContainerCode_f8ca0ce7-af48-4248-8364-ce699c67cfa1" class="codeSnippetContainerCode" dir="ltr">
<div style="color:black;">
<pre><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">object</span> _lock =  <span style="color:blue;">new</span> <span style="color:blue;">object</span>();

<span style="color:blue;">public</span> <span style="color:blue;">void</span> CacheData()
{
   DataTable oDataTable;
   SPListItemCollection oListItems;
   <span style="color:blue;">lock</span>(_lock)
   {
           oDataTable = (DataTable)Cache[<span style="color:#a31515;">"ListItemCacheName"</span>];
           <span style="color:blue;">if</span>(oDataTable == <span style="color:blue;">null</span>)
           {
              oListItems = DoQueryToReturnItems();
              oDataTable = oListItems.GetDataTable();
              Cache.Add(<span style="color:#a31515;">"ListItemCacheName"</span>, oDataTable, ..);
           }
   }
}</pre>
</div>
</div>
</div>
</div>
<p class="cl_CollapsibleArea_expanding LW_CollapsibleArea_Img">For more information and examples of using the <span class="input">DataTable</span> object, and other good ideas for developing SharePoint applications, see the reference topic for the <a href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx">DataTable</a> class.</p>
<div>
<div class="LW_CollapsibleArea_TitleDiv">
<div>
<div class="LW_CollapsibleArea_HrDiv">
<hr class="LW_CollapsibleArea_Hr" />
</div>
</div>
</div>
<div class="sectionblock"></div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/narunkumar.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/narunkumar.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/narunkumar.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/narunkumar.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/narunkumar.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/narunkumar.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/narunkumar.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/narunkumar.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/narunkumar.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/narunkumar.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/narunkumar.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/narunkumar.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/narunkumar.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/narunkumar.wordpress.com/241/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=narunkumar.wordpress.com&amp;blog=4347064&amp;post=241&amp;subd=narunkumar&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://narunkumar.wordpress.com/2012/01/10/object-caching-techniques-sharepoint-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21608a2d6384b52c5230b7c3c17ceb51?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Arunkumar</media:title>
		</media:content>
	</item>
	</channel>
</rss>
