Partilhar via


Adding Items to the Cache

Caches store items that are either expensive to create or expensive to transport. For example, in a retail application, a list of products must be passed from the data access components to the user interface components so that the product list can be displayed to the users. The data represents real-world business entities, such as products or orders. To increase performance, some of these items may be added to the cache.

Typical Goals

In this scenario, you want to add items to the cache. You may also want to set the expiration periods for the expiration process and the priorities for the scavenging process.

Solution

Use the Add method provided by the CacheManager class. The CacheManager class adds a new item to the cache. If you do not explicitly set the expiration period and the priority properties, they are set to the default settings. The default settings in this case are NeverExpired and Normal. If another item already exists with the same key, that item is removed before the new item is added. If any failure occurs during this process, the cache is restored to its original state.

Using the Add Method

The following code shows how to use the Add method. It creates an object of type Product and then adds it to the cache with a scavenging priority of Normal, an instruction not to refresh the item if it expires, and an expiration date of 5 minutes from the last time the item was accessed.

Note

The code does not include the Product class definition, and assumes that you resolve the class to obtain an instance of the default configured CacheManager class. For more information on instantiating objects, see Creating and Referencing Enterprise Library Objects.

public void ExampleScenario(ICacheManager productsCache)
{
  string id = "ProductOneId";
  string name = "ProductOneName";
  int price = 50;

  Product newProduct = new Product(id, name, price);

  productsCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal,
                    null, new SlidingTime(TimeSpan.FromMinutes(5)));
}
'Usage
Public Sub ExampleScenario(ByVal productsCache As ICacheManager)

  Dim id As String = "ProductOneId"
  Dim name As String = "ProductOneName"
  Dim price As Integer = 50

  Dim newProduct As Product = New Product(id, name, price)

  productsCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, _
                    Nothing, New SlidingTime(TimeSpan.FromMinutes(5)))

End Sub

Refreshing Removed Items

By using the Add method, you can specify an object that implements the interface ICacheItemRefreshAction. The Refresh method of the object is called whenever an item is removed from the cache, providing the opportunity to refresh items in the cache.

The following code shows a class that implements the ICacheItemRefreshAction interface.

[Serializable]
public class ProductCacheRefreshAction : ICacheItemRefreshAction
{
  public void Refresh(string key, object expiredValue, CacheItemRemovedReason removalReason)
  {
    // Item has been removed from cache. Perform desired actions here, based on
    // the removal reason (for example, refresh the cache with the item).
  }
}
'Usage
<Serializable()> _
Public Class ProductCacheRefreshAction
       Implements ICacheItemRefreshAction
  Public Sub Refresh(ByVal removedKey As String, ByVal expiredValue As Object, ByVal removalReason As CacheItemRemovedReason) _
         Implements ICacheItemRefreshAction.Refresh
    ' Item has been removed from cache. Perform desired actions here, based on
    ' the removal reason (for example, refresh the cache with the item).
  End Sub
End Class

Receiving notification of an item's removal from the cache requires that you specify the class implementing ICacheItemRefreshAction on the call to the Add method, as shown in the following code.

public void ExampleScenario(ICacheManager productsCache)
{
  string id = "ProductOneId";
  string name = "ProductOneName";
  int price = 50;

  Product newProduct = new Product(id, name, price);

  productsCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, 
                    new ProductCacheRefreshAction(), new SlidingTime(TimeSpan.FromMinutes(5)));
}
'Usage
Public Sub ExampleScenario(ByVal productsCache As ICacheManager)

  Dim id As String = "ProductOneId"
  Dim name As String = "ProductOneName"
  Dim price As Integer = 50

  Dim newProduct As Product = New Product(id, name, price)

  productsCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, _
                    New ProductCacheRefreshAction, New SlidingTime(TimeSpan.FromMinutes(5)))

End Sub

Usage Notes

Here is some additional information: