Share via


Programming Model (AppFabric 1.1 Caching)

There are now two available programming models for Microsoft AppFabric 1.1 for Windows Server:

  • Cache-aside programming pattern. This means that if your data is not present in the cache, your application, and not the AppFabric distributed cache, must reload data into the cache from the original data source.

  • Read-through/write-behind programming pattern. In this model, you create a custom provider to access the backend data store. If an item is not in the cache, the provider retrieves it from the backend. When items are written to a cache, the written items are periodically and asynchronously written to the backend. For more information about this model, see Read-Through and Write-Behind (AppFabric 1.1 Caching).

Caching Strategy

Application code should be designed so that it can function independent of the cache, and not require that cached data always be available. Because data in the cache is not persisted in a durable fashion, the possibility exists that the data in the case could be unavailable.

The high availability feature helps guard against computer and process failures from individual cache hosts while the cluster is running. But there may be scenarios when the entire cluster goes down. For example, if one too many lead hosts go down, the whole cluster will shut down. For more information, see Lead Hosts and Cluster Management (AppFabric 1.1 Caching).

There are many other reasons that your code may encounter a cache miss: the cache item may have expired or been evicted, the cache server may have rebooted, the cache service may have been restarted, or the cache cluster may have been accidentally restarted. Regardless of the reason, your application code should be able to access the database (or other data source) if the cached object is not available.

Cache Clients

To store data in the cache, use the GetCache method or the GetDefaultCache method to return a DataCache object. After it is instantiated, this DataCache object is referred to as the cache client.

Note

For performance reasons, we recommend that you minimize the number of DataCacheFactory objects created in a cache-enabled application. Store the DataCacheFactory object in a variable available to all parts of the application that use cache clients.

There are many options available to configure the behavior of the cache client. You can specify these configuration settings programmatically or with an application configuration file, or by using both approaches. For more information about cache clients and the available application configuration settings see Cache Clients and Local Cache (AppFabric 1.1 Caching) and Application Configuration Settings (AppFabric 1.1 Caching).

It is possible for more than one cache client to access a single cache at the same time. Applications running on different computers can do this by instantiating a cache client that is configured to use the same cache. The following code example demonstrates this concept. Note that comments are used to identify code executing on different cache client instances.

'Each application has a similar GetCache method call
Dim myCacheFactory As DataCacheFactory = New DataCacheFactory()
Dim catalog As DataCache = myCacheFactory.GetCache("catalog")

'One cache client saves an object to the catalog named "toy101"
Call catalog.Put("toy101", New ToyObject("toy101", "Playschool"))

'The same or different cache client retrieves the object
Dim toy As ToyObject = CType(catalog.Get("toy101"), ToyObject)

'The same or a different cache client removes the object
catalog.Remove("toy101")
//Each application has a similar GetCache method call
DataCacheFactory myCacheFactory = new DataCacheFactory();
DataCache catalog = myCacheFactory.GetCache("catalog");

//One cache client saves an object to the catalog named "toy101"
catalog.Put("toy101", new ToyObject("toy101", "Playschool"));

//The same or different cache client retrieves the object
ToyObject toy = (ToyObject)catalog.Get("toy101");

//The same or a different cache client removes the object
catalog.Remove("toy101");

See Also

Concepts

AppFabric Caching Physical Architecture Diagram (AppFabric 1.1 Caching)
AppFabric Caching Logical Architecture Diagram (AppFabric 1.1 Caching)
Developing a Cache Client

  2012-09-12