Last active
July 21, 2017 12:22
-
-
Save MikeGoldsmith/98257ca0c2ca4576df5ede57c6c123c8 to your computer and use it in GitHub Desktop.
example of doing async multi get
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var config = new ClientConfiguration {Servers = new List<Uri> {new Uri("http://10.112.170.191")}}; | |
using (var cluster = new Cluster(config)) | |
{ | |
var bucket = cluster.OpenBucket("default"); | |
var keys = new List<string> | |
{ | |
"test1", "test2", "test3" | |
}; | |
// using Task.When to build task list | |
// could also use await instead of.Wait() | |
var resultsA = new ConcurrentDictionary<string, IOperationResult<dynamic>>(); | |
Task.WhenAll( | |
keys.Select( | |
key => Task.Run(async () => resultsA.TryAdd(key, await bucket.GetAsync<dynamic>(key))) | |
) | |
).Wait(); | |
// using Parallel inside task to prevent blocking current thread | |
// could also use await instead of.Wait() | |
var resultsB = new ConcurrentDictionary<string, IOperationResult<dynamic>>(); | |
Task.Run( | |
() => Parallel.ForEach(keys, async key => | |
resultsB.TryAdd(key, await bucket.GetAsync<dynamic>(key)) | |
) | |
).Wait(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment