2017-08-01 83 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