请问哪种是有效的
这是:var employeesFacts = await _cacheService.GetEmployeesFacts();
还是:var employeesFacts = _cacheService.GetEmployeesFacts().Result;
有点困惑。
ValueTask
实现方式?
缓存服务从缓存或数据库返回数据。public async ValueTask<IList<HrEmploymentDataCustom>> GetEmployeesFacts()
{
try
{
var facts = (List<HrEmploymentDataCustom>) _memoryCache.Get("facts");
return facts ?? await _accountService.GetEmploymentFacts(DetailsRequestType.All, null);
}
catch (Exception e)
{
var tc = new TelemetryClient();
tc.TrackException(e);
return null;
}
}
这是:var employeesFacts = await _cacheService.GetEmployeesFacts();
还是:var employeesFacts = _cacheService.GetEmployeesFacts().Result;
有点困惑。
await
等待一个任务。使用.Result
阻塞线程在最好情况下是低效的,在最坏情况下会导致死锁。 - Crowcoder