AppFabric DataCacheFactory初始化通常需要约30秒

时间:2010-11-26 17:05:50

标签: c# asp.net appfabric

当我初始化我的客户端以连接到AppFabric的缓存时,在以下行上连接似乎不一致需要30秒:

factory = new DataCacheFactory(configuration); 

请参阅下面的完整Init()代码 - 主要来自here。 我说不一致,因为有时需要1秒钟,其他时间需要27,28等......秒。我有一个使用AppFabric缓存的asp.net站点 - 它位于不同的盒子上(在同一个域上)。除了连接时间不一致外,一切都很顺利。当它连接时,一切都很好 - 我只需要让它在~1秒内连续连接:) ...思考?

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 ...
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

如果将所有配置信息保存在.config文件中而不是以编程方式创建配置,它是否更快和/或更一致?有关详细信息,请参阅here - 我总是使用此方法而不是编程配置,因为在更改内容时更容易更新。

否则我认为一般的建议是DataCacheFactory 是一个昂贵的对象,因为它的作用是创建,即与群集中的每个服务器建立网络连接。您绝对不希望每次需要从缓存中获取内容时都创建DataCacheFactory,而您可能想要考虑在Application_Start中创建它可能是单例,然后在整个应用程序中重用它(哪个被授予,不会解决问题,但它可能会起到缓解作用的作用。)