1

Putting await for async method call is mandatory in .net ?? For example, I have a redis set command that I don't want to wait for.

 .....statements....
await redis.Hashes.Set(db, mainKey, value);
.....statements after set command......

I don't want to pause the execution of my method in set command. So I removed 'await'.

.....statements....
redis.Hashes.Set(db, mainKey, value);
....statements after set command......

Is this a good practice??

Manu Mohan
  • 1,694
  • 2
  • 20
  • 31

1 Answers1

2

You can choose to not await a Task, but I not sure it's good practice. If there are exceptions thrown in the called Task you won't know about them.

See https://msdn.microsoft.com/en-gb/library/hh965065.aspx

Also, Booksleeve has been deprecated for quite a while now, and is replaced by StackExchange.Redis. You should probably look to use that instead, if possible.

StackExchange.Redis has a CommandFlags.FireAndForget parameter that you can specify which will immediately return the default value and not wait around for the command to complete.

connection.GetDatabase().HashSet(key, field, value, flags: CommandFlags.FireAndForget)

Documentation for FireAndForget

Sam
  • 341
  • 1
  • 4
  • Thanks, which is better? I can see a big difference in execution time between ConnectionProvider.GetConnection(); of Booksleev and connectionProvider.GetMultiplexer().GetDatabase(); of stackexchange. Booksleeve is far away better in connection time - i feel so. correct me if i am wrong – Manu Mohan Jun 03 '16 at 10:43
  • I would definitely recommend StackExchange.Redis over Booksleeve. Booksleeve is no longer maintained, and SE.Redis has better support for modern redis feature including failover, clusters, etc... Here's Mark Gravell's blog entry explaining why SE.Redis was written as a replacement - http://blog.marcgravell.com/2014/03/so-i-went-and-wrote-another-redis-client.html – Sam Jun 03 '16 at 11:24
  • Regarding the connection time, I don't know what Booksleeve does internally, but SE.Redis maintains and automatically tries to reconnect to configured redis instances. Since connections are persistent, the connection time shouldn't be a major factor in your decision about which library to use. – Sam Jun 03 '16 at 11:27