C#: Microsoft Enterprise Library: Caching
By: Zack Turnbow
Introduction
Following in line in this short series on Microsoft’s Enterprise Library, this article will cover some of the common uses of the Caching Application Block (CAB). Nearly every application, web or windows application pulls data that is needed by the end user. There is data that is used more often than other data. To keep the overhead down on accessing the database every time that common data is needed, a developer would either create their own caching mechanism or if it is a web application that data would often end up session, which could cause performance issues. So, the CAB was created so a developer wouldn’t have to use the previous methods.
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Implementation
The CAB supplies the simple functionality of actually constructing and configuring a cache, the ability to add an object to the cache, remove an object from the cache, retrieve an object from the cache, and expire a cache. So, to get started, the project from the previous article will be used to as a starting point. Open up the project in Visual Studio add a reference to the Enterprise Library Caching dll file.
Don’t forget to add a using statement for the CAB:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
Next, open the web.config for the project in the Enterprise Library Configuration application.
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Right click the file in the left pane and select New – Caching Application block. Then right click the Cache Managers and select New – Cache Manager and give it a name of Product Cache.
Now go back to the Visual Studio project and open the Defualt.aspx page and add two separate labels. These labels will be used to display the contents of the cache when the application is tested. So now some code needs to be written to demonstrate caching. Open the code behind to the Default.aspx page and add the following class definitions with the following code:
{
string productID;
string productName;
string productNumber;
string lastUpdated;
public Product()
{
}
public Product(string id, string name, string number, string modDate)
{
productID = id;
productName = name;
productNumber = number;
lastUpdated = modDate;
}
public string GetProductDetails()
{
return (" " + productID + " " + productName + " " + productNumber + " " +
lastUpdated);
}
public string GetProductID()
{
return productID;
}
}
class RefreshCache : ICacheItemRefreshAction
{
public void Refresh(string keyToRemove, object expireTime, CacheItemRemovedReason
removeReason)
{
CacheFactory.GetCacheManager().Add(keyToRemove, expireTime);
string productID = "1";
string productName = "Widget";
string productNumber = "123";
string lastUpdated = DateTime.Now.ToLongTimeString();
Product product = new Product(productID, productName, productNumber,
lastUpdated);
CacheFactory.GetCacheManager("ProductCache").Add(product.GetProductID(), product,
CacheItemPriority.Normal, new RefreshCache(), new
SlidingTime(TimeSpan.FromSeconds(5)));
}
}
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Finally add code under the Page_Load event to actually use the cache.
{
CacheManager productCache = CacheFactory.GetCacheManager("ProductCache");
string productID = "1";
string productName = "Widget";
string productNumber = "123";
string lastUpdated = DateTime.Now.ToLongTimeString();
Product productNew = (Product)productCache.GetData("1");
if (productNew == null)
{
this.Label2.Text = "Could not retrieve";
Product product = new Product(productID, productName, productNumber,lastUpdated);
productCache.Add(product.GetProductID(), product, CacheItemPriority.Normal,
new RefreshCache(), new SlidingTime(TimeSpan.FromSeconds(10)));
}
else
{
this.Label1.Text = productNew.GetProductDetails();
}
}
catch (System.Exception ex)
{
this.Label2.Text = ex.StackTrace;
}
In the above code, the CacheFactory.GetCacheManager method gets the configuration for the cache from the web.config. The GetData method is what retrieves the data from the cache where as the Add method handles adding the object into the cache. The cache refresh is set to 10 seconds so when the cache actually refreshes it will have a different last updated date while the other information remains the same.
Now run the application to see the results. Remember to wait 10 seconds to refresh the page so the change in the cache can be seen.
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
Using this example, the Enterprise Library Caching Application Block gives another option when there is a need to use data on a regular basis instead of accessing the database each time it is needed.
What have we learned?
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
How to add a reference to the Enterprise Library Caching Application Block.
How to configure the CAB in the Enterprise Library Configuration application.
How to implement the CAB in code by adding and refreshing objects in the cache.
Attachments







Leave a Comment
#1 sankar
Posted By: sankar | 11.01.2008 at 6:39 AM
This is the article which helped me to work on CAB
#2 Dexter
Posted By: Dexter | 4.24.2009 at 8:45 AM
Thanks for an informative article! Indeed CAB is worth incorporating into your application if you are looking to enhance performance. However, I must point out that due to its in-proc and standalone nature it does not scale well; especially in the case of web farms. You can follow the link provided for more information.
#3 Gifts for men
Posted By: Gifts for men | 2.19.2010 at 3:48 AM
This is pretty useful article for many IT professionals in more than one ways. Thanks for sharing this with all the users!
#4 puzzle games
Posted By: puzzle games | 3.07.2010 at 3:38 AM
Could you please provide more information on this subject?? BTW your site is cool. Sincerely!
#5 642-974 exam
Posted By: 642-974 exam | 3.19.2010 at 5:34 AM
For all I know this could be a really old entry, since there are no date marker on the article. In that case I apologize.
#6 Technology
Posted By: Technology | 3.25.2010 at 12:13 PM
thank you Admin
#7 scegli il migliore casinò online
Posted By: scegli il migliore casinò online | 3.26.2010 at 3:02 AM
Interesting post and I really like your take on the issue. I now have a clear idea on what this matter is all about. Thank you so much.
#8 microempresasypymes
Posted By: microempresasypymes | 4.02.2010 at 4:56 AM
Is Enterprise Library Caching block a good mechanism for building a WCF service
#9 microempresasypymes
Posted By: microempresasypymes | 4.02.2010 at 4:57 AM
Is Enterprise Library Caching block a good mechanism for building a WCF service
#10 video conference
Posted By: video conference | 4.30.2010 at 5:57 AM
This is a good piece of writing mate for this particular topic. I was wondering if I could use this write-up on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away.
#11 laptop battery manufacturer
Posted By: laptop battery manufacturer | 5.19.2010 at 3:43 AM
thanks for your sharing.
#12 wholesale shoes
Posted By: wholesale shoes | 5.24.2010 at 3:28 AM
Spring summer pocket perfect supporting role (figure) except heart shape brooch, earring, necklace,
#13 kaka
Posted By: kaka | 5.27.2010 at 4:02 PM
Hey thanks for the article,..I found a similar one on http://w3mentor.com
#14 algae killer
Posted By: algae killer | 5.30.2010 at 7:36 PM
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.
#15 bad credit car loan
Posted By: bad credit car loan | 6.06.2010 at 5:27 AM
How do I transfer my microsoft zune library to another computer?
#16 replica coach purses
Posted By: replica coach purses | 6.11.2010 at 3:42 AM
Good good…this post deserves nothing Frown …hahaha just joking Tong …nice post
#17 moncler down jackets
Posted By: moncler down jackets | 6.13.2010 at 2:05 AM
Here elaborates the matter not only extensively but also detailly .I support the write's unique point.It is useful and benefit to your daily life.You can go those <a href=" conditions-encountered.com " >wslmart.net </a> sits to know more relate things.They are strongly recommended by friends.Personally
#18 new music database
Posted By: new music database | 6.14.2010 at 3:39 PM
First of all thanks a lot for the useful and informative article. I have to do a little research in my university about the Microsoft’s Enterprise Library so I am so glad that I have found this your post. The information which you have shared for us is really good explained and I will definitely will use it in my work. Thanks a lot one more time for the useful and detailed post and I will be waiting for more such great articles like this one from you in the nearest future.
#19 Indian Saree
Posted By: Indian Saree | 6.17.2010 at 1:16 AM
But, now when I'm testing out a ASP.NET MVC application with this setting it throw an error in class SessionStateTempDataProvider inside the mvc framework, it asked me to turn on session state, I did and it worked. Looking at the source it uses session
#20 Agile Informatics
Posted By: Agile Informatics | 6.17.2010 at 6:42 AM
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.
#21 kuber infotek
Posted By: kuber infotek | 6.18.2010 at 12:47 AM
Microsoft Enterprise Library is a collection of reusable application blocks designed to assist software developers with common enterprise development challenges. This release includes: Caching Block, Cryptography Block, Data Access Block, Exception Handling Block, Logging Block, Policy Injection Block, Security Block, Validation Block, and Unity.
#22 Women Health Care Center
Posted By: Women Health Care Center | 6.18.2010 at 2:01 AM
I am going to discuss the implementation of the Exception Application block of Enterprise Library 1.0., as I have gone through various sites but found there is no tutorial or any article which has highlights on this topic. Sometimes it may happen we have lots of modules having many layers in it
#23 Women Health Care Center
Posted By: Women Health Care Center | 6.18.2010 at 2:01 AM
I am going to discuss the implementation of the Exception Application block of Enterprise Library 1.0., as I have gone through various sites but found there is no tutorial or any article which has highlights on this topic. Sometimes it may happen we have lots of modules having many layers in it
#24 computer Hardware part
Posted By: computer Hardware part | 6.18.2010 at 4:54 AM
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.
#25 computer Hardware part
Posted By: computer Hardware part | 6.18.2010 at 4:55 AM
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.
#26 michael jordan shoes
Posted By: michael jordan shoes | 6.18.2010 at 8:51 PM
good post!!thank you
#27 coach outlet
Posted By: coach outlet | 7.06.2010 at 9:58 PM
thank you!I learned a lot from this article.
#28 gucci bag
Posted By: gucci bag | 7.18.2010 at 11:16 PM
www.programminghelp.com/.../creating-custom
#29 fendi bags
Posted By: fendi bags | 7.18.2010 at 11:16 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#30 coach handbags on sale
Posted By: coach handbags on sale | 7.18.2010 at 11:16 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#31 coach bag
Posted By: coach bag | 7.18.2010 at 11:17 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#32 coach purse
Posted By: coach purse | 7.18.2010 at 11:17 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#33 hermes birkin
Posted By: hermes birkin | 7.18.2010 at 11:17 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#34 chanel purses
Posted By: chanel purses | 7.18.2010 at 11:17 PM
I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
#35 tiffany co
Posted By: tiffany co | 7.21.2010 at 2:49 AM
For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#36 tiffany jewelry
Posted By: tiffany jewelry | 7.21.2010 at 2:49 AM
For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#37 tiffany jewellery
Posted By: tiffany jewellery | 7.21.2010 at 2:49 AM
For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/
#38 congcong
Posted By: congcong | 7.21.2010 at 4:35 AM
[url=http://www.imitatewatch.com/GoodsDetail/Replica-Hublot-Big-Bang-Mens-Watch-301-SB-131-RX-Watches-2213.html]Replica Hublot Big Bang Watches[/url]
[url=http://www.imitatewatch.com/GoodsSeries/Replica-Luminor-Watches-311.html]Replica Panerai Luminor Watches[/url]
[url=http://www.imitatewatch.com/GoodsSeries/Replica-Luminor-Watches-311.html]Patek Philippe Celestial Watches[/url]
[url=http://www.imitatewatch.com/GoodsSeries/Replica-Daytona-Watches-363.html]Replica Rolex Daytona Watches[/url]
[url=http://www.imitatewatch.com/GoodsSeries/Replica-Pilot-Watches-248.html]Replica IWC Pilot Watches[/url]
[url=http://www.imitatewatch.com/GoodsSeries/Replica-Portuguese-Watches-250.html]Replica IWC Portuguese Watches[/url]
[url=http://www.imitatewatch.com/GoodsSeries/Replica-Royal_Oak_Offshore-Watches-29.html]Audemars Piguet Royal Oak Offshore Replica Watches[/url]
[url=http://www.imitatewatch.com/GoodsSeries/Replica-Happy_Sport-Watches-179.html]Chopard Happy Sport Replica Watches[/url]
#39 tiffany ring
Posted By: tiffany ring | 7.26.2010 at 6:57 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#40 tiffany bracelet
Posted By: tiffany bracelet | 7.26.2010 at 6:57 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#41 tiffany necklace
Posted By: tiffany necklace | 7.26.2010 at 6:58 AM
Tiffany On Sale Offer a wide range of fashionable Tiffany & Co silver jewelry, including Necklaces, Pendants, Bracelets, Earrings, Rings and Accessories with fantastic discount.more http://www.tiffanyonsale.com/
#42 Gucci Shoes
Posted By: Gucci Shoes | 7.28.2010 at 10:30 PM
It also offers the ability to create your own custom trace listeners in addition to configuring log options.
#43 gucci bags
Posted By: gucci bags | 7.29.2010 at 10:25 PM
thank you