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 Entering Configuration Information.

  2. Declare a member variable for CacheManager in the QuickStartForm class.

    private CacheManager primitivesCache;
    
    Private primitivesCache As CacheManager
    
  3. In the QuickStart_Load method, 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();
    
    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);
    
    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, 2), instructions to not refresh the item if it expires, and 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)));
    
    primitivesCache.Add(newProduct.ProductID, newProduct, CacheItemPriority.Normal, Nothing, _
                         New SlidingTime(TimeSpan.FromMinutes(5)))
    
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.