【发布时间】:2021-03-02 18:08:44
【问题描述】:
注意:我使用了语言翻译
我在 C# 中使用的 api 系统中的一个方法中过滤并返回缓存的数据。如果缓存中没有数据,我会异步从数据库中拉取数据。如果缓存中没有数据,则同时来到 api 的投标者在异步代码中相互碾压。所以第一个请求者必须是第一个拉数据的人,其他人应该等待第一个请求者拉数据。我写的代码:
[HttpPost]
public ... GetPersonels(...)
{
var list = caching.Get<...>([CacheName]);
if (list is null)
{
System.Threading.Tasks.Task.Factory.StartNew(InsertCacheForGetPersonels);
}
...
return [Filtred Personels];
}
protected void InsertCacheForGetPersonels()
{
//HERE !!!
//what should i do here ?
//HERE !!!
if (caching.Get<...>([CacheName]) is null)
{
var list = GetPersonelList();
caching.Set(list, [CacheName], Indefinite());
}
}
【问题讨论】:
标签: c# asynchronous queue task-queue