Cache Up to the Caching Application Block in Enterprise Library 3.0 : Page 4
Use standardized caching to simplify your development and reduce hard-to-find bugs.
by Thiru Thangarathinam
Aug 15, 2007
Page 4 of 4
Implementing Custom Refreshing Behavior
You have full control over how or whether you want to refresh of cached items when they're removed from the cache. For example, you might want to invoke a custom method containing logic to replace a removed item by inserting a new item onto the cache. To accomplish this, follow these steps:
Create a custom class that implements the ICacheItemRefreshAction interface:
public class EmployeeCacheRefreshAction : ICacheItemRefreshAction
Implement the Refresh method, writing code to replenish the cache with the new item:
public void Refresh(string key, object expiredValue,
// When the object has been removed from cache, add the new item
Supply the custom class as an argument to the CacheManager.Add() method when adding the item to the cache:
_manager.Add(emp.EmployeeID, emp, CacheItemPriority.High,
In the preceding code, note that the fourth argument to the Add() method is the custom class you created in step 1. That's all there is to implementing a custom refresh action for a cached item.
Using SQL Server as the Backing Store
As mentioned earlier, in addition to the default null backing store (in-memory backing store), you also have the option of using SQL Server as the caching store. As you would expect, this approach is very scalable, but incurs additional overhead to store and retrieve cached items from the SQL Server database. To be able to use the SQL Server as the backing store, you need to go through the steps below:
Execute the SQL script file CreateCachingDatabase.sql that you'll find in the <DriveName>\EntLib3Src\App Blocks\Src\Caching\Database\Scripts folder. The script creates a database named Caching containing a table named CacheData. The CacheData table stores all the cached data. In addition, the Caching database contains several stored procedures that the cache provider uses to perform actions against the database.
Reference the Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll assembly from within your project, in addition to the Microsoft.Practices.EnterpriseLibrary.Caching.dll and Microsoft.Practices.EnterpriseLibrary.Common.dll assemblies.
Modify your app.config file to instruct the Caching Application Block to use the SQL Server as the backing store. As you can see, this configuration-driven approach provides a flexible caching implementation wherein you can seamlessly switch the backing store without having to touch a single line of code. Listing 1 shows the configuration file entries required to use a local SQL Server as the caching backing store.
After modifying app.config with the code in Listing 1 and executing the solution, you should get exactly the same results as running it with the in-memory store.
In addition to the in-memory and SQL Server caching stores, you can also implement custom backing stores such as an XML file or an Oracle database by creating a custom caching class that derives from the Microsoft.Practices.EnterpriseLibrary.Caching.IBackingStore interface. This custom class must implement the methods responsible for interacting with the underlying persistence mechanisms to store and retrieve cached items.
This article discussed the use of the Caching Application Block in .NET applications for creating high performance and fault-tolerant .NET applications. You have seen examples of how to use the Caching Application Block for caching both simple scalar data (such as strings or integers), and complex data (objects). In addition, you saw the flexibility of the Caching Application Block by discussing the steps involved in switching the backing store from the in-memory backing store to a SQL Server-based backing store.
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.