Monday, May 16, 2005

Enterprise Library Caching Application Block

Caching application block of Enterprise Library is pretty cool. Using the common patterns that Microsoft have already given in quick start tutorial I was able to make a simple application using caching. You can also have multiple caching expiration policy.

Some of them are given below:

private DataSet LoadData()
{
DataSet ds = (DataSet) myCache.GetData("MyData");


if(ds!=null)
{
return ds;
}
else
{
Database db = DatabaseFactory.CreateDatabase();
DBCommandWrapper selectCommandWrapper = db.GetSqlStringCommandWrapper("SELECT * FROM Articles");

ds = db.ExecuteDataSet(selectCommandWrapper);
myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new Microsoft.Practices.EnterpriseLibrary.Caching
.Expirations.AbsoluteTime(TimeSpan.FromMinutes(5)));

myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
.SlidingTime(TimeSpan.FromMinutes(2)));

myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
.FileDependency(Server.MapPath("MyXmlFile.xml")));


myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
.ExtendedFormatTime("* * * * 0 0 "));

return (DataSet) myCache.GetData("MyData");
}

1 comment:

Anonymous said...

I keep getting this error related to MyData

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."