Retrieving Items from the Cache
In the sample application, the handler for the button that retrieves items from the cache is somewhat more complex, because it must discover the type of the cached item and convert it for display. The CacheManager's
GetData method returns all cached objects as an Object reference type.
After creating the appropriate CacheManager instance, the code first checks whether the item with the key selected in the drop-down list next to the Retrieve button exists in the cache. This check saves a call to the backing store methods if the item is already in the synchronized in-memory cache. When it is, the code calls the
GetData method, and then uses a multiple
if statement to check the type and display it in the appropriate way.
protected void btn_Retrieve_Click(object sender, EventArgs e)
{
try
{
// create the selected cache manager instance
CacheManager cm = CacheFactory.GetCacheManager(
optCacheManager.SelectedValue);
// retrieve the item with the specified key
String itemKey = lstRetrieveItem.SelectedValue;
// see if the item is in the cache
if (cm.Contains(itemKey))
{
// check the type and display as appropriate
Object item = cm.GetData(itemKey);
if (item is DataSet)
{
// bind to GridView control
GridView1.DataSource = item as DataSet;
GridView1.DataBind();
}
else if (item is Object[])
{
// iterate array displaying values
StringBuilder builder = new StringBuilder(
"Retrieved item values are: ");
foreach (Object arrayItem in (Object[])item)
{
builder.Append(arrayItem.ToString());
builder.Append(", ");
}
lblResults.Text = builder.ToString();
}
else
{
// display the value
lblResults.Text = String.Format(
"Retrieved item is '{0}'",
item.ToString());
}
}
else
{
lblResults.Text = String.Format(
"Cache does not contain an item"
+ " with key '{0}'.", itemKey);
}
}
catch (Exception ex)
{
lblResults.Text = "<b>ERROR</b>: " + ex.Message;
}
}
Figure 6 shows the result of retrieving an Object Array from the cache.
 | |
| Figure 6. Retrieving a Cached Object Array: The line at the bottom shows the result of retrieving a cached Object Array. |
Removing Items from the Cache in the Sample Application
The sample application allows you to remove individual items from the cache by specifying a cache key, or flush the cache to remove all items (see the last two buttons in
Figure 6). As you will have guessed, the code in the handlers for these buttons just has to call the appropriate CacheManager method.
To remove a single item, the code creates an instance of the specified CacheManager, calls the
Contains method to see if the item is in the cache, and—if it is—calls the
Remove method with the cache key selected in the list next to the Remove button:
...
// create the selected cache manager instance
CacheManager cm = CacheFactory.GetCacheManager(
optCacheManager.SelectedValue);
// remove the item with the specified key
String itemKey = lstRemoveItem.SelectedValue;
if (cm.Contains(itemKey))
{
cm.Remove(itemKey);
}
else
{
lblResults.Text = String.Format(
"Cache does not contain an item "
+ "with key '{0}'.", itemKey);
}
// display number of items in cache
lblResults.Text = String.Format(
"Cache '{0}' contains {1} item(s).",
optCacheManager.SelectedValue, cm.Count.ToString());
...
Clicking the "Remove all items" button creates an instance of the specified CacheManager and calls the
Flush method:
...
// create the selected cache manager instance
CacheManager cm = CacheFactory.GetCacheManager(
optCacheManager.SelectedValue);
// clear the cache
cm.Flush();
// display number of items in cache
lblResults.Text = String.Format(
"Cache '{0}' contains {1} item(s).",
optCacheManager.SelectedValue, cm.Count.ToString());
// disable "Retrieve" and "Remove" buttons
btn_Retrieve.Enabled = false;
btn_Remove.Enabled = false;
...
Notice that none of the routines in the test web site require you to specify the cache partition name. The partition name, like the URL of the target Caching Web Service, is part of the backing store provider configuration. Therefore, by specifying the backing store provider you want to use in the
GetCacheManager method, the system automatically reads from and writes to the correct cache partition.
You can add multiple Cache Managers and Cache Backing Store providers to an application configuration, including providers of different types, to meet almost any combination of caching requirements. If you need to support multiple caching web services, and multiple cache partitions, just add a separate Web Service Caching Provider for each combination.
Possible enhancements
While there are some limitations in the current implementation, you can easily add features you require to the base model described here.
You could extend the sample Web Service Cache Provider and make it even more useful by adding support for:
- Credentials that the proxy will use to access non-anonymous web services.
- An encryption provider to encrypt the data passing over the wire—although you can use SSL to communicate with the web service without adding this feature. You could implement encryption using the same techniques as in the Isolated Storage provider that is part of the Caching Application Block.
- Additional cache priorities.
You can use the caching mechanism described in this article to extend the capabilities of the Enterprise Library Caching Application Block to support almost any scenario for caching data remotely. While this project began as a "blue sky" concept implemented purely to see if it was possible, it does have practical applications and provides an interesting (and possibly useful) mechanism that you may want to take advantage of in your applications.