Introducing Enterprise
Library Caching Block
Introduction:
Microsoft introduced Enterprise
Library in January 2005. Many companies have migrated their code from the
independent Application Blocks to the Enterprise Library Blocks. In this article
I will introduce the Caching application block which is part of one of the
blocks shipped with Enterprise Library.
Adding the reference:
First thing you need to do is to
add a reference to the dynamic link library. In order to use Caching Application
Block you should make a reference to Microsoft.Practices.Enterprise.Caching.
Than you have to add the namespace
to your C# code file.
using Microsoft.Practices.EnterpriseLibrary.Caching;
After this we are ready to use the Caching
Application Block of the Enterprise Library.
Using Enterprise Library Caching
Application Block:
Let's see how we can return
dataset from the cache object to the calling block. We will see the code in
small pieces so that you can easily understand it.
First of all we have to make the
CacheManager object which is responsible for adding cache features to our
application.
| private
CacheManager myCache; |
Next, we make a method "LoadData()" which will
return the dataset. We have to make it efficient that's why we will be checking
that if the data is already present in the cache. If the data is present than we
don't need to disturb the database access layer. First let's get the DataSet
from the Cache object.
| DataSet ds = (DataSet)
myCache.GetData("MyData"); |
The above line is very similar to the line
below:
| DataSet ds = (DataSet)
myCache["MyData"]; |
Next we have to check that if the Cache object
is empty or not. If its empty than we need to go to the database and fetch a
fresh copy of the DataSet.
| if(ds!=null)
{
return ds;
} |
Now let's see that what happens if the Cache
object is null.
| 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)));
return (DataSet) myCache.GetData("MyData");
} |
First we create a Database object which gets
its configuration settings from the dataConfiguration.config file. Later we are
executing a simple query which selects all rows from the table Articles.
|
myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new
Microsoft.Practices.EnterpriseLibrary.Caching
.Expirations.AbsoluteTime(TimeSpan.FromMinutes(5))); |
Next we are adding the dataset to the Cache
object using the CacheManager Add method. There are severl parameters that we
need to know first is the key which we have set to "MyData". Next is the DataSet
itself, then comes priority, refresh and finally the expiration policy. You can
set expiration policy in different ways. I have used Absolute Expiration which
means that the cache will expire after 5 minutes from the time that it was
created. If you want to check this you can simply place a Response.Write line to
display the time at which the cache is being recreated. And you will notice that
the cache recreates itself after 5 minutes.
There are some other different types of
expirations that you can use. In the code below I am using SlidingExpiration
policy.
|
myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new
Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
.SlidingTime(TimeSpan.FromMinutes(2))); |
We have sliding time of 2 minutes this means
that if the cache is not being accessed for 2 minutes than it will expire. On
the other hand if the cache is accessed within 2 minutes than the time of the
cache will be reset to 2 minutes. Meaning that the cache object will again get
its sliding time of 2 minutes.
You can also set the expiration policy
dependent on the file. This means that whenever a file is being changed the
cache object will automatically gets the latest copy of that file. In the code
below I am setting the file Dependency to the MyXmlFile.xml. This means that
anytime the xml file changes than the cache data will be updated.
|
myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new
Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
.FileDependency(Server.MapPath("MyXmlFile.xml")));
|
You can also set the expiration policy to
expire the cache at exact time and day. I don't know if anyone is going to use
this option but here is the syntax for using it. Please check the
ExtendedFormatTime format which can be found in ExtendedFormatTime.cs file
provided by the Caching Application Block.
|
myCache.Add("MyData",ds,CacheItemPriority.Normal,null,new
Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
.ExtendedFormatTime("*
* * * 0 0 ")); |
Removing Cache Items:
You can easily remove the items from the cache
using the Remove Method. The code below will remove once cache object from the
cache collection. This removal is based on key which in this case is "MyData".
| myCache.Remove("MyData");
|
If you want to remove all the items in the
cache collection than you can simply use the flush method.
Checking if the key is already present in
the caching collection:
If you want to know that if a particular key is
present in the cache than you can easily use the contains method of the
CacheManager object.
| myCache.Contains("MyData");
|
I hope you enjoyed the article, Happy
programming !
Attachments:
Project Files: EnterpriseLibrarycaching.zip