Wednesday, 19 September 2012

Using Windows Server AppFabric Caching Service

AppFabric Caching Service is a distributed cache platform for in-memory caches spread across multiple systems, developed by Microsoft. It is one of Microsoft's AppFabric's Services. In its early stages of development, it was referred by a code name Velocity.

Some useful PowerShell commands:
Get-Cache [-HostName] [-PortNumber] Without any parameters, this will list information about all caches and regions on a cluster. Otherwise on a specified cache host.
Get-CacheHost [-HostName] [-PortNumber] Sees all of the cache services status in a cluster. If the parameters are used then of the host.
Creates a new named cache. It has a few other parameters that are optional.
Get-CacheAllowedClientAccounts Lists all accounts that have permission.
Grant-CacheAllowedClientAccount -Account "DOMAINNAME\username" Grants permission for an account.
Stops the specified cache service.
The exception message that you will see when it is down: ErrorCode:SubStatus:There is a temporary failure. Please retry later.
Start-CacheCluster Starts all cache services in the cluster.
Starts the specified cache service.
Restart-CacheCluster Restarts all cache services in the cluster.

GUI admin tool
There is also a GUI admin tool for managing the cache service:

Coding guide
To use the cache service in the codes, we need to include Microsoft.ApplicationServer.Caching.Client and Microsoft.ApplicationServer.Caching.Core to the project's References.

Examples of how to add an object to cache using Add method, Put method or Item property:

A simple codes example of how to use the cache service:
DataCacheFactory factory = new DataCacheFactory();
DataCache cache = factory.GetCache("test");

string key = "key1";
string value = "value1";
cache.Put(key, value);

var value = cache.Get(key);

For a thorough example that uses various features of AppFabric caching service, look at the CacheAPISample project of this example projects from Microsoft. Alternatively download the codes from here and run it as a console application.


Cache Administration with Windows PowerShell (Windows Server AppFabric Caching)

Managing Security (Windows Server AppFabric Caching)

Friday, 7 September 2012

Some Notes about Parallel Programming in .NET 4 (and 4.5)

- Try to avoid writing to shared memory object such as static variables or class properties. Using locks in parallelization will suffer the performance.

- Only use thread-safe methods. Calling to non thread-safe methods in parallel programming can cause exceptions and undetected data loss.

- Most of the time, any regular loop that fulfills both of the above requirements can be converted into a parallel loop.

- Keep it simple and avoid over-parallelization (i.e.; unnecessary nested parallelization). When using parallelization there is an overhead cost to partition the work and merge the result back.

- If the parallel work is used to populate data into a collection, use one of the thread-safe collection types.

- Avoid any ordering operation if possible. By default PLINQ does not preserve the ordering sequence in the source.

- When needed to do further operation of a PLINQ result, prefer to use ForAll() method instead of Parallel.ForEach().
For example; use this
      .Where( i => i.SomePredicate() )
      .ForAll( i => i.DoSomething() );
instead of
var filteredItems = source.AsParallel().Where( i => i.SomePredicate() );

Parallel.ForEach(filteredItems, item =>

Potential Pitfalls with PLINQ
Parallelism in .NET – Part 2, Simple Imperative Data Parallelism
Parallelism in .NET – Part 6, Declarative Data Parallelism
Parallelism in .NET – Part 8, PLINQ’s ForAll Method