2015-07-09 61 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访问数据库对象的任何类是你数据库的单一实例。

相关问题