xxyy
2025-03-07 b7ee8c173ae75cfe32e58cc42020267fd155158a
增强缓存操作的线程安全性

在 `Dt_TaskService.cs` 中添加了对 `_simpleCacheService` 的锁定机制,以确保在并发情况下的线程安全。注释掉了 `GetFROutTrayToCW` 方法中的请求次数和时间限制代码,可能是为了简化逻辑或暂时禁用该功能。同时,在 `TaskController.cs` 中将 `ThrottleFilter` 的节流时间从 5 秒修改为 15 秒,以降低请求频率限制的严格性。
已修改3个文件
57 ■■■■■ 文件已修改
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Dt_TaskService.cs 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Partial/Dt_TaskService.cs 40 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/TaskController.cs 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Dt_TaskService.cs
@@ -137,12 +137,18 @@
            });
            try
            {
                _simpleCacheService.HashDel<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, new string[] { stock.PalletCode });
                using(_simpleCacheService.AcquireLock(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, 2000))
                {
                    _simpleCacheService.HashDel<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, new string[] { stock.PalletCode });
                }
            }
            catch (Exception ex)
            {
                LogFactory.GetLog("删除缓存失败").Error(true, $"{stock.PalletCode}_删除缓存失败,异常信息:{ex.Message}");
                _simpleCacheService.HashDel<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, new string[] { stock.PalletCode });
                using(_simpleCacheService.AcquireLock(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, 2000))
                {
                    _simpleCacheService.HashDel<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, new string[] { stock.PalletCode });
                }
            }
            return content.OK("任务完成成功", task.Remark);
@@ -380,7 +386,10 @@
                _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(locationInf, lastStatus, (int)StatusChangeTypeEnum.AutomaticStorage, task.TaskNum);
                stock.StockInfoDetails = new List<DtStockInfoDetail>() { { stock.StockInfoDetails[0] } };
                _simpleCacheService.HashAdd(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, stock.PalletCode, stock);
                using(_simpleCacheService.AcquireLock(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, 2000))
                {
                    _simpleCacheService.HashAdd(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, stock.PalletCode, stock);
                }
                content.OK("入库任务完成成功");
            }
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Partial/Dt_TaskService.cs
@@ -894,26 +894,26 @@
        WebResponseContent content = new WebResponseContent();
        try
        {
            string requestKey = JsonConvert.SerializeObject(taskDTO);
            // 检查请求次数和时间限制
            if (requestTrackerToCW.TryGetValue(requestKey, out var requestInfo))
            {
                if (requestInfo.Count > 5 && DateTime.Now < requestInfo.LastRequestTime.AddMinutes(2))
                {
                    // 如果请求次数超过限制且未超过10分钟,抛出异常
                    throw new InvalidOperationException("请求次数已达到限制,请稍后再试。");
                }
            }
            //string requestKey = JsonConvert.SerializeObject(taskDTO);
            //// 检查请求次数和时间限制
            //if (requestTrackerToCW.TryGetValue(requestKey, out var requestInfo))
            //{
            //    if (requestInfo.Count > 5 && DateTime.Now < requestInfo.LastRequestTime.AddMinutes(2))
            //    {
            //        // 如果请求次数超过限制且未超过10分钟,抛出异常
            //        throw new InvalidOperationException("请求次数已达到限制,请稍后再试。");
            //    }
            //}
            // 更新请求跟踪信息
            if (requestTrackerToCW.ContainsKey(requestKey))
            {
                requestTrackerToCW[requestKey] = (requestInfo.Count + 1, DateTime.Now);
            }
            else
            {
                requestTrackerToCW[requestKey] = (1, DateTime.Now);
            }
            //// 更新请求跟踪信息
            //if (requestTrackerToCW.ContainsKey(requestKey))
            //{
            //    requestTrackerToCW[requestKey] = (requestInfo.Count + 1, DateTime.Now);
            //}
            //else
            //{
            //    requestTrackerToCW[requestKey] = (1, DateTime.Now);
            //}
            var station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == taskDTO.Position && x.stationStatus == "1");
            var locations = _locationRepository.QueryData(x => x.RoadwayNo == station.Roadway && x.LocationStatus == (int)LocationEnum.Free && x.LocationType == 1);
@@ -947,7 +947,7 @@
                    _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(location, lastStatus, (int)StatusChangeTypeEnum.AutomaticDelivery, task.TaskNum);
                    // 返回成功响应
                    requestTrackerToCW.Remove(requestKey);
                    //requestTrackerToCW.Remove(requestKey);
                    return content.OK(data: wmsTask);
                }
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/TaskController.cs
@@ -162,7 +162,7 @@
    /// <param name="input">请求数据</param>
    /// <returns></returns>
    [HttpPost, AllowAnonymous, Route("GetFROutTrayToCW")]
    [TypeFilter(typeof(ThrottleFilter), Arguments = new object[] { 5 })] // 5秒节流
    [TypeFilter(typeof(ThrottleFilter), Arguments = new object[] { 15 })] // 5秒节流
    public async Task<WebResponseContent> GetFROutTrayToCW([FromBody] RequestTaskDto input)
    {
        return await Service.GetFROutTrayToCW(input);