Friday, September 9, 2011

Machine Caching in SharePoint 2010

Some caching techniques in SharePoint 2010:-

1. Object Caching (Configurable)
2. BLOB Cache (Configurable) :  Open web.config in SharePoint Root Hive:
<BlobCache location="D:\BLOB\" path="\.(gif|jpg|jpeg|jpe|jfif|bmp|dib|tif|tiff|ico|png|wdp|hdp|css|js|asf|avi|flv|m4v|mov|mp3|mp4|mpeg|mpg|rm|rmvb|wma|
wmv)$" maxSize="10" enabled="true" />

3. Caching in code (Programmable)

Important Links about Caching in SharePoint

Web Part Caching – A simple approach:-

Open Visual Studio2010 and select a WebPart form SharePoint 2010 Template:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace VisualWebPartProject1.VisualWebPart1
    public class VisualWebPart1 : WebPart
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/VisualWebPartProject1/VisualWebPart1/VisualWebPart1UserControl.ascx";
        protected override void CreateChildControls()
            Control control = Page.LoadControl(_ascxPath);

            List<SPList> lists = new List<SPList>();
            string status = "";

            if (HttpRuntime.Cache["SimpleSampleCache"] == null)
                status = "The following items are <strong>NOT</strong> fetched from the cache<br/><br/>";

                SPWeb web = SPContext.Current.Web;
                foreach (SPList list in web.Lists)

                System.Web.Caching.CacheItemPriority.Default, null);
                status = "The following items <strong>ARE</strong> fetched from the cache!<br/><br/>";
                lists = (List<SPList>)HttpRuntime.Cache["SimpleSampleCache"];
            Controls.Add(new LiteralControl(status));

            foreach (SPList l in lists)
                Controls.Add(new LiteralControl(l.Title + " - " + l.ItemCount + " items<br/>"));   

Press F5 and add WebPart to an WebPart Page and Show the Result as follows:-

Thank You.

No comments:

Post a Comment

Featured Post

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

Popular posts