Using XML as Appfabric Caching Service configuration provider

Posted: February 22, 2011 in Appfabric
Tags: ,

 When you choose XML as your Appfabric Caching Service provider,configuration information is stored in an XML file on a network file share.Make sure that you create a shared folder in your server & browse to this folder or type in a UNC server share for example, \\server\share

If you do not configure it this way & simply click on next in the wizard,later when you try starting the cache cluster windows powershell will not recognize any of the commands as you have not configured it completely.

Once you are done with all configurations,you will be able to see a xml file named ClusterConfig inside the folder mentioned with content similar as below

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
  <section name="dataCache" type="Microsoft.ApplicationServer.Caching.DataCacheSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
- <dataCache size="Small">
- <caches>
- <cache consistency="StrongConsistency" name="default">
- <policy>
  <eviction type="Lru" />
  <expiration defaultTTL="10" isExpirable="true" />
  </policy>
  </cache>
  </caches>
- <hosts>
  <host replicationPort="22236" arbitrationPort="22235" clusterPort="22234" hostId="1038108123" size="1010" leadHost="true" account="AMERICAS\WN7-3M563BS$" cacheHostName="AppFabricCachingService" name="WN7-3M563BS" cachePort="22233" />
  </hosts>
- <advancedProperties>
- <securityProperties>
- <authorization>
  <allow users="Anupama_roy" />
  </authorization>
  </securityProperties>
  </advancedProperties>
  </dataCache>
  </configuration>

This has set up a cache with name default in your machine.I have downloaded a sample from microsoft appfabric samples & this below code from that sample explains how to make use of the cache.Before that from Caching Administration Windows PowerShell,grant access to your account as below

Grant-CacheAllowedClientAccount Anupama_roy

After this,start the cluster using ‘Start-CacheCluster’ command

public class CacheUtil
{
  private static DataCacheFactory _factory = null;
  private static DataCache _cache = null;
  public static DataCache GetCache()
  {

      if (_cache != null)
          return _cache;
      //-------------------------
      // Configure Cache Client
      //-------------------------
      //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("WN7-3M563BS", 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;
  }
}

Add below code to your webpage

  private DataCache m_cache = null;
  protected void Page_Load(object sender, EventArgs e)
    {
            m_cache = CacheUtil.GetCache();
            Order order = new Order();
            order.Id = Convert.ToInt32(OrderId.Text);
            order.Description = OrderDesc.Text;
            order.Amount = OrderAmt.Text;
            order.Quantity = Convert.ToInt32(OrderQuant.Text);
            //Add data to the cache
            m_cache.Add(OrderId.Text, order);

    }    

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s