Monday, March 10, 2008

Enterprise Library 3.1 - Caching Application Block (Part 2)

The previous article in this series explained about the features and architecture of the Caching application Block. In this series we will try to implement the Application Block.

Using the Caching Application Block

1. Add Enterprise Library 3.1 references to the project.
• Microsoft.Practices.EnterpriseLibrary.Caching.dll
• Microsoft.Practices.EnterpriseLibrary.Common.dll
• Microsoft.Practices.ObjectBuilder.dll
2. Right click the App.config file and select Edit Enterprise Library Configuration
3. Right click the Project in the Enterprise Library Configuration console and select New -->Caching Application Block




4. Create a backing store by right-clicking the Cache Manager node and select



New -->Isolated Storage.
5. Select the PartitionName property and name it MyCachePartition



6. Save and close the Configuration Console
7. Implement the ICacheItemRefreshAction





The ICacheItemRefreshAction interface refreshes the CacheManager instance when it is expired, preventing the occurrence of a Null Reference exception.



[Serializable]
public class CacheRefreshPolicy : ICacheItemRefreshAction
{
#region ICacheItemRefreshAction Members

public void Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason)
{
CacheManager _myCacheManager = CacheFactory.GetCacheManager();
_myCacheManager.Add(removedKey, "Item removed on " + DateTime.Today.ToLongDateString());
}

#endregion
}

8. Adding into the cache



CacheManager _myCacheManager = CacheFactory.GetCacheManager();

_myCacheManager.Add("MyCacheKey", CacheContentTextBox.Text,
CacheItemPriority.Normal, new CacheRefreshPolicy(),
new SlidingTime(TimeSpan.FromSeconds(60)));



9. Retrieving data from the cache



CacheManager _myCacheManager = CacheFactory.GetCacheManager();
if (_myCacheManager != null)
{
if (_myCacheManager.Contains("MyCacheKey"))
{
CacheContentLabel.Text = _myCacheManager.GetData("MyCacheKey") as String;
}
else
{
CacheContentLabel.Text = "Cache is missing!!!";
}
}
else
{
CacheContentLabel.Text = "Cache Manager is not present!!!";
}



10. Removing data from the cache



CacheManager _ myCacheManager = CacheFactory.GetCacheManager();
_ myCacheManager.Remove("MyCacheKey");

2 comments:

Anonymous said...

In your Refresh method, if you are refreshing the cached item, wouldn't you also add it back to the cache with a new instance of the ICacheItemRefreshAction class?

[public void Refresh]
_myCacheManager.Add(removedKey, CacheContentTextBox.Text,
CacheItemPriority.Normal, new CacheRefreshPolicy(),
new SlidingTime(TimeSpan.FromSeconds(60)));

(I plan to use this in conjunction with SQLDependency, so I want to refresh my cache each time.)

Unknown said...

Hi Prajeesh,
Thanks for sharing your knowledge. Enterprise Library is a wonderful tool for developers as it saves a lot of valuable time. I wanted to use CAB in my application but I have a large distributed configuration and think that CAB might not scale nicely. Recently I came across this article about CAB. What do you think?