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 Up to the Caching Application Block in Enterprise Library 3.0 : Page 3

Use standardized caching to simplify your development and reduce hard-to-find bugs.


advertisement
Persisting Complex Data
You've seen how to cache, retrieve, and delete simple scalar values, but caching complex data is a little different. To illustrate, create a custom class named Employee that acts as a placeholder for storing the employee details:

using System; namespace CachingBlockExample { [Serializable] public class Employee { private string _employeeID; public string EmployeeID { get { return _employeeID; } set { _employeeID = value; } } private string _name; public string Name { get { return _name; } set { _name = value; } } private int _age; public int Age { get { return _age; } set { _age = value; } } } }

Now that you have created the Employee class, add three buttons to the form that store, retrieve, and remove Employee objects from the cache (see Figure 3 for an example). Here's the Click event code for each button:

private void btnAddObjectToCache_Click(object sender, EventArgs e) { lstResults.Items.Clear(); Employee emp = new Employee(); emp.EmployeeID = "1234"; emp.Name = "David"; emp.Age = 20; _manager.Add(emp.EmployeeID, emp, CacheItemPriority.High, new EmployeeCacheRefreshAction(), new AbsoluteTime(DateTime.Now.AddMinutes(60))); lstResults.Items.Add("Object added"); } private void btnRetrieveObject_Click(object sender, EventArgs e) { lstResults.Items.Clear(); object emp = _manager.GetData("1234"); if (emp != null) { Employee empTemp = (Employee)emp; lstResults.Items.Add(empTemp.EmployeeID); lstResults.Items.Add(empTemp.Name); lstResults.Items.Add(empTemp.Age.ToString()); } else lstResults.Items.Add("No item found"); } private void btnRemoveObject_Click(object sender, EventArgs e) { lstResults.Items.Clear(); _manager.Remove("1234"); lstResults.Items.Add ("Object identified by 1234 removed from the cache"); }

Note the additional arguments passed to the CacheManager.Add() method:

 
Figure 3. Retrieving Complex Data: The figure shows output produced by first storing and then retrieving an Employee object from the cache.


_manager.Add(emp.EmployeeID, emp, CacheItemPriority.High, new EmployeeCacheRefreshAction(), new AbsoluteTime( DateTime.Now.AddMinutes(60)));

In addition to the cache key (the EmployeeID) and the data (the emp variable), the overloaded CacheManager.Add() method also accepts a CacheItemPriority enumeration value, a custom object used to refresh the cached item when the item is removed from the cache, and the absolute time at which the cached item needs to expire.

With the buttons and code in place, if you now run the application, you'll see output somewhat similar to Figure 3:

As the name suggests, the CacheItemPriority enumeration allows you to specify priority levels for the cached items. The CacheItemPriority enumeration exposes the values shown in Table 1:

Table 1. CacheItemPriority Enumeration Values: These values are available in the CacheItemPriority enumeration.
ValueDescription
HighHigh priority for scavenging, meaning that the items with this priority level are the least likely to be deleted from the cache as the server frees the system memory.
LowLow priority for scavenging, meaning that the items with this priority are the most likely to be deleted from the cache as the server frees the system memory.
NoneNo priority associated with scavenging.
NormalNormal priority for scavenging, meaning that the items with this priority are likely to be deleted from the cache as the server frees the system memory only after those items with the "Low" priority.
NotRemovableNon-removable priority for scavenging, meaning that these items are not automatically deleted from the cache as the server frees the system memory. However, even items with this priority are removed according to the item's absolute or sliding expiration time.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap