Skip to content

Instantly share code, notes, and snippets.

@MikeGoldsmith
Last active July 21, 2017 12:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeGoldsmith/98257ca0c2ca4576df5ede57c6c123c8 to your computer and use it in GitHub Desktop.
Save MikeGoldsmith/98257ca0c2ca4576df5ede57c6c123c8 to your computer and use it in GitHub Desktop.
example of doing async multi get
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