A Simple Example
Here's a simple example in which you can follow along with the steps involved in inserting, retrieving, and removing a simple scalar item from the cache. To start, create a new Visual C# Windows Application named
CachingBlockExample, and add references to the
Microsoft.Practices.EnterpriseLibrary.Caching.dll and
Microsoft.Practices.EnterpriseLibrary.Common.dll assemblies by navigating to the
<DriveName>:\Program Files\Microsoft Enterprise Library 3.1—May 2007\Bin folder.
In the main form code-behind, import the following caching related namespaces:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
After that, declare a private variable named
_manager:
private CacheManager _manager;
Next add an application configuration file (
app.config) to the project and modify it to look as follows:
<configuration>
<configSections>
<section name="cachingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.
Caching.Configuration.CacheManagerSettings,
Microsoft.Practices.EnterpriseLibrary.Caching" />
</configSections>
<cachingConfiguration
defaultCacheManager="Default Cache Manager">
<backingStores>
<add name="inMemory"
type="Microsoft.Practices.EnterpriseLibrary.
Caching.BackingStoreImplementations.NullBackingStore,
Microsoft.Practices.EnterpriseLibrary.Caching" />
</backingStores>
<cacheManagers>
<add name="Default Cache Manager"
expirationPollFrequencyInSeconds="60"
maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10"
backingStoreName="inMemory" />
<add name="Loading Scenario Cache Manager"
expirationPollFrequencyInSeconds="60"
maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10"
backingStoreName="inMemory" />
</cacheManagers>
</cachingConfiguration>
</configuration>
These configuration file settings instruct the Caching Application Block to use the in-memory backing store (also known as
null backing store) for persisting cached items. Note that the configuration also sets the
inMemory cache provider as the default cache manager:
<cachingConfiguration
defaultCacheManager="Default Cache Manager">
As part of the
<cacheManagers> element, you also specify the scavenging behavior of the cache engine in terms of the maximum number of elements in cache before scavenging is kicked off, as well the as the number of elements to remove when the scavenging runs.
In the form's constructor, populate the
CacheManager variable by invoking the
CacheFactory.GetCacheManager() method:
public Form1()
{
InitializeComponent();
_manager = CacheFactory.GetCacheManager();
}
The
GetCacheManager() method returns a reference to the default CacheManager object. As the in-memory cache manager is the default for this example, that's sufficient. If you define multiple cache managers, you can get a reference to a specific cache manager by supplying the name of the cache manager (as specified in the configuration file) as an argument to the overloaded
GetCacheManager() method.
Now you are ready to add code to store, retrieve, and remove an item from the cache. The code below begins by showing the operations required for scalar items:
private void btnAddScalarItemToCache_Click(
object sender, EventArgs e)
{
lstResults.Items.Clear();
_manager.Add("12345", "David");
lstResults.Items.Add("Scalar item added");
}
private void btnRetrieveScalarItem_Click(
object sender, EventArgs e)
{
lstResults.Items.Clear();
object value = _manager.GetData("12345");
if (value != null)
lstResults.Items.Add((string)value);
else
lstResults.Items.Add("No item found");
}
private void btnRemoveScalarItem_Click(
object sender, EventArgs e)
{
lstResults.Items.Clear();
_manager.Remove("12345");
lstResults.Items.Add
("Scalar item identified by 12345 removed from the cache");
}
 | |
| Figure 2. Retrieving a Scalar Item from the Cache: After adding an item to the cache, you can easily retrieve and remove that item from the cache using the cache key. |
The
CacheManager.Add() method has two overloads; the preceding code uses the one that accepts a cache key and the actual data to be cached as arguments:
_manager.Add("12345", "David");
After adding the item, you can retrieve it using the
GetData() method that accepts the cache key as an argument:
object value = _manager.GetData("12345");
Figure 2 shows the output produced when you retrieve the cached item after persisting it through the "Add a scalar item to the cache" button.