Share via


AppFabric Caching API Usage Sample

This sample shows how to use various AppFabric Cache APIs.

Note

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples.

Prerequisites

Ideally, users should have knowledge of Windows PowerShell scripting.

The sample assumes the following:

  • Windows PowerShell 2.0 is installed.

  • Microsoft AppFabric 1.1 for Windows Server is installed. (This also adds the required DLLs to the GAC.)

Configure the AppFabric Cache

If you have not done so already, configure the AppFabric cache by using the Microsoft AppFabric 1.1 for Windows Server configuration tool (use default settings). The sample assumes that an AppFabric cache is configured with default settings (cacheHostName=AppFabricCachingService and cacheName=default).

Start the Cache Cluster

  1. Open a Windows PowerShell command window in elevated mode and run the following command to add the Distributed Cache administration module:

    Import-Module DistributedCacheAdministration
    
  2. Use the Use-CacheCluster command to set the context of your Windows PowerShell session to the desired cache cluster. This can be run without parameters to use the connection parameters provided when the AppFabric Caching Administration component was configured.

  3. Run the following command to grant your user account access to the cache cluster as a client. Supply your user and domain name.

    Grant-CacheAllowedClientAccount domain\username
    
  4. Use Get-CacheAllowedClientAccounts command to verify your user account has been granted access.

  5. Start the cluster by using Start-CacheCluster command.

Build and Run This Sample

  1. Open CacheAPI.sln solution file in Visual Studio. The sample automatically links to the AppFabric Cache DLLs (Microsoft.ApplicationServer.Caching.Client.dll and Microsoft.ApplicationServer.Caching.Core.dll) from the GAC. The sample demonstrates the following usage patterns:

    • Remove/Create a named region

    • Simple Add/Get to the default cache/region

    • Add/Get to a named region in the default cache

    • Add/Get/GetAndLock/GetIfNewer/Put/PutAndUnlock to a named region. Variations:

      • GetAndLock trying to access a locked object

      • PutAndUnlock trying to unlock an object that is not locked

    • Add/Get/Put of a versioned object

      • Explicitly modify an item only if versions match

      • Fail to modify an item if a newer item is available in the cache.

    • Simple Add/GetByTag to show the use of tags

    Note

    For each subsequent run of this sample, use the Restart-CacheCluster command to clear the cache, and wait for 10-15 seconds.

Remove This Sample

Restart the cache cluster by using the Restart-CacheCluster command to clean up data from the cache.

See Also

Other Resources

Samples (AppFabric 1.1 Caching)

  2012-09-12