ESB Toolkit : Resolver Cache

Below are the important code fragments you’ll need to implement caching in your custom resolvers.

To enable the use of the resolverConfig section of the esb.config for a resolver you must implement a resolver that exposes a parameter of type Microsoft.Practices.ESB.Configuration.Resolver. See below example.

public ResolveProvider(Microsoft.Practices.ESB.Configuration.Resolver resolverConfig)
        {
            try
            {
                if (resolverConfig == null)
                {
                    throw new ArgumentException("resolverConfig");
                }

                timeout = int.Parse(ResolverConfigHelper.ReadResolverConfigByKey(resolverConfig, "cacheTimeoutValue"), NumberStyles.Integer, CultureInfo.CurrentCulture);
                // Init cache
                _Cache = new Microsoft.Practices.ESB.Cache.Cache<Dictionary<string, string>>("Resolver Providers Cache Manager");
            }
            catch (Exception ex)
            {
                EventLogger.Write(System.Reflection.MethodBase.GetCurrentMethod(), ex);
                throw (ex);
            }
        }

Method to set cache. Note the timeout which sets the cache expiry seconds

        private static void SetCache(Dictionary<string, string> resolution, string config)
        {
            try
            {
                _Cache.Add(config, resolution, new TimeSpan(0, 0, timeout));
            }
            catch (Exception exception)
            {
                EventLogger.Write(System.Reflection.MethodBase.GetCurrentMethod(), exception);
                throw;
            }
        }

Method to retrieve from cache

        private static Dictionary<string, string> RetrieveFromCache(string config)
        {
            Dictionary<string, string> dictionary;
            try
            {
                dictionary = _Cache.Get(config) as Dictionary<string, string>;
            }
            catch (Exception exception)
            {
                EventLogger.Write(System.Reflection.MethodBase.GetCurrentMethod(), exception);
                throw;
            }
            return dictionary;
        }

The logic to implement the above is simple. On every call to the resolver simply call the RetrieveFromCache method. If null is returned from this method the cache is empty and we execute our code to retrieve values from for example SQL if we are implementing a SQL resolver, we then update the cache using the SetCache method. If the call to RetrieveFromCache is not null then we know the cache hasn’t expired and we can use the in memory values.

Advertisements
This entry was posted in BizTalk Server. Bookmark the permalink.

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