Export (0) Print
Expand All

Adding Items to the Cache

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

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.

QuickStart

For an extended example of how to use the Add method, see the QuickStart walkthrough, Walkthrough: Adding Items to the Cache.

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, together with a scavenging priority of 2, 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.

Ff649800.note(en-us,PandP.10).gifNote:
The code does not include the Product class definition.

CacheManager productsCache = CacheFactory.GetCacheManager();

string id = "ProductOneId";
string name = "ProductXYName";
int price = 50;

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

productsCache.Add(product.ProductID, product, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(5)));

Dim productsCache As CacheManager = CacheFactory.GetCacheManager()

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)))

Refreshing Removed Items

The Add method allows you to 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).
    }
  }

<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.

CacheManager productsCache = CacheFactory.GetCacheManager();

string id = "ProductOneId";
string name = "ProductXYName";
int price = 50;

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

productsCache.Add(product.ProductID, product, CacheItemPriority.Normal, new ProductCacheRefreshAction(), new SlidingTime(TimeSpan.FromMinutes(5)));

Dim productsCache As CacheManager = CacheFactory.GetCacheManager()

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)))

Usage Notes

Here is some additional information:

  • When adding any item to a cache configured to use a database as a backing store, the item's type must be serializable.
  • The class implementing the ICacheItemRefreshAction interface must be serializable when using a database as the backing store.
  • For more information about serialization, see Object Serialization in the .NET Framework on MSDN.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.
Show:
© 2014 Microsoft