2016-11-07 85 views
2

我正在使用ASP.NET Core和Redis緩存。我試圖在緩存中存儲不同類型的不同對象,我想避免顯式強制轉換。在緩存不同類型時使用泛型避免投射

這是我的包裝對於Redis的緩存

public class RedisCacheStorage : Contracts.ICacheStorage 
{ 
    private CachingFramework.Redis.Context _context = null; 

    public RedisCacheStorage(string configuration) 
    { 
     _context = new CachingFramework.Redis.Context(configuration, new CachingFramework.Redis.Serializers.JsonSerializer()); 
    } 
    public void SetItem<T>(string key, T value) 
    { 
     _context.Cache.SetObject<T>(key, value); 
    } 
    public T GetItem<T>(string key) 
    { 
     return _context.Cache.GetObject<T>(key); 
    } 

    public T GetItem<T>(string key, Func<T> loadCacheFunc) 
    { 
     return _context.Cache.FetchObject<T>(key, loadCacheFunc); 
    } 

然後我注入ICacheStorage在CacheManager中(實現的ICacheManager)。我試圖分離依賴關係並簡化CacheStorage,所以當我需要更改緩存類型時,我只實現了ICacheStorage。在CacheManager中,我們注入了所有在特殊鍵傳遞時獲取一些數據的服務。

的CacheManager

public class CacheManager : Contracts.ICacheManager 
{ 
    private Contracts.ICacheStorage _cacheStorage; 
    private SecurityCore.ServiceContracts.IParametersService _paramService; 
    public CacheManager(Contracts.ICacheStorage cacheStorage, SecurityCore.ServiceContracts.IParametersService paramService) 
    { 
     _cacheStorage = cacheStorage; 
     _paramService = paramService; 
    } 
    public Object GetItem(string key) 
    { 
     if (key == Constants.CacheKeys.SecuritySystemParams) 
      return _cacheStorage.GetItem<Dictionary<string, string>>(key, _paramService.GetSystemParameters); 

     //if (key == Constants.CacheKeys.EffectivePermissions) 
     // return List of Effective Permissions 

     return _cacheStorage.GetItem<Object>(key); 
    } 

_cacheStorage.GetItem<Dictionary<string, string>>(key, _paramService.GetSystemParameters);

通行證使用該取Redis的方法,如果緩存是空的,它會調用該服務,然後將數據存儲在一個函數緩存並返回。

我的問題是我需要避免投射,因爲我可能會返回不同的對象,我如何繼續使用泛型,所以我傳遞了返回的對象的類型。

正如您在下面看到的一個編譯錯誤,由於無法將類型對象轉換爲Dictionay,因此需要顯式轉換才能解析。

是否有更好的,優雅的方式來實現整個想法?

enter image description here

回答

3

閱讀錯誤信息。
您需要明確指定類型參數。

你可以讓這更好的與類型安全密鑰:

class CacheKey<T> { 
    public string Name { get; } 
    public string ToString() => Name; 
    public CacheKey(string name) { Name = name; } 
} 

public T GetItem<T>(CacheKey<T> key) { ... } 

public CacheKey<Dictionary<string, string>> SecuritySystemParams { get; } = new CacheKey<Dictionary<string, string>>("SecuritySystemParams"); 

這將讓GetItem()推斷T從鑰匙,並會阻止您傳遞錯誤的類型。

+0

'公共Ť的GetItem (CacheKey 鍵) { 如果(key.Name == SecuritySystemParams.Name) 返回_cacheStorage.GetItem (key.Name,_paramService.GetSystemParameters()); return _cacheStorage.GetItem (key.Name); }'。我想我誤解了這一點,但我們仍然錯過了一些東西,因爲它不能將'_paramService.GetSystemParameters()'的返回對象轉換爲'T'。那麼,實現如何在內部發生變化。 – Coding

+0

@ h.salman:使用'(T)(object)...' – SLaks