Functoid to read from MemoryCache

Jun 4, 2015 at 10:17 AM
Hi Mapper Extensions UtilityPack team,

I'm currently working on a new version of the BRE Pipeline Framework. You might be aware but the current version of the framework allows for caching of context properties or any custom strings based on the .NET 4+ MemoryCache class.

One of the changes I'm making in the upcoming v1.6 of the framework is to change the access level on the cache to public and to change the framework from using a named instance of the cache to using MemoryCache.Default. The real benefit of this is that it opens the door to allowing inspection of entries in the cache from components beyond the BRE Pipeline Framework such as maps and unit tests.

I think it'd be a cool idea to have a functoid that allows you to get a cached value from MemoryCache.Default based on a key. Some simplified code to do this based on the BRE Pipeline Framework is below.
    public string GetCustomStringFromCache(string key, FailureActionEnum failureAction)
    {
        MemoryCache cache = MemoryCache.Default;
        string value = null;
        object obj = null;

        obj = cache.Get(key);

        if (obj == null)
        {
            if (failureAction == FailureActionEnum.Exception)
            {
                Exception exc = new Exception(string.Format("Unable to fetch item from cache with a key of {0}.", key));
                base.SetException(exc);
            }
            else if (failureAction == FailureActionEnum.BlankOrDefaultValue)
            {
                value = string.Empty;
            }
            else if (failureAction == FailureActionEnum.Null)
            {
                // Do nothing, leave as null
            }
        }
        else
        {
            value = obj.ToString();
        }

        return value;
    }
One of the obvious implications of this is that the MemoryCache class is based on .NET 4+ so if you are maintaining a BizTalk Server 2009 version of the UtilityPack then that would require a separate branch to be maintained for BizTalk Server 2010+.

Up to you guys if you think this is worth including, but I think it's a cool idea that opens the door to a whole lot of use cases.

Cheers
Johann