Thursday, August 25, 2011

Web Part Caching

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web;
namespace Zimmergren.WebParts.SampleCachePart
{
public class SimpleCache : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
base.Render(writer);

List lists = new List();
string status = "";

if (HttpRuntime.Cache["SimpleSampleCache"] == null)
{
status = "The following items are NOT fetched from the cache

";

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

HttpRuntime.Cache.Add("SimpleSampleCache",
lists,
null,
DateTime.MaxValue,
TimeSpan.FromMinutes(10),
System.Web.Caching.CacheItemPriority.Default, null);
}
else
{
status = "The following items ARE fetched from the cache!

";
lists = (List)HttpRuntime.Cache["SimpleSampleCache"];
}

writer.Write(status);
foreach (SPList l in lists)
writer.WriteLine(l.Title + " - " + l.ItemCount + " items
");
}
}
}

No comments:

Post a Comment

Popular Posts