Created
May 4, 2015 04:15
-
-
Save programmation/9003f33b4e44dcf97c6e to your computer and use it in GitHub Desktop.
Async cache
This file contains hidden or 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
// 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