开发者

AppFabric DataCacheFactory initialization often takes ~30 seconds

When I initialize my client to connect to AppFabric's cache, it seems to inconsistently take up to 30 seconds to connect on the following line:

factory = new DataCacheFactory(configuration); 

See full Init() code below - mostly taken from here. I say inconsistently because sometimes it takes 1 second and other times 27, 28 , etc ... seconds. I have an asp.net si开发者_运维百科te using the AppFabric cache - which lives on a different box (on the same domain). Everything is working great, except for the inconsistent connection time. When it connects, its all good - I just need to get it to consistently connect in ~1 second :) ... Thoughts?

public static void Init()
    {
        if (cache == null)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                //Define Array for 1 Cache Host
                List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>(1);

                var appFabricHost = ConfigurationManager.AppSettings["AppFabricHost"];
                var appFabricPort = ConfigurationManager.AppSettings["AppFabricPort"].ParseAs<int>();

                //Specify Cache Host Details 
                //  Parameter 1 = host name
                //  Parameter 2 = cache port number
                servers.Add(new DataCacheServerEndpoint(appFabricHost, appFabricPort));
                TraceHelper.TraceVerbose("Init", string.Format("Defined AppFabric - Host: {0}, Port: {1}", appFabricHost, appFabricPort));

                //Create cache configuration
                DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();

                //Set the cache host(s)
                configuration.Servers = servers;

                //Set default properties for local cache (local cache disabled)
                configuration.LocalCacheProperties = new DataCacheLocalCacheProperties();

                //Disable tracing to avoid informational/verbose messages on the web page
                DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

                //Pass configuration settings to cacheFactory constructor
                factory = new DataCacheFactory(configuration);

                //Get reference to named cache
                cache = factory.GetCache(cacheName);
                TraceHelper.TraceVerbose("Init", "Defined AppFabric - CacheName: " + cacheName);
            }
            catch (Exception ex)
            {
                TraceHelper.TraceError("Init", ex);
            }
            finally
            {
                TraceHelper.TraceInfo("Init", string.Format("AppFabric init took {0} seconds", sw.Elapsed.Seconds));
            }

            if (cache == null)
            {
                TraceHelper.TraceError("Init", string.Format("First init cycle took {0} seconds and failed, retrying", sw.Elapsed.Seconds));
                UrlShortener.Init();    // if at first you don't succeed, try try again ...
            }
        }
    }


Is it any faster and/or more consistent if you keep all the configuration info in a .config file rather than creating your configuration programmatically? See here for details - I would always use this method as opposed to the programmatic configuration as it's much easier to update when something changes.

Otherwise I think the general advice is that DataCacheFactory is an expensive object to create due to what it does i.e. makes a network connection to each server in the cluster. You definitely don't want to be creating a DataCacheFactory every time you need to get something from the cache, instead you might want to think about creating it in Application_Start as perhaps a singleton and then reusing that one throughout your application (which, granted, doesn't solve the problem but it might serve to mitigate it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜