Programming Model (Windows Server AppFabric Caching)

The Windows Server AppFabric programming model is customized for the 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. Application code uses the DataCache class, also referred to as a cache client, after it is instantiated.

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 (Windows Server AppFabric 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.

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 (Windows Server AppFabric Caching) and Application Configuration Settings (Windows Server AppFabric 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
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

See Also

Community Additions