Scale ASP.NET Apps Through Distributed Caching: Listing 1.

Distributed cache in a .NET application for application data.

using DistCacheVendor.Web.Caching;

...

// Check the Cache before going to the database.
void Load(Customer cust)
{ 
  // Create a key to lookup in the cache.
  // The key will be like "Customer:PK:1000"
  string key = "Customer:CustomerId:" + cust.CustomerId.ToString();

  Customer customer = (Customer)Cache[key];
  if (customer == null)
  {
    // Item not found in the cache. Therefore, load from database.
    LoadCustomerFromDb(customer);

    // Now, let's add this object to the cache for future reference.
    Cache.Insert(key, customer, null, 
                   Cache.NoAbsoluteExpiration, 
                   Cache.NoSlidingExpiration, 
                   CacheItemPriority.Default, null );
  }

  cust.Copy((Customer)customer);
}

About the Author

Iqbal Khan is president, CEO and technology evangelist for Alachisoft in San Ramon, Calif., and a frequent contributor to TechNet Magazine.

comments powered by Disqus

Featured

Subscribe on YouTube