【问题标题】:Azure Redis Cache: is it possible to connect to the cache in an integration test?Azure Redis 缓存:是否可以在集成测试中连接到缓存?
【发布时间】:2017-08-02 16:23:15
【问题描述】:

我正在针对 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 字段显示为“PING 上的SocketFailure”。我不相信服务器会超时,因为超时窗口是 5 秒,而且我的系统速度很快,连接也很快。

连接字符串是标准 Azure Redis 缓存字符串,格式为

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

我尝试设置 ssl = False,但没有成功。

理想情况下,我希望能够在我的构建环境中运行这些测试,但目前我无法检索有效的连接。我看不到任何我可能在文档中遗漏的明显内容。是否可以进行任何检查以确保我做的是正确的事情?

【问题讨论】:

    标签: c# azure azure-redis-cache


    【解决方案1】:

    是否可以在集成测试中连接到缓存?

    是的,我直接使用 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 on PING,我假设您的开发环境防火墙正在阻塞端口6379、6380

    如果您的 redis 服务是高级定价层,请检查是否存在阻止客户端连接的防火墙规则。

    如果您的网络位于防火墙后面,请尝试确保端口已打开。或者请尝试使用其他网络环境。

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多