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

问题描述

我有一种方法将输入作为列表并将每个项目存储在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;
        }

这是Store方法

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));
        }

当我将列表(列表大小: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,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缓存时,我可以通过增加异步调用的阈值时间来解决错误。

这是参考代码。

方法1

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}");
            }  
        }

方法2

        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;
        }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...