简体   繁体   English

当每秒有400-500个请求时,使用“ DatabaseAsync”通过c#进行Redis异步调用

[英]Redis async call by c#, using “DatabaseAsync” makes response slower when there are 400-500 requests per second

when I use: 当我使用时:

`public call(Func<IDatabase, Task<T>> func){
  var task=func(redisInstance); 
  task.wait();
  return task.Result;
}`

call: call(client => redisInstance.SetMembersAsync(setName)) 呼叫: call(client => redisInstance.SetMembersAsync(setName))

It works Fine, but when there are many requests 'CPU' usage increases; 它工作正常,但是当有许多请求时,“ CPU”使用率增加;

but in second case: 但在第二种情况下:

`public async call2(Func<IDatabase, Task<T>> func){
   var task=func(redisInstance); 
   return await task.Result;
}`

call: call2(async client => await redisInstance.SetMembersAsync(setName)) 呼叫: call2(async client => await redisInstance.SetMembersAsync(setName))

CPU is ok but responses take much time; CPU可以,但是响应需要很多时间;

any Ideas about this case? 关于这个案例有什么想法吗? what is wrong in second method? 第二种方法有什么问题?

There are 2 things I think you could do to improve the wall-clock time speed under load. 我认为有两种方法可以提高负载下的挂钟时间速度。

  1. There are 2 await s, and this is because you have double wrapped the result, so it's now Task<Task<T>> . 有2个await ,这是因为您已经对结果进行了两次包装,所以现在是Task<Task<T>> Change your call to: 将您的通话更改为:

     call2(client => redisInstance.SetMembersAsync(setName)) 
  2. You could optimize by removing the implicit synchronization of the continuation after awaiting, by using .ConfigureAwait(false) , so change your await to: 您可以通过使用.ConfigureAwait(false)来删除等待后的隐式同步,以进行优化,因此将await更改为:

     return await task.ConfigureAwait(false); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM