Geeks With Blogs

News
Elton Stoneman (@EltonStoneman) IT Consultant, integration specialist, @Microsoft MVP and @Pluralsight author.

[Source: http://geekswithblogs.net/EltonStoneman]

If you're doing cross-reference lookups in a BizTalk map you have a few design options (see BizTalk Pattern: Mapping Reference Data), and the BizTalk native XRef lookup can provide lookup caching (see BizTalk XRef Undocumented feature) - and you'll need caching if your maps runs against large messages.

I prefer not to use the native XRef option because the values are hidden away in the BizTalk schema, are cumbersome to load, and are only available to BizTalk maps. A custom cross-reference implementation is fairly trivial, can be made available to any .NET consumer, and you can control what gets cached and how long the cache lives.

Assuming you have the lookup code in a helper class separate from the map, BizTalk will create a new instance of the helper class for every run of the map. To get the benefit of caching across different calls to the map, you can use a static dictionary of lookup values (see Implement Caching for your BizTalk applications using "static" classes and methods).

The problem with using a static dictionary is that it lives for the duration of the app domain – so the lookup values will be used for every run of the map until the BizTalk host instance is restarted. This is also true for the native Xref cache. Removing stale cache items becomes a manual process, liable to be forgotten.

In a custom implementation you can automate flushing the cache. Monitoring the data source and flushing when it changes is an expensive option, but a cheap alternative is to store the expected lifespan of the cache in configuration, so when the cache reaches the configured age it flushes itself:

<x.y.z.integration.maps>

  <caching>

    <cache key="StatusCodeLookup" lifespan="P1D"/>

    <cache key="CustomerLookup" lifespan="PT10M"/>

  </caching>

</x.y.z.integration.maps>

Returning the lookup value then falls into three parts – flush the cache if expired, check the cache, get from source and add to cache if not already present:

public string GetStatusCode(string inputCode)

    {

        EnsureStatusCode(inputCode);

        return _statusCodes[inputCode];

    }

 

    private static void EnsureStatusCode(string inputCode)

    {

        FlushCacheIfExpired();

        if (!_statusCodes.ContainsKey(inputCode))

        {

            string outputCode = GetLookupValue(inputCode);

            lock (_syncLock)

            {

                _statusCodes[inputCode] = outputCode;

            }

        }

    }

 

    private static void FlushCacheIfExpired()

    {

    TimeSpan lifespan = MapConfiguration.Current.Caching.GetLifespan(CacheConfigKey.StatusCodeLookup);

    DateTime expiry = _lastCacheLoad.Add(lifespan);

        if (DateTime.Now > expiry)

        {

            ResetCache();

        }

    }

Full sample on github here: ConfigurableCrossReferenceCache. (This snippet omits logging and error handling for brevity, but in the sample there's plenty of both, which makes building and monitoring your cache much easier).

Using BTSNTSvc.exe.config for the configuration means that if you need to change the lifespan of a cache item you'll need to restart host instances, but if that's an issue you can use Enterprise Single Sign-On as your configuration store, and lookup the value on every access.

Posted on Sunday, August 15, 2010 9:05 PM BizTalk 2009 , Caching , github | Back to top


Comments on this post: Caching and Flushing the Cache in BizTalk Maps

# re: Caching and Flushing the Cache in BizTalk Maps
Requesting Gravatar...
great tips! thanks for the taking the to share.
Left by Dating Reviews on Sep 27, 2010 11:44 PM

# re: Caching and Flushing the Cache in BizTalk Maps
Requesting Gravatar...
Great stuff!
This is quite similar to what we have done to cache data in maps. Apart from that we reused .net web APIs to exploit sliding expiration etc.

Cheers.
Left by Suneet on Oct 15, 2010 10:30 AM

# re: Caching and Flushing the Cache in BizTalk Maps
Requesting Gravatar...
I prefer not to use the native XRef option because the values are hidden away in the BizTalk schema, are cumbersome to load, and are only available to BizTalk maps.
Left by pay day loans uk on Oct 22, 2010 5:41 AM

# re: Caching and Flushing the Cache in BizTalk Maps
Requesting Gravatar...
Wow!!!! This is really a fabulous informative post. It helps me a lot and also the experts. Thank u very much. Good job. Keep it up.
Left by digital scrapbook on Oct 22, 2010 1:24 PM

# re: Caching and Flushing the Cache in BizTalk Maps
Requesting Gravatar...
I think a lot people forget about caching strategies and how they can benefit from it.
Left by virtual hosting on Jan 24, 2011 8:26 AM

# re: Caching and Flushing the Cache in BizTalk Maps
Requesting Gravatar...

The tutorials are very handy and i am excited to apply them and see how it will turn out.
Left by escort website design on Nov 24, 2011 7:40 PM

# re: Caching and Flushing the Cache in BizTalk Maps
Requesting Gravatar...

Thanks for writing valuable post regarding the subject.
I’m a fan of your site. Keep up the great work.
Left by kolacici.com on May 11, 2012 2:57 PM

Your comment:
 (will show your gravatar)
 


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net | Join free