AppFabric Caching
AppFabric Caching provides an in-memory, distributed cache platform for Windows Server. Microsoft developed AppFabric Caching and released it as part of AppFabric.

Architecture
AppFabric Caching stores serialized managed objects in a cache cluster. The cache cluster consists of one or more machines that pool their available physical memory. This pooled memory is presented to cache clients as a single source of caching memory. Objects are stored and accessed using an associated key value.
 
AppFabric Caching features must be installed on each server in the cache cluster. Following installation, the AppFabric Configuration Wizard must be used to join each server to the cache cluster. An external file share or database is required to maintain the cache cluster configuration settings. A set of Windows PowerShell commands for Caching provides administration capabilities on the cache cluster.

Code examples
Note that the code samples in this section are shown in C#.

A common task is to create code that puts, retrieves, and removes objects from the cache. These operations target either the default cache or a named cache.

First, create a static DataCache member:

public static DataCache _cache;

Next, create a method that accesses this cache. The properties of the cache can be stored in the app.config or web.config files. The cache settings can also be programmatically configured. The following example shows how to programmatically configure the cache.

public static DataCache GetCache()
{
    if (_cache != null)
        return _cache;
 
    // Define Array for 1 Cache Host
    List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(1);
 
    // Specify Cache Host Details
    //   Parameter 1 = host name
    //   Parameter 2 = cache port number
    servers.Add(new DataCacheServerEndpoint("mymachine", 22233));
 
    // Create cache configuration
    DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
       
    // Set the cache host(s)
    configuration.Servers = servers;
       
    // Set default properties for local cache (local cache disabled)
    configuration.LocalCacheProperties = new DataCacheLocalCacheProperties();
 
    // Disable tracing to avoid informational/verbose messages on the web page
    DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);
 
    // Pass configuration settings to cacheFactory constructor
    _factory = new DataCacheFactory(configuration);
 
    // Get reference to named cache called "default"
    _cache = _factory.GetCache("default");
       
    return _cache;
}

The following method shows how to use the _cache object to retrieve data from the cache. In this example, a user identifier (userid) is the key for the associated user information object. The code first attempts to get this user information from the cache using the userid key. If that does not succeed, the code retrieves the information with a database query and then stores the returned user data in the cache. The next time the same code is run, the user information will be returned from the cache rather than the database. This assumes that the cached data has not been expired or evicted.

dataType GetUserData(string userId) 
{
    dataType data = null;

    // Attempt to retrieve the user data from the cache:
    object dataObject = _cache.Get(userId);

    if (dataObject != null)
        data = (dataType)dataObject;
    else
    {
        // If it doesn't exist in the cache, retrieve it from the database:
        data = GetUserDataFromDatabase("SELECT * FROM users WHERE userid = @userId", userId);

        // Put the returned data in the cache for future requests:
        _cache.Add(userId, data);
    }

    return data;
}

The following method shows how to update data that is already in the cache.

void UpdateUserData(string userId, dataType data) 
{
    // Update the user information in the database:
    result = UpdateUserDataInDatabase(userId, data);
    
    if (result) 
    {
        // If successfully updated, update the cache:
        _cache.Put(userId, data);
    }
}

The following call removes the item from the cache.

_cache.Remove(userId);

History
Originally, AppFabric Caching had several beta releases under the code name Velocity. In June 2010, Microsoft officially released AppFabric Caching as part of AppFabric. For more detailed information, see the history section of the AppFabric page.

Related caching technologies
AppFabric Caching is related to other Microsoft caching technologies. These technologies share similar features, such as the assembly name, namespace, and types. However, there are some differences. The table below describes these technologies.

References

External links 
 AppFabric 1.1 Caching Features
 AppFabric Class Library Reference
 Download: Microsoft AppFabric 1.1 for Windows Server
 Download: Cumulative update package 3 for Microsoft AppFabric 1.1 for Windows Server

Cache (computing)
Microsoft software
Windows Server