2017-08-01 80 views
0

我正在針對Azure Redis緩存運行集成測試。這是我非常簡單的緩存實現:Azure Redis緩存:是否可以在集成測試中連接到緩存?

public class RedisCacheEngine : ICacheEngine 
{ 
    private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() => 
    { 
     var config = new ConfigurationService(); 
     var connectionString = config.Get("Redis.ConnectionString"); 
     var connection = ConnectionMultiplexer.Connect(connectionString); 
     return connection; 
    }); 

    private static ConnectionMultiplexer Connection => LazyConnection.Value; 

    public TValue Get<TValue>(string key) where TValue : class 
    { 
     var redisValue = Connection.GetDatabase().StringGet(key); 
     return redisValue == RedisValue.Null 
      ? default(TValue) 
      : JsonConvert.DeserializeObject<TValue>(redisValue); 
    } 

    public void Set<TValue>(string key, TValue value) where TValue : class => 
     Connection.GetDatabase().StringSet(key, JsonConvert.SerializeObject(value)); 

    public void Remove(string key) => Connection.GetDatabase().KeyDelete(key); 
} 

當我看到在調試器中connection對象,其failureMessage場寫着「SocketFailure上PING」。我不相信服務器超時,因爲超時窗口很慷慨五秒,而且我正在一個快速的系統上,連接速度很快。

連接字符串是一個標準的Redis天青緩存串,形式

myapp.redis.cache.windows.net:6380,password=___,ssl=True,abortConnect=False

我的嘗試設置ssl = False,但沒有成功。

我希望能夠在我的構建環境中運行這些測試,但目前我無法檢索工作連接。我看不出有什麼明顯的證據表明我可能會失蹤。是否有任何檢查可以確保我做正確的事情?

回答

0

是否可以在集成測試中連接到緩存?

是的,我做了演示與你提到的代碼只是使用直接的ConnectionString,它工作正常在我的身邊。

private static readonly Lazy<ConnectionMultiplexer> LazyConnection = new Lazy<ConnectionMultiplexer>(() => 
     { 
      var connectionString = "mytest.redis.cache.windows.net:6380,password=xxxxxxxx,ssl=True,abortConnect=False"; 
      var connection = ConnectionMultiplexer.Connect(connectionString); 
      return connection; 
     }); 

根據異常消息SocketFailure上PING,我認爲你的開發環境,防火牆,阻止端口6379,6380

如果您的redis服務是保費價格層,請檢查是否存在阻止客戶端連接的防火牆規則。

如果您的網絡位於防火牆後面,請嘗試確保端口處於打開狀態。或者請嘗試使用其他網絡環境。

enter image description here