Skip to content

Instantly share code, notes, and snippets.

@evilbloodydemon
Last active December 31, 2015 09:59
Show Gist options
  • Select an option

  • Save evilbloodydemon/7970686 to your computer and use it in GitHub Desktop.

Select an option

Save evilbloodydemon/7970686 to your computer and use it in GitHub Desktop.
public async Task<string> Get(int id)
{
using (var conn = new RedisConnection("localhost"))
{
await conn.Open();
var channel = conn.GetOpenSubscriberChannel();
try
{
var result = string.Empty;
var tcs = new TaskCompletionSource<bool>();
await channel.Subscribe("artemis", (s, bytes) =>
{
try
{
result = Encoding.UTF8.GetString(bytes);
tcs.SetResult(true);
}
catch (Exception e)
{
tcs.SetException(e);
}
});
await Task.WhenAny(new[]
{
tcs.Task,
Task.Delay(TimeSpan.FromSeconds(25))
});
return result;
}
catch (Exception)
{
return String.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment