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

Before you can use cached data, you must first load data into the cache. For example, in a retail application, you may want to load data about various products, or all products, into the cache.

Typical Goals

In this scenario, you want to add data to the cache to improve the performance of an application.

Solution

There are two methods you can use for loading data:

  • Proactive loading. This method retrieves all the required state and then caches it for the lifetime of the application or the process.
  • Reactive loading. This method retrieves data when it is requested by the application and then caches it for future requests.

Caching Data Proactively

You can cache data proactively to retrieve all the required state for an application or a process, typically when the application or process starts, and cache it for the lifetime of the application or process.

Advantages of Proactive Loading

Because you can guarantee that the data has been loaded into the cache, in theory you do not have to check whether the state exists in the cache. However, check whether an item exists in the cache before retrieving it because the cache may have been flushed.

Your application performance improves because cache operations are optimized when proactively loading state into the cache. Application response times also improve because all the data is cached.

Disadvantages of Proactive Loading

Proactive loading does not result in the most optimized system because much of the state is cached even though it may not all be required. For example, an application may contain 100 processes, and each process may require some items in the cache. If a user launches this application but activates only one process, hundreds of items are needlessly cached.

Proactive caching may result in an implementation more complex than conventional techniques. With conventional techniques, each item is retrieved synchronously in a well-known program flow. Using proactive caching requires working with several threads; therefore, it can be difficult to synchronize the threads with the application's main thread, keep track of thread status, and handle exceptions in an asynchronous programming model.

Recommendations for Proactive Loading

If you do not correctly use proactive loading, applications may initialize slowly. When you implement proactive caching, load as much state as possible when the application initializes or when each process initializes. You should use an asynchronous programming model to load the state on a background thread.

Proactive caching is recommended in situations that have one or more of the following characteristics:

  • You are using static or semistatic state that has known update periods. If you use it in other scenarios, the state might expire before it is used.
  • You are using state with a known lifetime.
  • You are using state of a known size. If you use proactive cache data loading when you do not know the size of the data, you might exhaust system resources. You must try to not use resources that you do not have.
  • You have problematic resources, such as a slow database, a slow network, or unreliable Web services. You can use this technique to retrieve all the state proactively, cache it, and work against the cache as much as it can.

Caching Data Reactively

You can reactively cache data to retrieve data as it is requested by the application and cache it for future requests.

Advantages of Reactive Loading

Because you are not loading a lot of data when the application initializes, your system resources are not misused. This method results in an optimized caching system because you are storing only requested items.

Disadvantages of Reactive Loading

Performance might decrease when any piece of data is requested the first time because it must be loaded from the source; it is not retrieved from the cache. Also, you have to check whether an item exists in the cache before you can use it. Checking this in every service agent can cause excessive conditional logic in your code.

Recommendations for Reactive Loading

Reactive caching is recommended in situations with one or more of the following characteristics:

  • You are using a lot of state and you do not have sufficient resources to cache all state for the entire application.
  • You are using reliable and responsive resources, such as a database, network, or Web service, that will not impede application stability and performance.
  • You are interested in caching data that is not available during the initialization of an application. For example, this data might be affected by user input such as common search queries or user-specific data such as a user's profile.

For more information about proactive and reactive loading, see Application Architecture for .NET: Designing Applications and Services.

QuickStart

For an extended example of how to load the cache proactively and reactively, see the QuickStart walkthrough, Walkthrough: Loading the Cache.

Loading Examples

The following code demonstrates one way of proactively loading the cache. It reads the data stored in the master data source (in this case, an XML file) and immediately stores it in the cache, using the Add method. The GetProductList method is a part of the QuickStart; it is not a part of the Caching Application Block.

public List<Product> GetProductList()
{
  // This returns a list of product objects.
}

public void LoadAllProducts()
{
  CacheManager cache = CacheFactory.GetCacheManager();

  List<Product>list = GetProductList();

  for (int i = 0; i < list.Count; i++)
  {
    Product product = list[i];
    cache.Add(product.ProductID, product);
  }
}
Public Function GetProductList() As List(Of Product)
  ' This returns a list of product objects.
End Function

Public Sub LoadAllProducts()
  Dim cache As CacheManager = CacheFactory.GetCacheManager()

  Dim list As List(Of Product) = GetProductList()
  Dim i As Integer
  For i = 0 To list.Count - 1
    Dim product As Product = list(i)
    cache.Add(product.ProductID, product)
  Next
End Sub

The following code demonstrates one way of reactively loading the cache. It first checks to see whether the item is already in the cache. If it is not, it retrieves it from the master data source (in this case, an XML file) and then adds it to the cache using the Add method. The GetProductByID method is a part of the QuickStart; it is not a part of the Caching Application Block.

public Product GetProductByID(string anID)
{
  // This returns a product object with the specified ID.
} 

public Product ReadProductByID(string productID)
{
  CacheManager cache = CacheFactory.GetCacheManager();

  Product product = (Product)cache.GetData(productID);

  // Does our cache already have the requested object?
  if (product == null)
  {
    // The requested object is not cached, so retrieve it from
    // the data provider and cache it for further requests.
    product = this.dataProvider.GetProductByID(productID);

    if (product != null)
    {
      cache.Add(productID, product);
    }
  }
  return product;
}
Public Function GetProductByID(ByVal anID As String) As Product
  ' This returns a product object with the specified ID.
End Function

Public Function ReadProductByID(ByVal productID As String) As Product
  Dim cache As CacheManager = CacheFactory.GetCacheManager()
  Dim product As Product = DirectCast(cache.GetData(productID), Product)

  ' Does our cache already have the requested object?
  If (product Is Nothing) Then

    ' The requested object is not cached, so retrieve it from
    ' the data provider and cache it for further requests.
    product = GetProductByID(productID)

    If (Not product Is Nothing) Then
      cache.Add(productID, product)
    End If
  End If
  Return product
End Function
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.