2010-11-26 72 views
1

當我初始化我的客戶端連接到的AppFabric的緩存,它似乎不一致需要長達30秒以下行連接:AppFabric的DataCacheFactory初始化往往需〜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 ... 
      } 
     } 
    } 
+0

我很樂意提供更多信息,如果它會有幫助嗎? – downatone 2010-12-01 17:38:02

回答

0

這是任何更快和/或如果你一直在config文件中的所有配置信息,而不是編程方式創建的配置更一致?有關詳細信息,請參閱here - 我總是使用此方法而不是程序化配置,因爲更改某些內容時更容易更新。

否則,我覺得一般的建議是,DataCacheFactory昂貴的對象創建,由於它的作用,即,使集羣中的網絡連接到各個服務器。你絕對不想在每次需要從緩存中獲取某些東西時創建一個DataCacheFactory,相反,你可能想要考慮將它創建爲Application_Start或許是一個單例,然後在整個應用程序中重用它(這被授予,沒有解決的問題,但它可能有助於緩解它)。

+0

感謝您的回覆 - 一直到我得到解決爲止,都沒有能夠推動它。我已經在Application_Start上調用Init(),並重用它的單例。一旦Init()完成,我沒有問題,它只是最初的潛力~30秒,這是踢球(見這裏:http://stackoverflow.com/questions/4338340/overlapped-recycle-and-application-start)。我會按照建議將AppFabric配置移動到web.config中,並會通知您是否可以解決問題。乾杯。 – downatone 2010-12-09 16:26:55