如何解决使用 Azure Redis 缓存时等待响应超时的问题?

How to fix Timeout awaiting response issue when using Azure Redis Cache?

我有一个方法将输入作为列表并将每个项目存储在 Azure Redis 缓存中。

public async Task<bool> StoreAsBatch<T>(T data)
        {
            var storeData = new List<Task<bool>>();
            try
            {
                foreach (var each in data as List<EmpUser>)
                {
                    storeData.Add(Store(each.UserId, each));
                }

                await Task.WhenAll(storeData).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.LogError($"StoreAsBatch failed with an exception - {ex.Message} ");
                return false;
            }
            
            return true;
        }

这是存储方法

public async Task<bool> Store<T>(string key, T value)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentException(nameof(key));
            key = $"emp_user_{key}";
            string val = JsonConvert.SerializeObject(value);
            return await _database.StringSetAsync(key, JsonConvert.SerializeObject(value), new TimeSpan(30, 0, 0, 0,0));
        }

当我将列表(列表大小:1k 条记录)传递给上述 StoreAsBatch 方法时。我遇到这样的异常 错误

Timeout awaiting response (outbound=0KiB, inbound=0KiB, 7625ms elapsed, timeout is 5000ms), command=SETEX, next: SETEX emp_user_00mb1, inst: 0, qu: 0, qs: 1, aw: True, rs: DequeueResult, ws: Writing, in: 0, serverEndpoint: mrcooper-originations-boss-dev.redis.cache.windows.net:6380, mc: 1/1/0, mgr: 9 of 10 available, clientName: WIN10H-DLPIH45D, IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=2,Free=32765,Min=8,Max=32767), v: 2.1.58.34321 (Please take a look at this article for some common client-side issues that can cause timeout) 

我是 Azure Redis 缓存的新手。请帮助我解决错误。

提前致谢。

基于@CamiloTervinto 的建议并参考此 Redis Configuration 文档。

我可以通过在设置 redis 缓存时增加异步调用的阈值时间来解决我的错误。

这是参考代码。

方法一

public void SetUpCacheManager()
        {
            try
            {
                _lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
                { 
                    string connectionString = _config.AzureRedis.HostName + _config.AzureRedis.Password + _config.AzureRedis.Ssl;
                    var redisConfig = ConfigurationOptions.Parse(connectionString);
                    redisConfig.AsyncTimeout = 15000;
                    return ConnectionMultiplexer.Connect(redisConfig);
                });

                _database = Connection.GetDatabase();
            }
            catch(Exception e)
            {
                _logger.LogError($"(SetUpCacheManager): Exception in Connecting to Cache, Message: {e.Message}");
            }  
        }

方法二

        public async Task<bool> StoreAsBatch(List<EncompassUser> data)
        {
            Parallel.ForEach(data, new ParallelOptions{MaxDegreeOfParallelism =  10 }, (obj) =>
            {
                    try
                    {
                        Store(obj.UserId, obj);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"StoreAsBatch failed with an exception - {ex.Message} ");
                    }
            });
            return true;
        }