Share via


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

This walkthrough demonstrates how to add items to the cache.

To reproduce the demonstration

  1. Configure the cache. For the necessary steps, see Configuring Enterprise Library.

  2. Declare a member variable of type ICacheManager to store the CacheManager object in the QuickStartForm class.

    private ICacheManager primitivesCache;
    
    'Usage
    Private primitivesCache As ICacheManager
    
  3. In the QuickStartForm_Load method (when not using the Unity Integration approach), create the CacheManager by adding the following code. The call to GetCacheManager does not include the name of a CacheManager so the factory creates the default CacheManager object, as indicated in the configuration file.

    this.primitivesCache = CacheFactory.GetCacheManager();
    
    'Usage
    Me.primitivesCache = CacheFactory.GetCacheManager()
    
  4. Create the item that will be added to the cache. The following code creates an item of type Product.

    string id="ProductOneId";
    string name = "ProductOneName";
    int price = 50;
    
    Product product = new Product(id, name, price);
    
    'Usage
    Dim id As String = "ProductOneId"
    Dim name As String = "ProductOneName"
    Dim price As Integer = 50
    
    Dim newProduct As Product = New Product(id, name, price)
    
  5. Add the item to the cache. The following code uses the overload of the Add method that includes the scavenging priority (in this case, Normal), does not specify a callback handler, and sets an expiration time of 5 minutes from the time the item was last accessed.

    primitivesCache.Add(product.ProductID, product, CacheItemPriority.Normal, null,
                         new SlidingTime(TimeSpan.FromMinutes(5)));
    
    'Usage
    primitivesCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, Nothing, _
                         New SlidingTime(TimeSpan.FromMinutes(5)))