Skip to content

Instantly share code, notes, and snippets.

@programmation
Created May 4, 2015 04:15
Show Gist options
  • Save programmation/9003f33b4e44dcf97c6e to your computer and use it in GitHub Desktop.
Save programmation/9003f33b4e44dcf97c6e to your computer and use it in GitHub Desktop.
Async cache
// http://blog.stephencleary.com/2015/04/a-tour-of-task-part-10-promise-tasks.html
public Task<string> GetValueAsync(int key)
{
string result;
if (cache.TryGetValue(key, out result))
return Task.FromResult(result);
return DoGetValueAsync(key);
}
private async Task<string> DoGetValueAsync(int key)
{
string result = await ...;
cache.TrySetValue(key, result);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment