2017-03-22 59 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中刪除緩存? –