Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Using the CacheFactory Object
Whenever you work with the Caching Block, the first class that you will have to deal with is the CacheFactory class. The CacheFactory class exposes a static method named GetCacheManager() that returns a reference to a specific CacheManager instance. The parameterless overload of GetCacheManager() returns a reference to the default cache manager. To get specific instances of the cache manager, you use the overload that accepts the name of the cache manager. The following code obtains a reference to the default cache manager and stores it in a local variable named employeesCache.

CacheManager employeesCache = CacheFactory.GetCacheManager();

After obtaining a reference to the CacheManager instance, you can then easily add, remove, or retrieve items from the caching store.

Here's an example. Suppose you want to add a simple object to the cache. For the purposes of this article, create a Windows Forms application named CachingBlockExample and add a class named Employee declared as follows:

[Serializable] public class Employee { int _id; string _name; string _address; public Employee(int id, string name, string address) { _id = id; _name = name; _address = address; } public int EmployeeID { get{return _id; } set {_id = value;} } public string Name { get{return _name;} set{_name = value;} } public string Address { get{return _address;} set{_address = value;} } }

As you can see, the Employee class simply acts as a placeholder for three public properties: EmployeeID, Name, and Address. The next step is to persist Employee class instances using the Caching Block. To demonstrate this, add a command button named btnAddItem to the default form (named Form1) and modify its Click event as shown below:

private void btnAddItem_Click( object sender, EventArgs e) { 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); MessageBox.Show("Successfully added"); }

The CacheManager.Add() method lets you add an item associated with a key to the cache. Be careful when using it though—the Add() method removes any existing item with the same key before adding the new item. In other words, the Add() method not only adds, it also replaces items, so if you're not sure whether a key already exists, check first.

The Add() method takes four parameters: a key, the object to be cached, a CacheItemPriority enumeration, and an object that implements the ICacheItemRefreshAction interface and is responsible for refreshing the cached item. The CacheItemPriority enumeration can be any of the following values: High, Low, None, Normal, and NotRemovable. You can't change the CacheItemPriority once it's been set unless you first manually remove and then re-add the item with the changed CacheItemPriority. Note that if you call Add() and an item with that key and a CacheItemPriority of NotRemovable already exists, you'll get an exception. You'll see an example of how to leverage the ICacheItemRefreshAction class parameter later in this article.

Retrieve the Cached Employee Object
After adding an object to the cache, you retrieve it from the cache using the CacheManager.GetData() method.

private void btnRetrieve_Click( object sender, EventArgs e) { CacheManager employeesCache = CacheFactory.GetCacheManager(); //Retrieve the item from the cache Employee emp = (Employee)employeesCache.GetData("1"); MessageBox.Show(emp.Name); }

Note that the GetData() method takes in the key of the cached item and returns an object that is of type Object, which you must typecast back into an Employee object.

Remove an Employee Object from the Cache
To remove an item from the cache, use the CacheManager.Remove() method and pass in the key of the cached item. The Remove() method does nothing if there is no item in the cache with that key.

private void btnRemove_Click( object sender, EventArgs e) { CacheManager employeesCache = CacheFactory.GetCacheManager(); //Remove the item from the cache employeesCache.Remove("1"); MessageBox.Show("Cached " + "item successfully removed"); }

If you supply a null value or an empty string as the key parameter to Remove(), the method raises an ArgumentNullException or ArgumentException respectively.

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