根因:
- MarketDataService 是 Scoped,每个 HTTP 请求有独立实例
- 两个请求同时查询同一股票,各自有自己的 _memoryCache 和 _pendingPriceRequests
- 导致重复查询数据库,连接冲突
修复:
1. _memoryCache 改为 static,跨请求共享
2. _pendingPriceRequests 改为 static,跨请求共享
3. GetOrAdd 正确模式:先创建 Lazy,再 GetOrAdd
流程:
请求1 → static _memoryCache 未命中 → static _pendingPriceRequests.GetOrAdd
请求2 → static _memoryCache 未命中 → static _pendingPriceRequests.GetOrAdd(复用请求1的 Lazy)
↓
只查一次数据库
|
||
|---|---|---|
| .. | ||
| Services | ||
| StrategyEngine | ||
| AssetManager.Infrastructure.csproj | ||