2017-03-22 64 views
0

我试图使用Azure Redis缓存来存储“多租户MVC应用程序”的输出缓存。我需要某种方式来区分存储在Redis服务器上的密钥。看看下面的截图,注意TenantId_a2/*,这里的关键字名称'TenantId',我想以编程方式进行控制。Azure Redis缓存动态应用程序名称或密钥名称

enter image description here

因为我使用Redis的输出缓存,我需要在web.config文件中配置Redis的输出缓存,截图在这里:

enter image description here

有配置另一种方式redis缓存从应用程序代码,但这不是输出缓存...我敢肯定。这是代码:

public class AzureRedisCache 
{ 
    private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => 
    { 
     string configString = "********"; 
     var options = ConfigurationOptions.Parse(configString); 
     options.ClientName = "TenantId"; // GREAT :) 
     options.AllowAdmin = true; 
     return ConnectionMultiplexer.Connect(options); 
    }); 

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

我们打电话,我们只是使用

IDatabase cache = AzureRedisCache.Connection.GetDatabase(); 

我不知道任何方式使用此代码输出缓存或一些其他的方式来实现上述要求。请建议。

回答

0

你在找这样的吗?如果您动态设置应用程序名称,它会有帮助吗?

OutputCacheSection ops = (OutputCacheSection)WebConfigurationManager.GetSection("system.web/caching/outputCache"); 
     ProviderSettings providerSettings = ops.Providers[0]; 
     providerSettings.Parameters["applicationName"] = "myDynamicApplicationName"; 
+0

为了澄清,您可以将上面的代码放到您的应用程序启动逻辑中。这不是您应该在应用程序执行期间随机更改的设置(例如,基于来自客户的输入)。 – JonCole

+0

非常感谢您的回复,这看起来是正确的,一旦我尝试这个,就会更新。 –

+0

是否可以根据applicationName从Redis中删除缓存? –