SharePoint Dragons

Nikander & Margriet on SharePoint

Windows AppFabric Cache API

A couple of years ago we wrote an article about the different ways of accessing SharePoint list items and leveraging enterprise cache in the process: http://www.loisandclark.eu/Pages/Velocity.aspx/ . As it turns out, the contents of the article are still very actual, but the Enterprise cache sample code for the Windows AppFabric Cache (which was called Velocity at the time) needs an update, although the process didn’t actually change that much.

First of all, don’t create any secondaries anymore when creating a new cache on a single server. Instead, do this:

new-cache -CacheName myCache -Secondaries 0

Adding references to the AppFabric caching assemblies is different too:

http://social.technet.microsoft.com/wiki/contents/articles/add-a-reference-to-the-microsoft-applicationserver-caching-client-assembly.aspx?CommentPosted=true#commentmessage

The sample code has changed as well. Signatures have changed a bit, and a couple of class names get prefixed with “Data”:

//Inspired by:
//http://www.hanselman.com/blog/InstallingConfiguringAndUsingWindowsServerAppFabricAndTheVelocityMemoryCacheIn10Minutes.aspx

//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(“astro”, 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     
var factory = new DataCacheFactory(configuration);      

//Get reference to named cache called “default”     
//var cache = factory.GetCache(“default”);      
var cache = factory.GetCache(“c1″);

//Then create a region inside the named cache:
try
{
    cache.RemoveRegion(“testregion”);
}
catch
{
    // No region found, but that’s ok.
}

// Pass true if you want to allow the eviction of cached objects.
cache.CreateRegion(“testregion”);

cache.Put(“testregion”, “test”);

//cache.Remove(“testregion”);

Finally, start the Caching Administration Windows PowerShell command prompt and use the following PowerShell command to check out the results of the code:

Get-CacheStatistics myCache

About these ads

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

Follow

Get every new post delivered to your Inbox.

Join 378 other followers

%d bloggers like this: