异步功能陷入僵局?

Func for async goes in deadlock?

Objective:

我正在使用 Redis 进行缓存,如果缓存值不可用,我会尝试从存储库获取缓存值并设置缓存值。

问题:

客户端 js 一直在等待,似乎处于某种死锁状态

导致问题的代码

   return await _cacheStore.GetAsync("CacheKey", new TimeSpan(24, 0, 0),
                 async () =>
                  await _repo.GetAllAsync()
                  );

有效的代码,但有点多余,因为我一直在很多地方使用这种模式。

    var data = await _cacheStore.GetAsync<List<objectType>>("cacheKey");
    if (data == null)
    {
        data = await _repo.GetAllAsync();
        _cacheStore.SetAsync<List<objectType>>("cacheKey", data, new TimeSpan(24, 0, 0));
    }
    return data;

//下面将异步方法作为 Func 传递的可能问题函数????? 以上代码调用的函数

public static async Task < T > GetAsync < T > (this IRedisCacheStore source, string key, TimeSpan time, Func < Task < T >> fetch) where T: class {
 if (source.Exists(key)) {
  return await source.GetAsync < T > (key);
 } else {
  var result = fetch();

  if (result != null) {
   source.Set(key, result, time);
  }

  return await result;
 }
}

public async Task < List < ObjectType >> GetAllAsync() {
 var result = await _procExecutor.ExecuteProcAsync < ObjectType > ("Sproc", null);

 return (result ? ? new List < ObjectType > ()).ToList();
}

public async Task < IEnumerable < TResult >> ExecuteProcAsync < TResult > (string sproc, object param) {
  using(var conn = _connection.GetConnection()) {
   return await conn.QueryAsync < TResult > (sproc, param, commandType: CommandType.StoredProcedure);
  }

这是我的redis cacheStore

public interface IRedisCacheStore
{
    bool Exists(string key);
    T Get<T>(string key);
    void Set<T>(string key, T value, TimeSpan expiredIn);
    void Remove(string key);

    Task<bool> ExistsAsync(string key);
    Task<T> GetAsync<T>(string key);
    Task SetAsync<T>(string key, T value, TimeSpan expiredIn);
    Task RemoveAsync(string key);
}

问题出在这里:

var result = fetch();

if (result != null) {
   source.Set(key, result, time);
} 

fetch() returns Task<T>,这就是您要存储在 Redis 缓存中的内容,而任务(显然)不可序列化。只需使用:

var result = await fetch();