2017-10-18 28 views
12

在ASP.NET的Core 2我們可以添加一個Azure的Redis的緩存是這樣的:ASP.NET睿2 - 多Azure的Redis的緩存服務DI

services.AddDistributedRedisCache(config => 
{ 
    config.Configuration = Configuration.GetConnectionString("RedisCacheConnection"); 
    config.InstanceName = "MYINSTANCE"; 
}); 

然後使用將是這樣的:

private readonly IDistributedCache _cache; 

public MyController(IDistributedCache cache) 
{ 
    _cache = cache; 
} 

我怎麼能做到這一點,讓我有:

private readonly IDistributedCache _cache1; 
private readonly IDistributedCache _cache2; 

public MyController(IDistributedCache cache1, IDistributedCache cache2) 
{ 
    _cache1 = cache1; 
    _cache2 = cache2; 
} 

我的問題,我怎麼能添加指向不同的Redis Azure的另一個緩存服務當我想要使用它們時,連接和實例並將它們分開?

+0

這種先進的方案是不使用默認的'IDistributedCache'做法確實管理。您應該使用像[CacheManager](http://cachemanager.michaco.net/)這樣的庫,它允許您根據類型參數定義不同的緩存。 –

+2

一個潛在的選擇是使用[策略模式](https://stackoverflow.com/a/46597099)來選擇要在運行時使用的緩存。 – NightOwl888

回答

8

幕後,AddDistributedRedisCache()擴展方法執行以下操作(code on github):

  1. 寄存器操作以配置RedisCacheOptions。您傳遞給AddDistributedRedisCache()的Lambda對此負責。 RedisCacheOptions的實例傳遞給RedisCache的構造函數,包裝成IOptions<T>
  2. 註冊單點執行RedisCacheIDistributedCache接口。

不幸的是,這兩種行爲都不適合您的要求。 只能配置一個動作來配置特定類型的選項。 本地執行.net核心依賴注入does not support註冊覆蓋。

仍然有一個解決方案,將做你想做的。然而這個解決方案有點讓我失望

訣竅是您從RedisCacheOptions繼承自定義的RedisCacheOptions1,RedisCacheOptions2併爲它們註冊不同的配置。

然後,定義從IDistributedCache繼承的自定義IDistributedCache1和IDistributedCache2接口。

最後你定義RedisCache1類(它繼承RedisCache的實現,也實現了IDistributedCache1)和RedisCache2(同樣)。

事情是這樣的:

public interface IDistributedCache1 : IDistributedCache 
{ 
} 

public interface IDistributedCache2 : IDistributedCache 
{ 
} 

public class RedisCacheOptions1 : RedisCacheOptions 
{ 
} 

public class RedisCacheOptions2 : RedisCacheOptions 
{ 
} 

public class RedisCache1 : RedisCache, IDistributedCache1 
{ 
    public RedisCache1(IOptions<RedisCacheOptions1> optionsAccessor) : base(optionsAccessor) 
    { 
    } 
} 

public class RedisCache2 : RedisCache, IDistributedCache2 
{ 
    public RedisCache2(IOptions<RedisCacheOptions2> optionsAccessor) : base(optionsAccessor) 
    { 
    } 
} 

public class MyController : Controller 
{ 
    private readonly IDistributedCache _cache1; 
    private readonly IDistributedCache _cache2; 

    public MyController(IDistributedCache1 cache1, IDistributedCache2 cache2) 
    { 
     _cache1 = cache1; 
     _cache2 = cache2; 
    } 
} 

// Bootstrapping 

services.AddOptions(); 

services.Configure<RedisCacheOptions1>(config => 
{ 
    config.Configuration = Configuration.GetConnectionString("RedisCacheConnection1"); 
    config.InstanceName = "MYINSTANCE1"; 
}); 
services.Configure<RedisCacheOptions2>(config => 
{ 
    config.Configuration = Configuration.GetConnectionString("RedisCacheConnection2"); 
    config.InstanceName = "MYINSTANCE2"; 
}); 

services.Add(ServiceDescriptor.Singleton<IDistributedCache1, RedisCache1>()); 
services.Add(ServiceDescriptor.Singleton<IDistributedCache2, RedisCache2>()); 
+0

我其實和你一樣。你是什​​麼意思:「然而這個解決方案有點殺我」?我沒有看到這種方法有什麼問題,我認爲會正常工作 – user2818430

+1

我的意思是它需要更多的努力,它應該:( – CodeFuller