繁体   English   中英

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

[英]Azure Redis Cache: is it possible to connect to the cache in an integration test?

我正在针对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”。 我不认为服务器会超时,因为超时时间很长,只有五秒钟,而且我正在使用快速连接的快速系统上。

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

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

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

理想情况下,我希望能够在自己的构建环境中运行这些测试,但目前无法检索有效的连接。 我看不到任何明显的迹象表明我可能会丢失文档。 我可以进行任何检查以确保自己做对了吗?

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

是的,我只是直接使用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;
        });

根据PING上的异常消息SocketFailure ,我认为您的开发环境防火墙正在阻止端口6379、6380

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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM