Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Cache In On the Enterprise Library Caching Block for .NET 2.0 : Page 4

Nearly every application needs to cache data. While you're probably familiar wth the caching functionality built into ASP.NET, the Enterprise Library Caching Block provides in-memory, file-based, or database caching storage for all your other .NET applications.


advertisement
Automatically Refreshing Cached Items
Sometimes you might want to refresh the cache immediately with the latest data whenever the cached item is removed. Additionally, you might want to refresh the cache automatically with the latest data when a cached item has expired or has been explicitly removed from the cache. To accomplish this, perform the steps listed below:

  1. Create a custom class that implements the ICacheItemRefreshAction interface
  2. Implement the Refresh() method with the appropriate code that re-populates the cache with the latest values
  3. Supply the derived class as an argument to the CacheManager.Add() method. This enables the caching framework to automatically invoke the Refresh() method.
Here's an example custom class that implements the ICacheItemRefreshAction interface.

using System; using Microsoft.Practices. EnterpriseLibrary.Caching; [Serializable] public class EmployeeCacheRefreshAction : ICacheItemRefreshAction { public void Refresh(string key, object expiredValue, CacheItemRemovedReason removalReason) { CacheManager employeesCache = CacheFactory.GetCacheManager(); int id = 1; string name = "Thiru"; string address ="2644 E Remington"; Employee emp = new Employee(id, name, address); employeesCache.Add( emp.EmployeeID.ToString(), emp, CacheItemPriority.Normal, null); } }

In the preceding code, the Refresh() method obtains a reference to the CacheManager and then refreshes the cache with the latest employee data.

After creating the EmployeeCacheRefreshAction class, you can pass an instance of the class to the CacheManager.Add() method as shown below.

employeesCache.Add( emp.EmployeeID.ToString(), emp, CacheItemPriority.Normal, new EmployeeCacheRefreshAction());

After doing that, any removal of the Employee object from the cache triggers the EmployeeCacheRefreshAction.Refresh() method.

Using a Database Cache Store
Using the in-memory store you've seen so far as the caching store is efficient, but it isn't permanent. To switch from an in-memory cache to a database cache, follow these steps:

  • Execute the SQL script in the file CreateCachingDatabase.sql, which you can find at <DriveName>:\Program Files\Microsoft Enterprise Library January 2006\src\Caching\Database\Scripts
  • Modify the app.config file to use the appropriate caching providers.
  • Add a reference to the Microsoft.Practices.EnterpriseLibrary.Caching.Data.dll assembly to your solution
Listing 1 shows how the modified app.config file might look:

Notice that the app.config file in Listing 1 also adds the Data Access Block settings. This is required, because the Caching Block leverages the Data Access Block internally to persist and retrieve data from the database. Conveniently, after modifying the app.config file with the settings shown in Listing 1 in place, the code to add, remove, and retrieve items from the database cache doesn't need to change at all; in other words, you can change the backing cache store without modifying your code.

As you have seen from this article, the EntLib Caching Block obviates the need to write repetitive caching plumbing code by providing a set of highly reusable classes for adding, retrieving, and removing items from the cache. By using the Caching Block classes, you can reduce errors, bugs, and typos in your application and focus more attention on the core business logic of the application to achieve increased productivity.



Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date