Wednesday, May 2, 2012

Caching Techniques in Sharepoint


public void ListItemCache()
{
   SPListItemCollection spListItemsObject;

   spListItemsObject = (SPListItemCollection)Cache["ListItemCacheName"];
   if(spListItemsObject == null)
   {
      spListItemsObject = DoQueryToReturnItems();
      Cache.Add("ListItemCacheName", spListItemsObject, ..);
   }
}




If you need thread safe code the try following :


private static object _lock =  new object();

public void CacheData()
{
   SPListItemCollection spListItemsObject;
       spListItemsObject = (SPListItemCollection)Cache["ListItemCacheName"];
      if(spListItemsObject == null)
      {
         lock (_lock)
         {
              // Ensure that the data was not loaded by a concurrent thread
              spListItemsObject = (SPListItemCollection)Cache["ListItemCacheName"];
              if (spListItemsObject == null)
              {
                   spListItemsObject = DoQueryToReturnItems();
                   Cache.Add("ListItemCacheName", spListItemsObject, ..);
              }
         }
     }
}


No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...