Improve Parallel Cache with Reactive Extensions & Unity Interception The requirement is to be able to cache method invocations made on a boundary layer (Services layer). I'm using Unity to inject the concrete implementation of the Service layer classes. The intercepting CacheCallHandler caches all the responses across this layer.

The twist is that this is a multi-threaded environment with multiple clients invoking the same boundary layer in the context of a single request. If a service layer call is already in "flight", it should wait for the original invokers return and use that.

I've used Reactive extensions to implement this.

/// &lt;summary&gt;
/// Intercepts the calls and tries to retrieve from the cache
/// &lt;/summary&gt;
class CacheCallHandler : ICallHandler
{

    [Dependency]
    public ICache RequestCache { get; set; }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        IMethodReturn mesg = null;

        string cacheKey = CacheKeyGenerator.GetCacheKey(input);

        //create the task to retrieve the data
        var task = new Task&lt;IMethodReturn&gt;(() =&gt;
        {
            return getNext()(input, getNext);
        });

        //make it observable
        var observableItem = task.ToObservable();

        //try to add it to the cache
        //we need to do this in the order of Add and then try to get, otherwise multiple thread might enter the same area
        if (RequestCache.TryAdd(cacheKey, observableItem))
        {
            //if the add succeeed, it means that we are responsible to starting this task
            task.Start();
        }
        else
        {
            if ( RequestCache.TryGetValue(cacheKey, out observableItem) )
            {
                //do nothing, the observable item is already updated with the requried reference
            }
            else
            {
                throw new CacheHandlerException("Could not add to cache AND could not retrieve from cache either. Something's wrong", input);
            }
        }

        //observe the return 
        if ( observableItem != null )
            mesg = observableItem.FirstOrDefault();

        if (mesg == null)
            throw new CacheHandlerException("Not return value found. this should not happen", input);

        return mesg;
    }


    /// &lt;summary&gt;
    /// Should always be the first to execute on the boundary
    /// &lt;/summary&gt;
    public int Order
    {
        get { return 1; }
        set { ; }
    }
}
Personally i believe that there is no need for the reactive framework in this scenario.
To handle the multithreaded nature of your problem i would first make sure that your RequestCache (Icache) uses the ConcurrentDictionary> and exposrts the method GetOrAdd

With this your code will look like this:

/// &lt;summary&gt;
/// Intercepts the calls and tries to retrieve from the cache
/// &lt;/summary&gt;
class CacheCallHandler : ICallHandler
{

    [Dependency]
    public ICache RequestCache { get; set; }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        return InvokeAsync.Result;
    }

    public Task&lt;IMethodReturn&gt; InvokeAsync(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        IMethodReturn mesg = null;

        string cacheKey = CacheKeyGenerator.GetCacheKey(input);

        //create the task to retrieve the data
        var task = RequestCache.GetOrAdd(
               cacheKey,
               key =&gt; new Task.Factory.StartNew(() =&gt; getNext()(input, getNext))
               );

        return task;       
    }


    /// &lt;summary&gt;
    /// Should always be the first to execute on the boundary
    /// &lt;/summary&gt;
    public int Order
    {
        get { return 1; }
        set { ; }
    }
}


The asynchronous version offers the option of to blocking the calling thread when invoking it.