2015-07-09 41 views
1

創造一次根據這個指南: https://github.com/mspnp/azure-guidance/blob/master/Retry-Service-Specific.md如何訪問一個變量,我只需要在Application.Start

他們說:

注意,StackExchange.Redis客戶通過單一連接使用複用。建議的用法是在應用程序啓動上創建客戶端的實例,並針對緩存使用此實例進行所有操作。出於這個原因,與緩存的連接只進行一次,因此本節中的所有指導都與此初始連接的重試策略相關,而不是針對訪問緩存的每個操作。

現在,我有這樣的事情:

public static Models.UserProfile GetUserProfile(string identityname) 
     { 
      /// It needs to be cached for every user because every user can have different modules enabled. 
      try 
      { 
       var cachekeyname = "UserProfileInformation|" + identityname; 
       IDatabase cache = CacheConnectionHelper.Connection.GetDatabase(); 
       Models.UserProfile userProfile = new Models.UserProfile(); 
       object obj = cache.Get(cachekeyname); 

我可以移動的連接線的Global.asax

protected void Application_Start() 
     { 

      IDatabase cache = CacheConnectionHelper.Connection.GetDatabase(); 

     } 

如果我提出這條線,那麼如何才能得到該實例上我需要使用它的其他方法?

這是緩存連接助手

public class CacheConnectionHelper 
    { 
     private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => 
     { 
      return ConnectionMultiplexer.Connect(SettingsHelper.AzureRedisCache); 
     }); 

     public static ConnectionMultiplexer Connection 
     { 
      get 
      { 
       return lazyConnection.Value; 
      } 
     } 
    } 

回答

1

你可以把它在Global.asax文件內部的靜電

public class Global : HttpApplication { 
    public static IDatabase Cache = CacheConnectionHelper.Connection.GetDatabase(); 
    void Application_Start(object sender, EventArgs e) { 

    } 
    ..... 
} 

現在,你可以通過簡單地訪問Global.Cache訪問數據庫對象的任何類是你數據庫的單一實例。

相關問題