Following code shows implementation of SPCache object in SharePoint 2010.
// Object for Cacheing
public class TestClass
{
public string someData { get; set; }
public TestClass(string myData)
{
this.someData = myData;
}
}
// SPCache Object Settings Cache Parameters
public class MyCache
{
public static SPCacheConfig cacheConfig = new SPCacheConfig();
internal static CacheParameter[] CacheParameters = new CacheParameter[]
{
// DataCache
new CacheParameter(ServiceName,
DataCache,
0xfffff,
PriorityType.High,
new TimeSpan(0,60,0))
};
internal const string ServiceName = "CacheAppStressTest";
internal const string DataCache = "CacheAppStressTest.Data";
}
Create Web part and add following code
public class CustomCache : WebPart
{
Label lblMessage;
Label lblCacheData;
public CustomCache()
{
this.lblMessage = new Label();
this.lblMessage.ID = "lblMessage";
this.lblCacheData = new Label();
this.lblCacheData.ID = "lblCacheData";
}
protected override void CreateChildControls()
{
try
{
byte priority = 0;
MyCache.CacheParameters[0].lifetime = new TimeSpan(0, 1, 0); //Set life time of parameter
MyCache.CacheParameters[0].priority = (PriorityType)priority; // Set priority of paramater
MyCache.cacheConfig.AddCaches(MyCache.CacheParameters);
SPCache.Cache.Put(MyCache.DataCache, new SPCachedObject("Test", new TestClass("some data"), new TimeSpan(1, 0, 0))); // Add cache object into SPCache Collection
TestClass newClass = (TestClass)SPCache.Cache.Get(MyCache.DataCache, "Test");
if (newClass != null)
{
this.lblCacheData.Text = "<br /><b>" + newClass.someData + "</b>";
}
else
{
this.lblCacheData.Text = "<br /><b> Null data</b>";
}
// Remove cache object
SPCache.Cache.Delete(MyCache.DataCache, "Test");
//Display Cache data
this.lblMessage.Text = string.Format("CacheHits={0}\r\nReads={1}\r\nCount={2}\r\nSizeBytes={3}\r\nHitRatio={4}",
SPCache.Cache.TotalCacheHits, SPCache.Cache.TotalReadAttempts, SPCache.Cache.Count, SPCache.Cache.UsedBytes, SPCache.Cache.HitRatio);
MyCache.cacheConfig.RemoveCaches(MyCache.CacheParameters);
}
catch (Exception ex)
{
this.lblMessage.Text = ex.Message;
}
this.Controls.Add(this.lblMessage);
this.Controls.Add(this.lblCacheData);
}
}
// Object for Cacheing
public class TestClass
{
public string someData { get; set; }
public TestClass(string myData)
{
this.someData = myData;
}
}
// SPCache Object Settings Cache Parameters
public class MyCache
{
public static SPCacheConfig cacheConfig = new SPCacheConfig();
internal static CacheParameter[] CacheParameters = new CacheParameter[]
{
// DataCache
new CacheParameter(ServiceName,
DataCache,
0xfffff,
PriorityType.High,
new TimeSpan(0,60,0))
};
internal const string ServiceName = "CacheAppStressTest";
internal const string DataCache = "CacheAppStressTest.Data";
}
Create Web part and add following code
public class CustomCache : WebPart
{
Label lblMessage;
Label lblCacheData;
public CustomCache()
{
this.lblMessage = new Label();
this.lblMessage.ID = "lblMessage";
this.lblCacheData = new Label();
this.lblCacheData.ID = "lblCacheData";
}
protected override void CreateChildControls()
{
try
{
byte priority = 0;
MyCache.CacheParameters[0].lifetime = new TimeSpan(0, 1, 0); //Set life time of parameter
MyCache.CacheParameters[0].priority = (PriorityType)priority; // Set priority of paramater
MyCache.cacheConfig.AddCaches(MyCache.CacheParameters);
SPCache.Cache.Put(MyCache.DataCache, new SPCachedObject("Test", new TestClass("some data"), new TimeSpan(1, 0, 0))); // Add cache object into SPCache Collection
TestClass newClass = (TestClass)SPCache.Cache.Get(MyCache.DataCache, "Test");
if (newClass != null)
{
this.lblCacheData.Text = "<br /><b>" + newClass.someData + "</b>";
}
else
{
this.lblCacheData.Text = "<br /><b> Null data</b>";
}
// Remove cache object
SPCache.Cache.Delete(MyCache.DataCache, "Test");
//Display Cache data
this.lblMessage.Text = string.Format("CacheHits={0}\r\nReads={1}\r\nCount={2}\r\nSizeBytes={3}\r\nHitRatio={4}",
SPCache.Cache.TotalCacheHits, SPCache.Cache.TotalReadAttempts, SPCache.Cache.Count, SPCache.Cache.UsedBytes, SPCache.Cache.HitRatio);
MyCache.cacheConfig.RemoveCaches(MyCache.CacheParameters);
}
catch (Exception ex)
{
this.lblMessage.Text = ex.Message;
}
this.Controls.Add(this.lblMessage);
this.Controls.Add(this.lblCacheData);
}
}
No comments:
Post a Comment