gpt4 book ai didi

c# - 如何将异步与锁定结合起来?

转载 作者:行者123 更新时间:2023-12-03 19:44:03 26 4
gpt4 key购买 nike

正如著名的blog post from Stephen Cleary所规定的那样,永远不要尝试同步运行异步代码(例如,通过Task.RunSynchronously()或访问Task.Result)。另一方面,您不能在lock语句中使用async/await。
我的用例是ASP.NET Core应用程序,它使用IMemoryCache缓存一些数据。现在,当数据不可用时(例如删除缓存),我必须重新填充它,应该使用lock保护它。

public TItem Get<TItem>(object key, Func<TItem> factory)
{
if (!_memoryCache.TryGetValue(key, out TItem value))
{
lock (_locker)
{
if (!_memoryCache.TryGetValue(key, out value))
{
value = factory();
Set(key, value);
}
}
}
return value;
}
在此示例中,工厂功能不能异步!如果必须异步该怎么办?

最佳答案

协调对共享变量的异步访问的一种简单方法是使用 SemaphoreSlim 。您调用WaitAsync开始异步锁定,并调用Release结束异步锁定。
例如。

private static readonly SemaphoreSlim _cachedCustomersAsyncLock = new SemaphoreSlim(1, 1);
private static ICollection<Customer> _cachedCustomers;

private async Task<ICollection<Customer>> GetCustomers()
{
if (_cachedCustomers is null)
{
await _cachedCustomersAsyncLock.WaitAsync();

try
{
if (_cachedCustomers is null)
{
_cachedCustomers = GetCustomersFromDatabase();
}
}
finally
{
_cachedCustomersAsyncLock.Release();
}
}

return _cachedCustomers;
}

关于c# - 如何将异步与锁定结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44269412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com